Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
Output
Sample Input
6 3 3 1 1 1 2 1 3 2 1 2 3 3 1 0
Sample Output
3
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <cstdio> 6 using namespace std; 7 int k, m, n; 8 bool v[505]; 9 bool e[505][505]; 10 int pl[505]; 11 bool find(int x) 12 { 13 int i; 14 for (i = 1; i <= n; i++) 15 { 16 if (e[x][i] &&!v[i]) 17 { 18 v[i] = 1; 19 if (pl[i] == 0 || find(pl[i]))//没有伴侣或者能为伴侣找到其它伴侣 20 { 21 pl[i] = x; 22 return 1; 23 } 24 } 25 } 26 return 0; 27 } 28 int main() 29 { 30 while (cin >> k && k) 31 { 32 memset(e, 0, sizeof(e)); 33 memset(pl, 0, sizeof(pl)); 34 cin >> m >> n;//m个女生,n个男生 35 int i; 36 for (i = 1; i <= k; i++) 37 { 38 int x, y; 39 cin >> x >> y; 40 e[x][y] = 1; 41 } 42 int ans = 0; 43 for (i = 1; i <= m; i++) 44 { 45 memset(v, 0, sizeof(v)); //这个在每一步中清空 46 if (find(i)) ans++; 47 } 48 cout << ans << endl; 49 } 50 }