The Accomodation of Students(二分图判断+匈牙利算法)

描述

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.

输入

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.

输出

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.

样例输入

4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6

样例输出

No
3

 

1.先要判断能否分成两组,使得每组内的人互不认识,即判断是否为二分图

2.不是二分图输出No,否则就计算两两匹配的对数

判断二分图:采用染色法+bfs(染色法是将一个点先染色,然后把和它相邻的点染成不同的颜色,如果遇到相邻点的颜色相同的情况就不是二分图)

匹配的对数:由于是两个相同的集合进行配对,所以最后将结果除2

 

这题的来源是hdoj,很多的题解上对于bfs那部分是只做了一次,即默认从1开始可以搜索到所有出现的点,但是这显然是不全面的,还是可能会存在有单独的集合(比如在题目样例2在加上一组:7 8),虽然那样在hdoj也可以ac,但是我觉得还是多次bfs更加合理

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 
 7 const int N=205;
 8 int n,m;
 9 int maps[N][N],num[N],vis[N],flag[N],par[N];
10 queue<int> que;
11 
12 int bfs()//多次bfs
13 {
14     memset(vis,0,sizeof(vis));//初始都未访问
15     memset(num,-1,sizeof(num));//染色数组,-1为未染,0,1则为两种不同颜色
16     for(int i=1;i<=n;i++){
17         if(!vis[i]){
18             vis[i]=1;
19             num[i]=0;
20             que.push(i);
21             while(!que.empty()){
22                 int k=que.front();
23                 que.pop();
24                 for(int j=1;j<=n;j++){
25                     if(maps[k][j]){
26                         if(num[j]==-1){
27                             num[j]=1-num[k];
28                             que.push(j);
29                             vis[j]=1;
30                         }
31                         else if(num[k]==num[j]){
32                             return 0;
33                         }
34                     }
35                 }
36             }
37         }
38     }
39     return 1;
40 }
41 
42 int find(int x)//匈牙利算法
43 {
44     for(int i=1;i<=n;i++){
45         if(maps[x][i]==1&&!flag[i]){
46             flag[i]=1;
47             if(!par[i]||find(par[i])){
48                 par[i]=x;
49                 return 1;
50             }
51         }
52     }
53     return 0;
54 }
55 
56 int main()
57 {
58     int x,y;
59     while(scanf("%d%d",&n,&m)!=EOF){
60         memset(maps,0,sizeof(maps));
61         for(int i=0;i<m;i++){
62             scanf("%d%d",&x,&y);
63             maps[x][y]=maps[y][x]=1;//关系是互相的
64         }
65         if(!bfs()) printf("No\n");
66         else{
67             int sum=0;
68             memset(par,0,sizeof(par));
69             for(int i=1;i<=n;i++){
70                 memset(flag,0,sizeof(flag));
71                 sum+=find(i);
72             }
73             printf("%d\n",sum/2);//两个相同集合配对结果除2
74         }
75         while(!que.empty()){
76             que.pop();
77         }
78     }
79     return 0;
80 }

 

转载于:https://www.cnblogs.com/ChangeG1824/p/9511294.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值