HDU - 2444 The Accomodation of Studentsp[二分图判定,匈牙利算法]

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. 

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

OutputIf 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

点染色判定二分图,将二分图分成2个部分,之后匈牙利求最大匹配即可。

 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<vector>
 6 const int maxn = 1000;
 7 vector <int> maps[maxn];
 8 int n,m;
 9 int ok[maxn];//ok[i]代表第i个节点可否配对 
10 int matched[maxn];//matched[i]代表第i个节点的配对对象; 
11 bool match(int x){
12     for(int i=0;i<maps[x].size();i++){
13         int k = maps[x][i];
14         if(ok[k]==0){
15             ok[k]=1;
16             if(matched[k]==0||match(matched[k])==true){
17                 matched[k]=x;
18                 return true;
19             }
20         }
21     }
22     return false;
23 }
24 int ans = 1;
25 int color[1100];
26 void dfs(int x,int Color){
27     color[x]=Color;
28     for(int i=0;i<maps[x].size()&&ans;i++){
29         if(color[maps[x][i]]!=-1){
30             if(color[maps[x][i]]==Color){
31                 ans = 0;
32                 return;
33             }
34         }
35         else
36         dfs(maps[x][i],!Color);
37     }
38 }
39 int main(){
40     while(scanf("%d%d",&n,&m)!=EOF){
41         for(int i=1;i<=n;i++)
42             maps[i].clear();
43         memset(matched,0,sizeof(matched));
44         ans = 1;
45         for(int i=0;i<n;i++)
46             maps[i].clear();
47         memset(color,-1,sizeof(color));
48         for(int i=1;i<=m;i++){
49             int k,q;
50             scanf("%d%d",&k,&q);
51             maps[k].push_back(q);
52             maps[q].push_back(k);
53         }
54         ans = 1;
55         for(int i=1;i<=n&&ans;i++){
56             if(color[i]!=-1)
57                 continue;
58             dfs(i,0);
59         }
60         if(!ans){
61             printf("No\n");
62             continue;
63         }
64         ans = 0;
65         for(int i=1;i<=n;i++){
66             memset(ok,0,sizeof(ok));
67             if(color[i]!=0)
68                 continue;
69             if(match(i)==true)
70                 ans+=1;
71         }
72         cout<<ans<<endl;
73     }
74     return 0;
75 }

 

转载于:https://www.cnblogs.com/xfww/p/8542342.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值