There are m girls and N boys. If boys and girls have a crush on each other, they will be connected and asked how many pairs can be matched.
This blog post is well written and does not contain daunting graph theory terms.
Http://blog.csdn.net/dark_scope/article/details/8880547
The core idea is the word "Teng". It doesn't matter if you don't have a sister. Let the old ones change their favorite sister to see if they can "Teng" their favorite sister.
1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 505; 8 int line[maxn][maxn], used[maxn], girl[maxn]; 9 int k, m, n;10 11 bool find(int x)12 {13 for(int j = 1; j <= m; ++j)14 {15 if(line[x][j] && !used[j])16 {17 used[j] = 1;18 if(girl[j] == 0 || find(girl[j]))19 {20 girl[j] = x;21 return true;22 }23 }24 }25 return false;26 }27 28 int main(void)29 {30 #ifdef LOCAL31 freopen("2063in.txt", "r", stdin);32 #endif33 34 while(scanf("%d", &k) == 1 && k)35 {36 scanf("%d%d", &n, &m);37 memset(line, 0, sizeof(line));38 memset(girl, 0, sizeof(girl));39 for(int i = 0; i < k; ++i)40 {41 int a, b;42 scanf("%d%d", &a, &b);43 line[a][b] = 1;44 }45 int match = 0;46 for(int i = 1; i <= n; ++i)47 {48 memset(used, 0, sizeof(used));49 if(find(i)) ++match;50 }51 printf("%d\n", match);52 }53 return 0;54 }
Code Jun
HDU 2063 (Hungarian algorithm) roller coaster