1. 程式人生 > >HDU2444 :The Accomodation of Students(二分圖染色+二分圖匹配)

HDU2444 :The Accomodation of Students(二分圖染色+二分圖匹配)

The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9477    Accepted Submission(s): 4165

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2444

Description:

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 6

Sample Output:

No 3 題意: n個學生,m對互相認識彼此,問能不能把互相認識的分到同一個房間,不能的話輸出No,能的話輸出最大對數。 題解: 考慮能的情況,顯然是二分圖的最大匹配,A和B一個房間,可以看作A,B以選中,A,B自然不能和其它人在一個房間,其餘的也同理。 題幹中說的是分成兩個group,每一組中的人互不認識,這樣就可以構成一個二分圖。如果存在二分圖,那麼就必然有最大匹配。 如果同一組中有認識的人,那麼就不能構成二分圖,所以還需要二分圖的判斷(二分圖染色)。 注意一點細節:由於要二分圖染色,所以連的是雙向邊,最後求最大匹配的時候求了兩次。   程式碼如下:
#include <cstdio>
#include 
<cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 205; int link[N][N],match[N],check[N],color[N]; int n,m,ans=0; inline void init(){ ans=0;memset(color,-1,sizeof(color)); memset(link,0,sizeof(link));memset(match,-1,sizeof(match)); } inline int dfs(int x){ for(int i=1;i<=n;i++){ if(link[x][i] && !check[i]){ check[i]=1; if(match[i]==-1 || dfs(match[i])){ match[i]=x; return 1; } } } return 0; } inline bool ok(int x){ //二分圖染色 for(int i=1;i<=n;i++){ if(link[x][i]){ if(color[i]==-1){ color[i]=1-color[x]; if(!ok(i)) return false; }else if(color[i]==color[x]) return false ; } } return true; } /*dfs實現 ,連通圖直接呼叫ok(1,0) inline bool ok(int x,int c){ color[x]=c; for(int i=1;i<=n;i++){ if(link[x][i]){ if(color[i]==-1){ if(!ok(i,1-color[x])) return false; }else if(color[i]==color[x]) return false ; } } return true; } */ int main(){ while(scanf("%d%d",&n,&m)!=EOF){ init(); for(int i=1,a,b;i<=m;i++){ scanf("%d%d",&a,&b); link[a][b]=1; link[b][a]=1; } bool flag=false ; for(int i=1;i<=n;i++){ if(color[i]==-1){ color[i]=0; if(!ok(i)){ flag=true;break; } } } if(flag){ puts("No");continue; } for(int i=1;i<=n;i++){ memset(check,0,sizeof(check)); if(dfs(i)) ans++; } printf("%d\n",ans/2); } return 0; }