题意:有一个集合男和一个集合女,给出两集合间一些一一对应关系,问该两集合中的最大独立集的点数。
最大独立集=顶点总数-最大匹配数
此题中,若(a,b)有关,则(b,a)有关,每一个关系算了两次,相当于二分图的两边集合没有分男女,两边都是总人数,所以此题中答案应该是: 顶点总数-最大匹配数/2
链接 :hdu 1068
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2555;
int used[maxn];
int link[maxn];
int mat[maxn][maxn];
int gn, gm;
int dfs(int t) {
for(int i = 0; i < gm; i++) {
if(!used[i] && mat[t][i]) {
used[i] = 1;
if(link[i] == -1 || dfs(link[i])) {
link[i] = t;
return 1;
}
}
}
return 0;
}
int maxmatch() {
int num = 0;
memset(link, 0xff, sizeof(link));
for(int i = 0; i < gn; i++) {
memset(used, 0, sizeof(used));
if(dfs(i)) {
num++;
}
}
return num;
}
int main()
{
int n, m;
while(~scanf("%d", &n)){
memset(mat, 0, sizeof(mat));
gn = n;
gm = n;
int a;
for(int i = 0; i < n; i++) {
scanf("%d: (%d)", &a, &m);
int k;
for(int j = 0; j < m; j++) {
scanf("%d", &k);
mat[a][k] = 1;
}
}
int res = maxmatch();
printf("%d\n", n - res / 2);
}
return 0;
}