HDU 2444 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. 

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
题解:
题目是二分图模板题,判断是否是二分图的方法是:对于两部分的点,不存在一条边两个点在同一个集合里;BFS搜索即可,对于一条变的两个点大不同的标记,如果有矛盾,则不是二分图,否则是:
参考代码:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 210;
 4 int g[maxn][maxn];
 5 int match[maxn],vis[maxn];
 6 int col[maxn];
 7 
 8 bool bfs(int s,int n)
 9 {
10     queue<int> q;
11     q.push(s);
12     col[s] = -1;
13     while(!q.empty())
14     {
15         int u = q.front(); q.pop();
16         for(int i=1;i<=n;i++)
17         {
18             if(g[u][i])
19             {
20                 if(col[i]==col[u]) return false;
21                 else if(col[i]==0)
22                 {
23                     col[i] = -col[u];
24                     q.push(i);
25                 }
26             }
27         }
28     }
29     return true;
30 }
31 
32 bool judge(int n)
33 {
34     memset(col,0,sizeof(col));
35     for(int i=1;i<=n;i++)
36     {
37         if(col[i]==0)
38             if(!bfs(i,n)) return false;
39     }
40     return true;
41 }
42 
43 bool dfs(int u,int n)
44 {
45     for(int i=1;i<=n;i++)
46     {
47         if(vis[i] || !g[u][i]) continue;
48         vis[i] = 1;
49         if(!match[i] || dfs(match[i],n))
50         {
51             match[i] = u; match[u] = i;
52             return true;
53         }
54     }
55     return false;
56 }
57 
58 int main(void)
59 {
60     int n,m;
61     while(~scanf("%d %d",&n,&m))
62     {
63         memset(match,0,sizeof(match));
64         memset(g,0,sizeof(g));
65         for(int i=0;i<m;i++)
66         {
67             int x,y;
68             scanf("%d%d",&x,&y);
69             g[x][y]=1;
70         }
71         if(!judge(n)) puts("No");
72         else
73         {
74             int ans=0;
75             for(int i=1;i<=n;i++)
76             {
77                 memset(vis,0,sizeof(vis));
78                 if(dfs(i,n)) ans++;
79             }
80             printf("%d\n",ans);
81         }
82     }
83     return 0;
84 }
View Code

 

转载于:https://www.cnblogs.com/songorz/p/9520559.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值