http://acm.hdu.edu.cn/showproblem.php?pid=1054
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int N=1510,M=3010; 6 struct edge 7 { 8 int v,next; 9 }e[M]; 10 int head[N],n,m; 11 bool vis[N]; 12 int mat[N]; 13 void graphinit() 14 { 15 memset(head,-1,sizeof(head)); 16 m=0; 17 } 18 void addedge(int u,int v) 19 { 20 edge et={v,head[u]}; 21 e[m]=et; 22 head[u]=m++; 23 } 24 bool read_graph() 25 { 26 graphinit(); 27 if(scanf("%d",&n)==EOF) return false; 28 for(int i=0;i<n;i++) 29 { 30 int u,c; 31 scanf("%d:(%d)",&u,&c); 32 while(c--) 33 { 34 int v; 35 scanf("%d",&v); 36 addedge(u,v); 37 addedge(v,u); 38 } 39 } 40 return true; 41 } 42 bool find(int u) 43 { 44 for(int i=head[u];i!=-1;i=e[i].next) 45 { 46 int v=e[i].v; 47 if(vis[v]) continue; 48 vis[v]=true; 49 if(mat[v]==-1 || find(mat[v])) 50 { 51 mat[v]=u; 52 return true; 53 } 54 } 55 return false; 56 } 57 int maxmatch() 58 { 59 int cnt=0; 60 memset(mat,-1,sizeof(mat)); 61 for(int i=0;i<n;i++) 62 { 63 memset(vis,0,sizeof(vis)); 64 if(find(i)) cnt++; 65 } 66 return cnt; 67 } 68 int main() 69 { 70 while(read_graph()) 71 { 72 int ans=maxmatch()/2; 73 printf("%d\n",ans); 74 } 75 return 0; 76 }