hdu2444The Accomodation of Students【判断二分图+最大匹配】
来源:互联网 发布:能听裂空小说软件 编辑:程序博客网 时间:2024/05/16 14:53
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
题意:这个题先问了最开始分成两批,组内互相不认识,问是否能做到;能做到,分成两个房间,房间中每对都是认识的,问最多有多少对。
做法:和刚刚做的题几乎一样,第一问认识的连边,求是否是二分图。第二问也是认识的连边,问最大匹配对数
代码写的好恶心==,那两个判断的函数是可以写在一起的 多WA了一次居然是因为No写成NO
#include <iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;vector<int>G[209];int col[209];bool flag[209][209];int n,m,x,y;const int MAXN=550;bool dfs(int u,int color){ col[u]=color; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(col[v]==color) return false; if(col[v]==-1) { if(!dfs(v,!color)) return false; } } return true;}bool solve(){ memset(col,-1,sizeof(col)); // puts("solve"); for(int i=1;i<=n;i++) { if(col[i]==-1) { if(!dfs(i,0))return false; } } return true;}///====================================================#define M 550#define inf 0x3f3f3f3fint uN,vN;//u,v数目int g[MAXN][MAXN];int linker[MAXN];bool used[MAXN];bool dfs(int u)//从左边开始找增广路径{ int v; for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改 if(g[u][v]&&!used[v]) { used[v]=true; if(linker[v]==-1||dfs(linker[v])) {//找增广路,反向 linker[v]=u; return true; } } return false;//这个不要忘了,经常忘记这句}int hungary(){ // puts("hungary"); int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=0;u<uN;u++) { memset(used,0,sizeof(used)); if(dfs(u)) res++; } return res;}///============================================================int main(){ // freopen("cin.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { // printf("%d%d ",n,m); memset(flag,0,sizeof(flag)); for(int i=1;i<=n;i++) G[i].clear(); memset(g,0,sizeof(g)); uN=vN=n; for(int i=0;i<m;i++) { scanf("%d%d",&x,&y); flag[x][y]=1; // flag[y][x]=1; G[x].push_back(y); G[y].push_back(x); g[x-1][y-1]=1; g[y-1][x-1]=1; } if(!solve()) { puts("No"); continue; } printf("%d\n",hungary()/2); } return 0;}
0 0
- hdu2444The Accomodation of Students【判断二分图+最大匹配】
- hdu2444The Accomodation of Students(二分图判断+最大匹配)
- HDU2444The Accomodation of Students(二分图判断+最大匹配)
- hdu2444The Accomodation of Students (最大匹配+判断是否为二分图)
- hdu2444 The Accomodation of Students (二分图判断+最大匹配)
- hdu2444 The Accomodation of Students【二分图判断+最大匹配】
- hdu2444 The Accomodation of Students(判断二分匹配+最大匹配)
- hdu2444The Accomodation of Students
- hdu2444The 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(二分图判断,二分图最大匹配)
- 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(二分图判断+求最大匹配)
- Spring声明式事务配置管理方法
- Java之基于S2SH与手机数据交互(一)
- $GLOBALS ["HTTP_RAW_POST_DATA"] 取不到值如何排除
- 如何判断js中的数据类型
- zzuli oj 1873: This offer 【dfs+数组标记技巧(set会超时)】
- hdu2444The Accomodation of Students【判断二分图+最大匹配】
- SharedPreferences
- python @ property classmethod staticmethod
- 创建 xlx java
- Java 将15位身份证号转化为18位返回,非15位身份证号原值返回
- javascript去除字符串左右两端的空格
- 215. Kth Largest Element in an Array
- mac下安装brew遇到的问题总结
- 类目和延展的作用和区别?