题目
大学第二年,有人开始研究学生之间的恋爱关系。恋爱关系是指一个女孩和一个男孩之间的关系。由于研究的原因,有必要找出满足条件的最大集合:集合中没有两个学生“恋爱过”。程序的结果是这样一个集合中的学生数量。
输入包含几个文本格式的数据集。每个数据集代表一组研究对象,描述如下:
学生人数
每个学生的描述,格式如下
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3…
或
student_identifier:(0)
student_identifier是一个介于0和n-1之间的整数,用于n个主题。
对于每个给定的数据集,程序应该向标准输出写入一行包含结果的代码。
Sample Input
7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0
Sample Output
5
2
无向图独立集: ans=n-最大匹配。
#include <cstdio>
#include <cstring>
#define m(a,b) memset(a,b,sizeof a)
const int N=1000+5;
const int M=2500000+5;
const int INF=0x3f3f3f3f;
using namespace std;
typedef long long ll;
struct Edge{int to,nex;}edge[M];
int tot;
bool vis[N];
int mt[N],head[N];
bool find_path(int x)
{
for(int i=head[x];i;i=edge[i].nex)
{
int y=edge[i].to;
if(!vis[y])
{
vis[y]=1;
if(!mt[y]||find_path(mt[y]))
{
mt[y]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
m(head,0);
m(mt,0);
tot=1;
for(int i=1;i<=n;i++)
{
int a,num,b;
scanf("%d: (%d)",&a,&num);
a++;
while(num--)
{
scanf("%d",&b);
b++;
edge[++tot]=(Edge){b,head[a]};head[a]=tot;
}
}
int ans=0;
for(int i=1;i<=n;i++)
{
m(vis,0);
if(find_path(i))
ans++;
}
printf("%d\n",n-ans/2);
}
}