hdu 2444 是否是二分图+二分图最大匹配数
来源:互联网 发布:大管家收银软件 编辑:程序博客网 时间:2024/05/16 08:13
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.
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.
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 41 21 31 42 36 51 21 31 42 53 6
Sample Output
No3
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<vector>#include<string>#include<algorithm>#define LL long long#define inf 0x3f3f3f3fusing namespace std;int color[250];vector<int> g[250];bool visited[250];int linker[250];int n;bool col(int u){ for(int i=0;i<g[u].size();i++) { if(color[g[u][i]]==-1) { color[g[u][i]]=!color[u]; if(!col(g[u][i])) return false; } else if(color[g[u][i]]==color[u]) return false; } return true;}bool dfs(int u){ for(int i=0;i<g[u].size();i++) if(!visited[g[u][i]]) { visited[g[u][i]]=1; if(linker[g[u][i]]==-1||dfs(linker[g[u][i]])) { linker[g[u][i]]=u; return true; } } return false;}int hungary(){ int num=0; memset(linker,-1,sizeof(linker)); for(int u=1;u<=n;u++) { memset(visited,0,sizeof(visited)); if(dfs(u)) num++; } return num;}int main(){ while(cin>>n) { int m; cin>>m; for(int i=0;i<=210;i++) g[i].clear(); memset(visited,0,sizeof(visited)); while(m--) { int a,b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); } memset(color,-1,sizeof(color)); color[1]=1; if(!col(1)) cout<<"No"<<endl; else cout<<hungary()/2<<endl; } return 0;}
判断是否是二分图:
bool col(int u)//判断是否是二分图{ for(int i=0;i<g[u].size();i++) { if(color[g[u][i]]==-1) { color[g[u][i]]=!color[u]; if(!col(g[u][i])) return false; } else if(color[g[u][i]]==color[u]) return false; } return true;}
color[1]=1; if(!col(1))//返回false则不是二分图 cout<<"No"<<endl;
0 0
- hdu 2444 是否是二分图+二分图最大匹配数
- hdu 2444 The Accomodation of Students(是否二分图,找二分最大匹配数)
- HDU 2444 判断是否是二分图以及二分最大匹配
- hdu 2444 The Accomodation of Students (判断是否是二分图,最大匹配)
- HDU 2444 The Accomodation of Students(判断是否是二分图及求最大匹配)
- HDU-2444 二分图的判别和最大匹配数。
- HDU 2444 The Accomodation of Students 判断是否为二分图,二分图的最大匹配
- hdu 2444 The Accomodation of Students【二分图染色+最大二分匹配数】
- hdu 2444 二分图判定+最大匹配
- hdu 2444 (判断二分图+最大匹配)
- HDU 2444 判断二分图+最大匹配
- hdu 2444(二分图判断+最大匹配)
- HDU 2444 (二分图判定+最大匹配)
- HDU 2444 二分图判断+最大匹配
- HDU - 2444 二分图判定 + 最大匹配
- hdu 2444 二分图判定+求二分图最大匹配
- HDU 2444 黑白染色判二分图+二分最大匹配
- hdu 2444 交叉染色判断二分图+二分最大匹配
- POJ 2226 Muddy Fields(最小点覆盖)
- 奇数-函数实现
- 静悄悄地生活
- main()是程序运行的第一个函数吗?
- c++第五次实验
- hdu 2444 是否是二分图+二分图最大匹配数
- Universal-Image-Loader中的线程模型
- Web框架性能基准测试 (Round 12) techempower的第12轮Web框架性能测试
- iOS延迟关闭
- hdu 2594 Simpsons’ Hidden Talents
- 重写和重载的区别
- HS9148红外遥控解码
- eth 和 em的区别
- 【接口测试】接口测试的自我修养