Problem : Girls and Boys
Description : 有N个人,这些人中有些人之间有暧昧关系,现在要选出这样的一群人,他们两两之间都没有暧昧关系。
Solution : 二分图的最大独立集。很典型的题目,无需转化,看代码。、
Code(C++) :
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int F=600+5;
int n;
vector<int> edge[F];
bool used[F];
int belong[F];
bool dfs(int s)
{
for(int i=0;i<edge[s].size();i++){
int end=edge[s].at(i);
if(used[end])
continue;
used[end]=true;
if(belong[end]==-1||dfs(belong[end])){
belong[end]=s;
return true;
}
}
return false;
}
int main()
{
//freopen("in.data","r",stdin);
while(~scanf("%d",&n)){
for(int i=0;i<F;i++)
edge[i].clear();
memset(belong,-1,sizeof(belong));
int x,y,m;
for(int i=0;i<n;i++){
scanf("%d: (%d)",&x,&m);
++x;
while(m--){
scanf("%d",&y);
++y;
edge[x].push_back(y);
}
}
int sum=0;
for(int i=1;i<=n;i++){
memset(used,false,sizeof(used));
if(dfs(i))
++sum;
}
printf("%d\n",n-sum/2);
}
return 0;
}