1. 程式人生 > >HDU——T 2444 The Accomodation of Students

HDU——T 2444 The Accomodation of Students

sca who href several otto name tin logs ins

http://acm.hdu.edu.cn/showproblem.php?pid=2444

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


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 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

Source 2008 Asia Harbin Regional Contest Online

Recommend gaojie | We have carefully selected several similar problems for you: 2438 2443 2442 2441 2440 蒟蒻無腦法:二分圖染色+二分圖匹配模板 代碼好詭異
  1 #include <algorithm>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <queue>
  5 
  6 using namespace std;
  7 
  8 const int N(233);
  9 int n,m,head[N],sumedge;
 10 struct Edge
 11 {
 12     int v,next;
 13     Edge(int v=0,int next=0):v(v),next(next){}
 14 }edge[233333];
 15 inline void ins(int u,int v)
 16 {
 17     edge[++sumedge]=Edge(v,head[u]);
 18     head[u]=sumedge;
 19     edge[++sumedge]=Edge(u,head[v]);
 20     head[v]=sumedge;
 21 }
 22 
 23 int col[N];
 24 bool Paint(int x)
 25 {
 26     col[x]=0;
 27     queue<int>que;
 28     que.push(x);
 29     for(int u,v;!que.empty();)
 30     {
 31         u=que.front(); que.pop();
 32         for(int i=head[u];i;i=edge[i].next)
 33         {
 34             v=edge[i].v;
 35             if(col[v]!=-1)
 36             {
 37                 if(col[v]==col[u])
 38                     return false;
 39             }
 40             else
 41             {
 42                 col[v]=col[u]^1;
 43                 que.push(v);
 44             }
 45         }
 46     }
 47     return true;
 48 }
 49 
 50 int sumvis,vis[N],match[N],Map[N][N];
 51 bool find(int u)
 52 {
 53     /*for(int v,i=head[i];i;i=edge[i].next)
 54     {
 55         v=edge[i].v;
 56         if(!vis[v])
 57         {
 58             vis[v]=1;
 59             if(!match[v]||find(match[v]))
 60             {
 61                 match[v]=u;
 62                 return true;
 63             }
 64         }
 65     }*/
 66     for(int v=1;v<=n;v++)
 67         if(Map[u][v]&&!vis[v])
 68         {
 69             vis[v]=1;
 70             if(!match[v]||find(match[v]))
 71             {
 72                 match[v]=u;
 73                 return true;
 74             }
 75         }
 76     return false;
 77 }
 78 
 79 inline void init()
 80 {
 81     memset(edge,0,sizeof(edge));
 82     memset(head,0,sizeof(head));
 83     memset(match,0,sizeof(match));
 84 }
 85 
 86 int main()
 87 {
 88     for(;~scanf("%d%d",&n,&m);init())
 89     {
 90         int ans=0,flag=0;sumvis=0;
 91         memset(Map,0,sizeof(Map));
 92         for(int u,v;m--;ins(u,v))
 93             scanf("%d%d",&u,&v),Map[u][v]=1;
 94         memset(col,-1,sizeof(col));
 95         for(int i=1;i<=n;i++)
 96             if(col[i]==-1)
 97                 if(!Paint(i))
 98                 {
 99                     flag=1;
100                     break;
101                 }
102         if(flag)
103         {
104             puts("No");
105             continue;
106         }
107         for(int i=1;i<=n;i++)
108         {
109             if(find(i)) ans++;
110             memset(vis,0,sizeof(vis));
111         }
112         printf("%d\n",ans);
113     }
114     return 0;
115 }

HDU——T 2444 The Accomodation of Students