hdu 2444 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): 1743    Accepted Submission(s): 843


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
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2438 2443 2442 2441 2440 
 
 1 //15MS    992K    1490 B    C++
 2 /*
 3     
 4     题意:
 5         给出一个图,判该图是否为二分图,如果是则输出最大匹配 
 6     
 7     二分图判断+二分匹配:
 8         
 9         二分图判断用染色法,该方法:
10             选取某个点作为起点并染为某种颜色、同时把与它相邻的元素
11         染为对立的颜色,进行BFS,如果到那步发现当前点和相邻点的颜色
12         一样,那么就出现了矛盾,就不是二分图。
13         (个人认为此处理方法在图是全部点联通的情况下可行)
14         
15         二分匹配:
16             用匈牙利算法模板即可 
17  
18 */
19 #include<iostream>
20 #include<queue>
21 #include<vector>
22 #define N 205
23 using namespace std;
24 int n,m;
25 int dye[N];
26 int match[N];
27 int vis[N];
28 vector<int>V[N*N];
29 int dye_judge()
30 {
31     queue<int>Q;
32     memset(dye,-1,sizeof(dye));
33     dye[1]=1;
34     Q.push(1);
35     while(!Q.empty()){
36         int t=Q.front();
37         Q.pop();
38         int tdye=1-dye[t];
39         int n0=V[t].size();
40         for(int i=0;i<n0;i++){
41             int v=V[t][i];
42             if(dye[v]==-1){
43                 dye[v]=tdye;
44                 Q.push(v);
45             }else if(dye[v]==dye[t]) return 1;
46         }
47     }
48     return 0;
49 }
50 int dfs(int u)
51 {
52     int n0=V[u].size();
53     for(int i=0;i<n0;i++){
54         int v=V[u][i];
55         if(!vis[v]){
56             vis[v]=1;
57             if(match[v]==-1 || dfs(match[v])){
58                 match[v]=u;
59                 return 1;
60             }
61         }
62     }
63     return 0;
64 }
65 void hungary()
66 {
67     int cnt=0;
68     memset(match,-1,sizeof(match));
69     for(int i=1;i<=n;i++){
70         memset(vis,0,sizeof(vis));
71         cnt+=dfs(i);
72     }
73     printf("%d\n",cnt/2);
74 }
75 int main(void)
76 {
77     int a,b;
78     while(scanf("%d%d",&n,&m)!=EOF)
79     {
80         for(int i=0;i<=n;i++) V[i].clear();
81         for(int i=0;i<m;i++){
82             scanf("%d%d",&a,&b);
83             V[a].push_back(b);
84             V[b].push_back(a);
85         }
86         if(dye_judge()){
87             puts("No");continue;
88         }
89         hungary();
90     }
91     return 0;
92 }

 

posted @ 2013-11-06 16:18  heaventouch  阅读(183)  评论(0编辑  收藏  举报