HDU2444(二分图判定+最大匹配)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU2444(二分图判定+最大匹配)相关的知识,希望对你有一定的参考价值。
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4250 Accepted Submission(s): 1946
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 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
题意:一些学生存在认识与不认识的关系,但这种关系不传递。问能否将学生分为两组,每组中的学生互相不认识(即二分图判定)。若可以,把相互认识的同学放在一个双人间,问最多需要几个双人间(即二分图最大匹配)。
#include"cstdio" #include"cstring" #include"vector" #include"algorithm" using namespace std; const int MAXN=205; vector<int> G[MAXN]; int V,E; bool bfs(int s)//二分图判断 { int que[MAXN],rear=0,front=0; int color[MAXN]; memset(color,-1,sizeof(color)); color[s]=1;que[rear++]=s; while(rear!=front) { int v=que[front++]; for(int i=0;i<G[v].size();i++) { int u=G[v][i]; if(color[u]==-1) { color[u]=!color[v];//若没有染色则染成相反颜色 que[rear++]=u; } else if(color[u]==color[v]) return false;//若相邻结点颜色相同则不是二分图 } } return true; } int match[MAXN]; int vis[MAXN]; bool dfs(int u)//二分图匹配 { vis[u]=1; for(int i=0;i<G[u].size();i++) { int v=G[u][i],w=match[v]; if(w==0||(!vis[w]&&dfs(w))) { match[u]=v; match[v]=u; return true; } } return false; } int matching() { memset(match,0,sizeof(match)); int ans=0; for(int i=1;i<=V;i++) { if(!match[i]) { memset(vis,0,sizeof(vis)); if(dfs(i)) ans++; } } return ans; } int main() { while(scanf("%d%d",&V,&E)!=EOF) { for(int i=1;i<=V;i++) G[i].clear(); for(int i=0;i<E;i++) { int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } if(bfs(1)) printf("%d\n",matching()); else printf("No\n"); } return 0; }
以上是关于HDU2444(二分图判定+最大匹配)的主要内容,如果未能解决你的问题,请参考以下文章
HDU2444 The Accomodation of Students —— 二分图最大匹配
hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)