POJ 1466 Girls and Boys
题目链接:POJ 1466
题意:
求最大集合:集合里任何两个学生都没有发生过浪漫关系。
分析:
答案等于n-最大匹配数/2 因为成对匹配的,只需要求出有多少对就行了
构图,连边。然后匈牙利算法求解。
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int n;
// DFS
const int maxn=512;
bool used[maxn];
vector<int>g[maxn];
int L,R;
int x[maxn],y[maxn];
bool SearchPath(int u)
{
int Size=g[u].size(),v;
for(int i=0;i<Size;i++)
{
v=g[u][i];
if(!used[v])
{
used[v] = true;
if(y[v] == -1 || SearchPath(y[v]))
{
y[v]=u;
x[u]=v;
return true;
}
}
}
return false;
}
int MaxMatch()
{
int ret=0;
memset(x,-1,sizeof(x));
memset(y,-1,sizeof(y));
for(int i=0;i<L;i++)
{
if(x[i]==-1)
{
memset(used,false,sizeof(used));
if(SearchPath(i))
ret++;
}
}
return ret;
}
int main()
{
// freopen("in.txt","r",stdin);
int num,u,v;
while(scanf("%d",&n)!=EOF)
{
memset(g,0,sizeof(g));
for(int i=0;i<n;i++)
{
g[i].clear();
scanf("%d: (%d)",&u,&num);
for(int j=0;j<num;j++)
{
scanf("%d",&v);
g[u].push_back(v);
}
}
L=n;
printf("%d\n",n-MaxMatch()/2);
}
return 0;
}