Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1068
There are n classmates, the format ni: (m) N1 n2 N3 said classmate Ni destined and n1,n2,n3 become a couple, ask the collection does not exist in the first couple of classmates the biggest classmate number.
Independent set (a subset of the vertex sets of the graph, where any two points are not adjacent)
Maximum independent set = vertex number-Maximum match number in a binary graph
Because men and women do not know, to split a person into two gender, the maximum match, divided by 2 on the line.
This approach is more difficult to understand.
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intN = 1e3 +5;6 structEdge {7 intnext, to;8}edge[n *N];9 intHead[n], CNT;Ten intMatch[n]; One BOOLVis[n]; A - voidinit () { -Memset (Head,-1,sizeof(head)); thememset (Match,-1,sizeof(Match)); -memset (Vis,false,sizeof(Vis)); -CNT =0; - } + -InlinevoidAddintU,intv) { +Edge[cnt].next =Head[u]; AEdge[cnt].to =v; atHead[u] = cnt++; - } - - BOOLDfsintu) { - for(inti = Head[u]; ~i; i =Edge[i].next) { - intv =edge[i].to; in if(!Vis[v]) { -VIS[V] =true; to if(Match[v] = =-1||DFS (Match[v])) { +MATCH[V] =u; - return true; the } * } $ }Panax Notoginseng return false; - } the + intHungry (intN) { A intres =0; the for(inti =0; I < n; ++i) { +memset (Vis,false,sizeof(Vis)); - if(Dfs (i)) $res++; $ } - returnRes; - } the - intMain ()Wuyi { the intN; - while(~SCANF ("%d", &N)) { Wu init (); - intv; About for(inti =0; I < n; ++i) { $ intnum1, num; -scanf"%d: (%d)", &NUM1, &num); - while(num--) { -scanf"%d", &v); A Add (i, v); + } the } -printf"%d\n", N-hungry (n)/2); $ } the return 0; the}
HDU 1068 Girls and Boys (the largest independent set of two graphs)