http://acm.hdu.edu.cn/showproblem.php?pid=1068
题意:给你一系列的浪漫关系,求其中不在浪漫关系中人的学生数。
思路:最大点独立集。最大点独立集=节点数-最大匹配数。注意“The relation “romantically involved” is defined between one girl and one boy.”,虽然样例中输入的都是无向图,不过这句本身就表示关系是双方的,所以针对无向图匹配数除以2。再没什么坑了。
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long LL;
const int N = 505;
const int INF = 0x3f3f3f3f;
int head[N], match[N];
bool vis[N];
int n, m, cnt;
struct Edge
{
int to,next;
}edge[N*N];
void add(int u, int v)
{
edge[cnt] = (struct Edge){v, head[u]};
head[u] = cnt++;
}
void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
}
bool Augment(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!vis[v])
{
vis[v] = true;
if(match[v]==-1 || Augment(match[v]))
{
match[v] = u;
return true;
}
}
}
return false;
}
int Hungary(int n)
{
int ans = 0;
memset(match, -1, sizeof(match));
for(int i = 0; i < n; i++)
{
memset(vis, false, sizeof(vis));
if(Augment(i)) ans++;
}
return ans;
}
int main()
{
// freopen("in.txt", "r", stdin);
int n, u, v, k;
while(~scanf("%d", &n))
{
init();
for(int i = 0; i < n; i++)
{
scanf("%d: (%d) ", &u, &k);
while(k--)
{
scanf("%d", &v);
add(u, v);
}
}
printf("%d\n", n-(Hungary(n)>>1));
}
return 0;
}