匈牙利算法求出最大匹配
最大独立集= n - 最大匹配
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
bool vis[555];
int mp[555][555];
int match[555];
int n;
bool dfs(int x){
for(int i = 0 ; i < n ; i ++){
if(mp[x][i] == 1 && vis[i] == 0){
vis[i] = 1;
int temp = match[i];
match[i] = x;
if(temp == -1 || dfs(temp))
return true;
match[i] = temp;
}
}
return false;
}
void init(){
memset(vis,0,sizeof(vis));
memset(mp,0,sizeof(mp));
memset(match,-1,sizeof(match));
}
int main()
{
while(scanf("%d",&n) != EOF){
init();
for(int i = 0 ; i < n ; i ++){
int a,num;
scanf("%d:",&a);
scanf(" (%d)",&num);
while(num--){
int b;
scanf("%d",&b);
mp[a][b] = mp[b][a] = 1;
}
}
int path = 0;
for(int i = 0 ; i < n ; i ++){
memset(vis,0,sizeof(vis));
if(dfs(i))
path++;
}
printf("%d\n",n-path/2);
}
return 0;
}