标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3003 Accepted Submission(s): 1407
/** 题意:n个人中有m组数据,表示两个人彼此熟悉,然后看怎么分可以使得现在的 组内的人是彼此互不了解的 做法:二分图匈牙利算法 **/ #include<iostream> #include<cmath> #include<algorithm> #include<stdio.h> #include<string.h> #include<queue>> using namespace std; #define maxn 210 int g[maxn][maxn]; int linker[maxn]; bool used[maxn]; int un,vn; int n; int color[maxn]; int bfs() { queue<int>que; memset(color,-1,sizeof(color)); que.push(1); color[1] = 0; while(!que.empty()) { int tt = que.front(); que.pop(); int mm = 1 - color[tt]; for(int i=1;i<=n;i++) { if(g[tt][i]) { if(color[i] == -1) { color[i] = mm; que.push(i); } else { if(color[i] == color[tt]) return 0; } } } } return 1; } bool dfs(int u) { for(int v = 1; v <= vn; v++) { if(g[u][v] && used[v] == false) { used[v] = true; if(linker[v] == -1 || dfs(linker[v])) { linker[v] = u; return true; } } } return false; } int hungary() { int res = 0; memset(linker,-1,sizeof(linker)); for(int i=1; i<=un; i++) { memset(used,false,sizeof(used)); res += dfs(i); } return res; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE int m; while(~scanf("%d %d",&n,&m)) { memset(g,0,sizeof(g)); int u,v; for(int i=0; i<m; i++) { scanf("%d %d",&u,&v); g[u][v] = 1; g[v][u] = 1; } un = vn = n; if(!bfs()) printf("No\n"); else printf("%d\n",hungary()/2); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4392382.html