HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
题意:
给出N个点,M条边,判断是否是二分图,如果是则输出最大匹配数;
题解:
DFS或者BFS判断是否为二分图;
匈牙利算法求最大匹配数;
AC代码:
#include<cstdio> #include<cstring> #include<vector> #include<queue> #define MAX 203 using namespace std; //匈牙利算法 - st int n,m; struct Edge{ int u,v; }; vector<Edge> E; vector<int> G[MAX]; int matching[MAX]; int vis[MAX]; void init(int l,int r) { E.clear(); for(int i=l;i<=r;i++) G[i].clear(); } void add_edge(int u,int v) { E.push_back((Edge){u,v}); E.push_back((Edge){v,u}); int _size=E.size(); G[u].push_back(E.size()-2); G[v].push_back(E.size()-1); } bool dfs(int u) { for(int i=0,_size=G[u].size();i<_size;i++) { int v=E[G[u][i]].v; if (!vis[v]) { vis[v]=1; if(!matching[v] || dfs(matching[v])) { matching[v]=u; matching[u]=v; return true; } } } return false; } int hungarian() { int ret=0; memset(matching,0,sizeof(matching)); for(int i=1;i<=n;i++) { if(!matching[i]) { memset(vis,0,sizeof(vis)); if(dfs(i)) ret++; } } return ret; } //匈牙利算法 - ed bool judge() { memset(vis,0,sizeof(vis)); queue<int> q; q.push(1); vis[1]=1; while(!q.empty()) { int now=q.front();q.pop(); for(int i=0,_size=G[now].size();i<_size;i++) { int nex=E[G[now][i]].v; if(!vis[nex]) { q.push(nex); vis[nex]=-vis[now]; } if(vis[nex]==vis[now]) return false;//不是二分图 } } return true; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(1,n); for(int i=1,u,v;i<=m;i++) { scanf("%d%d",&u,&v); add_edge(u,v); add_edge(v,u); } if(!judge()) printf("No\n"); else printf("%d\n",hungarian()); } }
PS.这里因为懒得枚举U集里的点(因为这要先得到U集里的点都是哪些),所以直接枚举所有点;
转载请注明出处:https://dilthey.cnblogs.com/