标签:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; #define maxn 500 bool G[maxn][maxn], vis[maxn]; int color[maxn], P[maxn];///黑白染色 -1 黑色 1 白色 int n, m; bool DFS(int u,int c)///判断是否是二分图,黑白染色 { color[u] = c; for(int i=1; i<=n; i++) { if(!G[u][i] ) continue; if(color[i] == 0) { if( DFS(i, -c) ) continue; return false; } else if(color[i] + color[u]) return false; } return true; } bool Find(int u) { for(int i=1; i<=n; i++) { if(G[u][i] && !vis[i]) { vis[i] = true; if(P[i] == -1 || Find(P[i])) { P[i] = u; return true; } } } return false; } int solve() { bool ok; for(int i=1; i<=n; i++) { if(color[i] == 0) ok = DFS(i, 1); if(ok == false) return -1; } int ans = 0; for(int i=1; i<=n; i++) { memset(vis, false, sizeof(vis)); if(color[i] && Find(i)) ans ++; } return ans; } int main() { while(scanf("%d %d",&n, &m) != EOF) { int a, b; memset(G, false, sizeof(G)); memset(P, -1, sizeof(P)); memset(color, 0, sizeof(color)); for(int i=0; i<m; i++) { scanf("%d %d",&a, &b); G[a][b] = true; } int ans = solve(); if(ans == -1) puts("No"); else printf("%d\n", ans ); } return 0; }
HDU 2444 The Accomodation of Students(判断是否可图 + 二分图)
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4718575.html