hdu2444The Accomodation of Students

来源:互联网 发布:小清新治愈电影知乎 编辑:程序博客网 时间:2024/05/22 00:22

Problem 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 41 21 31 42 36 51 21 31 42 53 6

Sample Output
No3

Source
2008 Asia Harbin Regional Contest Online


二分图判断+求最大匹配

用染色法来判断二分图,操作就是,对每个点bfs,然后把相连的点的颜色染为不同的颜色,如果有一段上两个端点颜色一样,那么就是不能形成二分图。
证明:
  这里用到一个定理:二分图的充要条件是“所有环路的长度均为偶数”,那么一旦找到一条线段,然后两个端点(设为u,v)颜色一样,那么这条线一定不是在bfs树上出现的,也就是说, 这条线段和bfs树上从u到v的路形成一个环,而且bfs上的路,每个相邻点的颜色不一样,那么这条bfs树上的路径长度为偶数,所以这个环的长度一定是奇数,因此不可能形成二分图。
#include<map>#include<set>#include<list>#include<stack>#include<queue>#include<cmath>#include<cstdio>#include<string>#include<iterator>#include<iostream>#include<algorithm>using namespace std;const int maxn=220;struct node{int to;int next;}edge[maxn*maxn];int head[maxn];int tot;int col[maxn];bool used[maxn];int mark[maxn];int n,m;void addedge(int from,int to){  edge[tot].to=to;  edge[tot].next=head[from];  head[from]=tot++;}bool bfs(int s){      queue<int> p;      p.push(s);      col[s] = 1;      while(!p.empty()) {          int from = p.front();          p.pop();          for(int i=head[from];i!=-1;i=edge[i].next){if(col[edge[i].to]==-1){  p.push(edge[i].to);  col[edge[i].to]=!col[from];}if(col[from]==col[edge[i].to])  return false;}     }      return true;       } bool dfs(int x){for(int i=head[x];i!=-1;i=edge[i].next){if(!used[edge[i].to]){used[edge[i].to]=1;if(mark[edge[i].to]==-1 || dfs(mark[edge[i].to])){mark[edge[i].to]=x;return true;}}}return false;}int hungary(){int ans=0;memset(mark,-1,sizeof(mark));for(int i=1;i<=n;i++){memset(used,0,sizeof(used));if(dfs(i))  ans++;}return ans;}int main(){while(~scanf("%d%d",&n,&m)){tot=0;memset(head,-1,sizeof(head));memset(col,-1,sizeof(col));int u,v;for(int i=0;i<m;i++){scanf("%d%d",&u,&v);addedge(u,v);addedge(v,u);}bool flag=false;for(int i=1;i<=n;i++){if(col[i]==-1 && !bfs(i))   {flag=true;break;   }}        if(flag)  printf("No\n");        else          printf("%d\n",hungary()/2);}return 0;}
0 0