The Accomodation of Students

The Accomodation of Students

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): 9349    Accepted Submission(s): 4104


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
 
先用num数组判重,然后跑二分图。。还有,输出是No不是NO。。
 1 #include<iostream>
 2 #include<cmath>
 3 #include<vector>
 4 #include<cstring>
 5 #include<algorithm>
 6 #define mem(a,b) memset(a,b,sizeof(a))
 7 using namespace std;
 8 
 9 int mp[205][205];
10 int man[205],woman[205];
11 int flag[205],vis[205];
12 int n,m;
13 
14 int DFS(int u){
15     for(int i=1;i<=n;i++){
16         if(mp[u][i]&&!vis[i]){
17             vis[i]=1;
18             if(!man[i]||DFS(man[i])){
19                 woman[u]=i;
20                 man[i]=u;
21                 return 1;
22             }
23         }
24     }
25     return 0;
26 }
27 
28 int Match(){
29     mem(man,0);
30     mem(woman,0);
31     int ans=0;
32     for(int i=1;i<=n;i++){
33         if(!woman[i]){
34             mem(vis,0);
35             ans+=DFS(i);
36         }
37     }
38     return ans/2;
39 }
40 
41 int main(){
42     while(cin>>n>>m){
43         int a,b;
44         mem(mp,0);
45         mem(flag,0);
46         int Flag=0;
47         for(int i=1;i<=m;i++){
48             cin>>a>>b;
49             mp[a][b]=mp[b][a]=1;
50             if(!flag[a]&&!flag[b]) flag[a]=1,flag[b]=2;
51             else if(flag[a]==1&&!flag[b]) flag[b]=2;
52             else if(flag[a]==2&&!flag[b]) flag[b]=1;
53             else if(!flag[a]&&flag[b]==1) flag[a]=2;
54             else if(!flag[a]&&flag[b]==2) flag[a]=1;
55             else if(flag[a]==flag[b]&&flag[a]) Flag=1;
56         }
57         if(Flag) cout<<"No"<<endl;
58         else cout<<Match()<<endl;
59     }
60 }
View Code

 

posted on 2018-10-07 22:54  Fighting_sh  阅读(231)  评论(0编辑  收藏  举报

导航