hdu The Accomodation of Students(二分匹配)
来源:互联网 发布:jquery源码 编辑:程序博客网 时间:2024/05/29 16:27
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3136 Accepted Submission(s): 1466
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
Source
2008 Asia Harbin Regional Contest Online
题意:给你一些n个点,m条双向边,判断图是不是二分图,是的话求出最大匹配。
#include<cstring>#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>#include<vector>#define N 222using namespace std;int n,m;vector<int>G[N];int linker[N];bool used[N];int c[N];int maze[N][N];bool dfs(int u) { int v; for(v=0; v<G[u].size(); v++) { int i=G[u][v]; if(!used[i]) { used[i]=true; if(linker[i]==-1||dfs(linker[i])) { linker[i]=u; return true; } } } return false;}int hungary() { int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=1; u<=n; u++) { memset(used,0,sizeof(used)); if(dfs(u)) res++; } return res;}bool dfs_c(int pos,int col) { for(int i=0; i<G[pos].size(); i++) { int v=G[pos][i]; if(c[v]==c[pos]) return false; if(!c[v]) { c[v]=-col; if(!dfs_c(v,-col))return false; } } return true;}///染色法判断二分图bool color() { memset(c,0,sizeof c); int col=1; for(int i=1; i<=n; i++) { if(c[i]!=0)continue; c[i]=col; if(!dfs_c(i,col)) { return false; } } return true;}int main() { //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { for(int i=0; i<N; i++)G[i].clear(); int x,y; for(int i=0; i<m; i++) { scanf("%d%d",&x,&y); G[x].push_back(y); G[y].push_back(x); } if(!color()) { printf("No\n"); continue; } printf("%d\n", hungary()/2); } return 0;}
0 0
- hdu The Accomodation of Students(二分匹配)
- hdu 2444-The Accomodation of Students(二分匹配)
- hdu 2444 The Accomodation of Students 二分匹配
- hdu 2444 The Accomodation of Students 二分匹配
- hdu 2444 The Accomodation of Students (交叉染色+二分匹配)
- 【HDU】2444 The Accomodation of Students 二分匹配
- hdu 2444The Accomodation of Students 二分匹配+dfs分组
- 【二分匹配】 HDU 2444 The Accomodation of Students
- hdu 2444 The Accomodation of Students(二分匹配)
- HDU 2444 The Accomodation of Students 二分匹配
- hdu 2444 The Accomodation of Students 【二分图匹配】
- hdu2444 二分匹配 The Accomodation of Students
- The Accomodation of Students (二分匹配)
- hdu2444 The Accomodation of Students(判断二分匹配+最大匹配)
- HDU 2444 The Accomodation of Students 判断是否为二分图,二分图的最大匹配
- hdu 2444 The Accomodation of Students(是否二分图,找二分最大匹配数)
- hdu 2444 The Accomodation of Students (判断二分图,二分图最大匹配)
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
- ACM-ICPC Regionals 2013 >> Asia - Aizu - Count the Regions
- hdu 2007 平方和与立方和 (java)
- 3-38
- HDU 5214 Movie
- c++关于类型强转后作为引用参数的一些问题
- hdu The Accomodation of Students(二分匹配)
- ARM的异常处理过程分析
- [Practical.Vim(2012.9)].Drew.Neil.Tip14,15学习摘要
- ubuntu 15上 从windows 上拷贝 文件。
- P122.20
- Ural 1353 Milliard Vasya's Function
- JDBC_获取插入记录的主键值
- 数据结构详解
- python+Eclipse+pydev环境搭建