| Time Limit: 5000MS | Memory Limit: 10000K | |
| Total Submissions: 10912 | Accepted: 4887 |
Description
Input
Output
Sample Input
7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0
Sample Output
5
2
Source
#include <cstdio>
#include <cstring>
int const MAX = 505;
bool g[MAX][MAX];
bool vis[MAX];
int cx[MAX], cy[MAX];
int n;
int DFS(int x)
{
for(int y = 0; y < n; y++)
{
if(!vis[y] && g[x][y])
{
vis[y] = true;
if(cy[y] == -1 || DFS(cy[y]))
{
cy[y] = x;
cx[x] = y;
return 1;
}
}
}
return 0;
}
int MaxMatch()
{
memset(cx, -1, sizeof(cx));
memset(cy, -1, sizeof(cy));
int res = 0;
for(int i = 0; i < n; i++)
{
if(cx[i] == -1)
{
memset(vis, false, sizeof(vis));
res += DFS(i);
}
}
return res;
}
int main()
{
while(scanf("%d", &n) != EOF)
{
memset(g, false, sizeof(g));
for(int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
int num;
scanf(": (%d)", &num);
for(int j = 0; j < num; j++)
{
int y;
scanf("%d", &y);
g[x][y] = true;
}
}
int ans = MaxMatch();
printf("%d\n", n - ans / 2);
}
}POJ 1466 Girls and Boys (匈牙利算法 最大独立集)
原文:http://www.cnblogs.com/clnchanpin/p/7002552.html