http://acm.hdu.edu.cn/showproblem.php?pid=1054
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct arcnode
{
int adjvex;
arcnode *next;
arcnode():adjvex(-1),next(0){}
};
struct vnode
{
arcnode *first;
vnode():first(0){}
};
vnode v[1505];
void add_edge(int i,int adjvex)
{
arcnode *t=new arcnode;
t->adjvex=adjvex;
t->next=v[i].first;
v[i].first=t;
}
int vis[1505];
int link1[1505];
int dfs(int i)
{
arcnode *p=v[i].first;
for(;p;p=p->next)
{
if(!vis[p->adjvex])
{
vis[p->adjvex]=1;
if(link1[p->adjvex]==-1||dfs(link1[p->adjvex]))
{
link1[p->adjvex]=i;
return 1;
}
}
}
return 0;
}
void freenode(int n)
{
arcnode *p,*q;
int i=0;
for(i=0;i<n;i++)
{
p=v[i].first;
while(p)
{
q=p;
p=p->next;
delete q;
}
v[i].first=0;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i;
int t1,j,t2,k;
for(i=0;i<n;i++)
{
scanf("%d:(%d)",&t1,&k);
for(j=0;j<k;j++)
{
scanf("%d",&t2);
add_edge(t1,t2);
add_edge(t2,t1);
}
}
memset(link1,-1,sizeof(link1));
int s=0;
for(i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
s++;
}
printf("%d\n",s/2);
freenode(n);
}
}