EOJ 1047 Guardian of Decency
EOJ 1047 http://acm.cs.ecnu.edu.cn/problem.php?problemid=1047
POJ 2771 http://poj.org/problem?id=2771
二分匹配 不说了,直接上代码
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <vector> 7 #include <cmath> 8 using namespace std; 9 10 #define MAXN 505 11 struct node 12 { 13 int h; 14 char music[101], sport[101]; 15 }; 16 node boy[MAXN], girl[MAXN]; 17 int nb, ng; 18 int g[MAXN][MAXN]; 19 int match[MAXN]; 20 int f[MAXN]; 21 22 bool dfs(int u) 23 { 24 for (int i = 0; i < nb; i++) 25 if (g[u][i] && !f[i]) 26 { 27 f[i] = 1; 28 if (match[i] == -1 || dfs(match[i])) 29 { 30 match[i] = u; 31 return true; 32 } 33 } 34 return false; 35 } 36 37 int hungary() 38 { 39 int ans = 0; 40 for (int i = 0; i < ng; i++) 41 { 42 memset(f, 0, sizeof(f)); 43 if (dfs(i)) 44 ans++; 45 } 46 return ans; 47 } 48 49 void init() 50 { 51 memset(g, 0, sizeof(g)); 52 memset(match, -1, sizeof(match)); 53 ng = 0; 54 nb = 0; 55 } 56 57 int main() 58 { 59 int T, x, N; 60 char c, a[100], b[100]; 61 scanf("%d", &T); 62 while (T--) 63 { 64 init(); 65 scanf("%d", &N); 66 for (int i = 0; i < N; i++) 67 { 68 scanf("%d", &x); 69 getchar(); 70 c = getchar(); 71 scanf("%s%s", a, b); 72 if (c == 'M') 73 { 74 girl[ng].h = x; 75 strcpy(girl[ng].music, a); 76 strcpy(girl[ng].sport, b); 77 ng++; 78 } 79 else 80 { 81 boy[nb].h = x; 82 strcpy(boy[nb].music, a); 83 strcpy(boy[nb].sport, b); 84 nb++; 85 } 86 } 87 for (int i = 0; i < ng; i++) 88 for (int j = 0; j < nb; j++) 89 { 90 if (abs(girl[i].h - boy[j].h) > 40) 91 continue; 92 if (strcmp(girl[i].music, boy[j].music)) 93 continue; 94 if (strcmp(girl[i].sport, boy[j].sport) == 0) 95 continue; 96 g[i][j] = 1; 97 } 98 int ans = hungary(); 99 printf("%d\n", N - ans); 100 } 101 return 0; 102 }