HDU2444 The Accomodation of Students(二分匹配 匈牙利算法)
来源:互联网 发布:java下载 编辑:程序博客网 时间:2024/05/01 11:26
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
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define N 205int a[N][N],vis[N],link[N];int n,m;bool bfs(){int i,j;queue<int>q;for(i=1;i<=n;i++){if(vis[i]!=-1) continue;vis[i]=1;q.push(i);while(!q.empty()){int cur=q.front();q.pop();for(j=1;j<=n;j++)if(a[cur][j]){if(vis[j]==vis[cur]) return false;if(vis[j]==-1){vis[j]=(vis[cur]+1)%2;q.push(j);}}}}return true;}bool dfs(int x){int i,j;for(i=1;i<=n;i++)if(a[x][i]&&!vis[i]){vis[i]=1;if(link[i]==0||dfs(link[i])){link[i]=x;return true;}}return false;}int main(){int i,j;while(~scanf("%d%d",&n,&m)){memset(a,0,sizeof(a)); int e,s; while(m--){scanf("%d%d",&s,&e);a[e][s]=a[s][e]=1;}memset(vis,-1,sizeof(vis));if(!bfs()){printf("No\n");continue;}int ans=0;memset(link,0,sizeof(link));for(i=1;i<=n;i++){memset(vis,0,sizeof(vis));if(dfs(i)) ans++;}printf("%d\n",ans/2);}return 0;}
0 0
- HDU2444 The Accomodation of Students(二分匹配 匈牙利算法)
- 二分图 匈牙利算法 HDU2444 The Accomodation of Students
- HDU2444 The Accomodation of Students(二分图匹配,匈牙利算法,DFS)
- hdu2444 二分匹配 The Accomodation of Students
- hdu2444 The Accomodation of Students(判断二分匹配+最大匹配)
- hdu2444——The Accomodation of Students(判断二分图+匈牙利算法)
- hdu2444 The Accomodation of Students (二分图判断+最大匹配)
- hdu2444 The Accomodation of Students(判联通+二分匹配)
- HDU2444 The Accomodation of Students 染色+二分匹配
- hdu2444 The Accomodation of Students【二分图判断+最大匹配】
- HDU2444--The Accomodation of Students
- HDU2444 The Accomodation of Students
- hdu2444 The Accomodation of Students
- hdu2444 The Accomodation of Students
- 【HDU2444】The Accomodation of Students
- The Accomodation of Students---hdu2444
- HDU2444-The Accomodation of Students
- HDU2444 The Accomodation of Students
- oracle 错误ORA-27300: OS system dependent operation:semctl failed with status: 22 ORA-00600: internal
- TCP序列号和确认号详解
- 创建Material Design风格的Android应用--应用主题
- 自制全铝CNC雕刻机全过程(完工篇)
- 一个国内的 ngrok 服务器
- HDU2444 The Accomodation of Students(二分匹配 匈牙利算法)
- Practical C Programming - Chapter 9
- 如何导入frm格式MYD格式MYI格式文件到MySQL中
- oracle 中的translate函数
- first Missing Positive number
- IOS缓存基本知识和清除缓存的方法
- mapreduce编程:求平均值
- QueryBuilder: A Simple Way to Read and Write Query Strings
- highCharts+ajax+json+ashx混合图形动态生成