我觉得有必要粘一下英文:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2502 Accepted Submission(s): 1190
1 #include <iostream> 2 #include <set> 3 #include <cstdio> 4 #include <cstring> 5 #include <vector> 6 using namespace std; 7 8 const int maxn = 205; 9 const int INF = 1000000000; 10 int n; 11 12 int col[maxn]; 13 vector<int> G[maxn]; 14 bool is_bi(int u) { 15 for(int i = 0; i < G[u].size(); i++) { 16 int v = G[u][i]; 17 if(col[u] == col[v]) return false; 18 if(!col[v]) { 19 col[v] = 3 - col[u]; 20 if(!is_bi(v)) return false; 21 } 22 } 23 return true; 24 } 25 bool check() { 26 for(int i = 1; i <= n; i++) { 27 if(G[i].size()) { 28 memset(col, 0, sizeof(col)); 29 col[i] = 1; 30 if(!is_bi(i)) return false; 31 } 32 } 33 return true; 34 } 35 36 int vis[maxn]; 37 int Left[maxn]; 38 int Link[maxn]; 39 vector<int> V[maxn]; 40 bool Find(int u) { 41 for(int i = 0; i < V[u].size(); i++) { 42 int v = V[u][i]; 43 if(!vis[v]) { 44 vis[v] = 1; 45 if(Link[v] == -1 || Find(Link[v])) { 46 Link[v] = u; 47 return true; 48 } 49 } 50 } 51 return false; 52 } 53 int solve() { 54 int cnt = 0; 55 memset(Link, -1, sizeof(Link)); 56 for(int i = 1; i <= n; i++) { 57 if(V[i].size()) { 58 memset(vis, 0, sizeof(vis)); 59 if(Find(i)) cnt++; 60 } 61 } 62 return cnt; 63 } 64 65 int main() { 66 int m; 67 int u, v; 68 while(EOF != scanf("%d %d",&n, &m)) { 69 for(int i = 0; i <= n; i++) { 70 G[i].clear(); 71 V[i].clear(); 72 } 73 while(m--) { 74 scanf("%d %d",&u, &v); 75 G[u].push_back(v); 76 G[v].push_back(u); 77 V[u].push_back(v); 78 } 79 if(!check()) { 80 puts("No"); 81 continue; 82 } 83 printf("%d\n",solve()) ; 84 } 85 return 0; 86 }
hdu2444The Accomodation of Students【判断二分图+最大匹配】,布布扣,bubuko.com
hdu2444The Accomodation of Students【判断二分图+最大匹配】
原文:http://www.cnblogs.com/zhanzhao/p/3908291.html