HDU2444 The Accomodation of Students(判断二分图+最大匹配)

来源:互联网 发布:java swing教程 pdf 编辑:程序博客网 时间:2024/06/08 21:07

The Accomodation of Students

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


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



思路:二分图的判断以及求最大匹配数,

#include <cstdio>  #include <vector>  #include <cstring>  #include <algorithm>  using namespace std;    int n,m,vis[205],color[205],link[205];  vector<int>p[205];    int dfs(int u){//染色法判断是否能够形成二分图       for(int i = 0; i < p[u].size(); i ++){          int v = p[u][i];          if(!color[v]){              color[v] = !color[u];              if(!dfs(v))                  return 0;          }          else if(color[v] == color[u])                  return 0;      }      return 1;  }    int find(int x){//匈牙利算法求最大匹配       for(int i = 0; i < p[x].size(); i ++){          int v = p[x][i];          if(!vis[v]){              vis[v] = 1;              if(!link[v] || find(link[v])){                  link[v] = x;                  return 1;              }          }      }      return 0;  }    int main(){      while(~scanf("%d%d",&n,&m)){          for(int i = 0; i <= n; i ++)              p[i].clear();             int a,b;          for(int i = 0; i < m; i ++){              scanf("%d%d",&a,&b);              p[a].push_back(b);  //          p[b].push_back(a);  //无向图的话输出要cnt/2;           }          memset(color,0,sizeof(color));                    int flag = 0;          for(int i = 1; i <= n; i ++){              if(!dfs(i)){                  flag = 1;                  break;              }          }                    if(flag){//不能构成二分图               printf("No\n");          }          else{//求二分图的最大匹配               int cnt = 0;              memset(link,0,sizeof(link));              for(int i = 1; i <= n; i ++){                  memset(vis,0,sizeof(vis));                  if(find(i))                      cnt ++;              }              printf("%d\n",cnt);          }      }      return 0;  }  


0 0
原创粉丝点击