【HDU - 2444】The Accomodation of Students(二分图判断 + 匈牙利算法求最大匹配)
題干:
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.?
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.?
Calculate the maximum number of pairs that can be arranged into these double rooms.?
Input
For each data set:?
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.?
Proceed to the end of file.?
?
Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.?
Sample Input
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6Sample Output
No 3題目大意:
有n個學(xué)生。其中有些人相互認(rèn)識,有些人相互之間不認(rèn)識。現(xiàn)在有m對認(rèn)識關(guān)系。問你能否把這n個人分成兩個組。一個組中沒有相互認(rèn)識的人。如果不能做到的話,輸出NO。如果可以的話,把這些人分到一些房間中,只有相互認(rèn)識的人能分到一個房間里。問你需要多少房間。
?
解題報告:
? ?這是判斷二分圖和二分圖匹配的裸題。
首先要判斷該圖是否為二分圖。當(dāng)該圖為二分圖時,才能分為兩個組。當(dāng)判斷為二分圖時,我們需要求出該二分圖的最大匹配數(shù)目。得出的結(jié)果除2就得出了需要房間數(shù)。(或者只對col==1的進行find匹配,這樣就不需要除以2了。)
二分圖的判定:
? ? 在判定一個圖是否為二分圖的時候只需要進行判斷圖中是否存在奇數(shù)長度的回路
? ? 常用的辦法是基于BFS的相鄰染色法 即對任意一個點進行BFS如果uv之間有一條邊 我們從u訪問到v 如果v未被染色 則v將被染成和u相對的染色(比如1和0)
? ?判斷出口:如果相鄰節(jié)點(u和v)是相同顏色 則存在奇回路
? ?還有個優(yōu)化(但是貌似沒卵用),就是used數(shù)組不是bool的,設(shè)成int,然后每次就不需要置零used了,直接看是否等于i就可以了,相當(dāng)于find中兩個參數(shù)find(i,i),其中bool find(int x,int id),看是否是id,就可以看出是否是
AC代碼:
#include<bits/stdc++.h> using namespace std; int nxt[205]; int col[205]; bool used[205]; int n,m; vector<int> vv[205]; bool bfs() {memset(col,-1,sizeof col);queue<int> q;q.push(1);col[1] = 1;while(!q.empty()) {int cur = q.front();q.pop();for(int i = 0; i<vv[cur].size(); i++) {int now = vv[cur][i];if(col[now] == -1) {col[now] = !col[cur];q.push(now);}else {if(col[now] == col[cur]) return 0;}}} return 1; } bool find(int x) {for(int i = 0; i<vv[x].size(); i++) {int now = vv[x][i];if(used[now] == 1) continue;used[now] = 1;if(nxt[now] == 0 || find(nxt[now])) {nxt[now] = x;return 1;}}return 0 ; } int main() {int u,v,ans;while(~scanf("%d%d",&n,&m)) {memset(nxt,0,sizeof nxt);for(int i = 0; i<=200; i++) vv[i].clear();for(int i = 1; i<=m; i++) {scanf("%d%d",&u,&v);vv[u].push_back(v);vv[v].push_back(u);}if(!bfs()) {puts("No");continue;}ans = 0;for(int i = 1; i<=n; i++) {memset(used,0,sizeof used);if(find(i)) ans++;}printf("%d\n",ans/2);}return 0; }總結(jié):
? ?1WA了,因為忘記了清空vv和nxt數(shù)組。
總結(jié)
以上是生活随笔為你收集整理的【HDU - 2444】The Accomodation of Students(二分图判断 + 匈牙利算法求最大匹配)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双币信用卡怎么还 两种结算方式及时掌握
- 下一篇: 2020年我国实际使用外资近万亿,超过美