Idea: It is clear that a certain child happy can be done, but at the same time it is possible to let other children unhappy, so each pair of such two children there is a "conflict", if there is "conflict" between the children to build a side, our problem is transformed into a binary map of the largest independent set. Specifically why is a binary chart, Bo Master also did not want to understand ...
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 6 Const intN =501;7 Const intM = N *N;8 Const intL = One;9 BOOLVisit[n];Ten intHead[n]; One intMark[n]; A intN, M, p, E; - - structEdge the { - intV, next; - } Edge[m]; - + structHobby - { + CharLike[l], dislike[l]; A } Hobby[n]; at - voidInit () - { -E =0; -Memset (Head,-1,sizeof(head)); - } in - voidAddedge (intUintv) to { +EDGE[E].V =v; -Edge[e].next =Head[u]; theHead[u] = e++; * } $ Panax Notoginseng intDfsintu) - { the for(inti = Head[u]; I! =-1; i =edge[i].next) + { A intv =edge[i].v; the if( !Visit[v]) + { -VISIT[V] =true; $ if(Mark[v] = =-1||DFS (Mark[v])) $ { -MARK[V] =u; -Mark[u] =v; the return 1; - }Wuyi } the } - return 0; Wu } - About inthunagry () $ { -memset (Mark,-1,sizeof(Mark)); - intres =p; - for(inti =1; I <= p; i++ ) A { + if(Mark[i]! =-1)Continue; thememset (Visit,0,sizeof(visit)); -Res-=DFS (i); $ } the returnRes; the } the the intMain () - { in while(SCANF ("%d%d%d", &n, &m, &p)! =EOF) the { the init (); About for(inti =1; I <= p; i++ ) the { thescanf"%s%s", Hobby[i].like, hobby[i].dislike); the for(intj = i-1; J >0; j-- ) + { - if(strcmp (hobby[i].like, hobby[j].dislike) = =0 the|| strcmp (hobby[j].like, hobby[i].dislike) = =0 )Bayi { the Addedge (i, j); the Addedge (J, i); - } - } the } theprintf"%d\n", Hunagry ()); the } the return 0; -}
HDU 3829 Maximum Independent set