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.
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.
InputFor 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.
OutputIf 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
点染色判定二分图,将二分图分成2个部分,之后匈牙利求最大匹配即可。
1 #include<iostream> 2 using namespace std; 3 #include<cstdio> 4 #include<cstring> 5 #include<vector> 6 const int maxn = 1000; 7 vector <int> maps[maxn]; 8 int n,m; 9 int ok[maxn];//ok[i]代表第i个节点可否配对 10 int matched[maxn];//matched[i]代表第i个节点的配对对象; 11 bool match(int x){ 12 for(int i=0;i<maps[x].size();i++){ 13 int k = maps[x][i]; 14 if(ok[k]==0){ 15 ok[k]=1; 16 if(matched[k]==0||match(matched[k])==true){ 17 matched[k]=x; 18 return true; 19 } 20 } 21 } 22 return false; 23 } 24 int ans = 1; 25 int color[1100]; 26 void dfs(int x,int Color){ 27 color[x]=Color; 28 for(int i=0;i<maps[x].size()&&ans;i++){ 29 if(color[maps[x][i]]!=-1){ 30 if(color[maps[x][i]]==Color){ 31 ans = 0; 32 return; 33 } 34 } 35 else 36 dfs(maps[x][i],!Color); 37 } 38 } 39 int main(){ 40 while(scanf("%d%d",&n,&m)!=EOF){ 41 for(int i=1;i<=n;i++) 42 maps[i].clear(); 43 memset(matched,0,sizeof(matched)); 44 ans = 1; 45 for(int i=0;i<n;i++) 46 maps[i].clear(); 47 memset(color,-1,sizeof(color)); 48 for(int i=1;i<=m;i++){ 49 int k,q; 50 scanf("%d%d",&k,&q); 51 maps[k].push_back(q); 52 maps[q].push_back(k); 53 } 54 ans = 1; 55 for(int i=1;i<=n&&ans;i++){ 56 if(color[i]!=-1) 57 continue; 58 dfs(i,0); 59 } 60 if(!ans){ 61 printf("No\n"); 62 continue; 63 } 64 ans = 0; 65 for(int i=1;i<=n;i++){ 66 memset(ok,0,sizeof(ok)); 67 if(color[i]!=0) 68 continue; 69 if(match(i)==true) 70 ans+=1; 71 } 72 cout<<ans<<endl; 73 } 74 return 0; 75 }