Link:Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3829
Question:There are n dogs, M cats and P children in the zoo. Each child has a favorite animal and a nasty animal. Now the zoo wants to transfer some animals, if a child's favorite animal is absent, the child will be happy and ask the animal to make up to a few children happy.
Ideas:An obvious bipartite graph cannot use cats and dogs as vertices, so we can find out which animals will be transferred and take children as vertices to find the largest vertex independent set. There are two ways to create a graph, one is the number of vertices in the left and right vertices of the total number of children P. If a prefers an animal that B does not like, a side edge (A, B) is used ); the second method is to set the number of children who like cats to the left vertex and the right vertex to the dog Based on the conflicting edge. In this case, the number of vertices is different. Then find the biggest vertex independence set of the Bipartite Graph.
Maximum vertex independence set of a bipartite graph = number of vertices-maximum matching number
However, the number of vertices on the left and right of the first graph is P, that is, each child appears on the left and right, and appears twice in total. Therefore, dividing the largest vertex independent set by 2 is the answer.
# Include <cstring> # include <string> # include <fstream> # include <iostream> # include <iomanip> # include <cstdio> # include <cctype> # include <algorithm> # include <queue> # include <map> # include <set> # include <vector> # include <stack> # include <ctime> # include <cstdlib> # include <functional> # include <cmath> using namespace STD; # define PI ACOs (-1.0) # define maxn 500010 # define EPS 1e-7 # define INF 0x3f3f3f3f // 0x7fffffff # define llinf 0 X7fffffffffffff # define seed 1313131 # define mod 1000000007 # define ll long # define ull unsigned ll # define lson L, M, RT <1 # define rson m + 1, R, RT <1 | 1 struct node {int V, next;} edge [maxn]; int head [510], vis [510], DX [510], dy [510]; int CNT, N1, N2; // points on the left of N1, void add_edge (int A, int B) on the Right of N2 {edge [CNT]. V = B; edge [CNT]. next = head [a]; head [a] = CNT ++;} int path (int u) {int I, j; for (I = head [u]; I! =-1; I = edge [I]. Next) {int v = edge [I]. V; If (! Vis [v]) {vis [v] = 1; if (dy [v] =-1 | path (dy [v]) {DX [u] = V; dy [v] = u; return 1 ;}}return 0 ;}int maxmatch () {int I, j; int res = 0; memset (dx,-1, sizeof (dx); memset (dy,-1, sizeof (dy); for (I = 1; I <= N1; I ++) {If (dx [I] =-1) {memset (VIS, 0, sizeof (VIS); Res + = path (I );}} return res;} struct node {string like, dislike;} animal [505]; int main () {int N, M, P, I, J; while (scanf ("% d", & N, & M, & P )! = EOF) {CNT = 0; memset (Head,-1, sizeof (head); for (I = 0; I <p; I ++) {CIN> animal [I]. like> animal [I]. dislike;} n1 = P; for (I = 0; I <p; I ++) {for (j = 0; j <p; j ++) {If (animal [I]. like = animal [J]. dislike | animal [I]. dislike = animal [J]. like) {add_edge (I + 1, J + 1) ;}} int ans = maxmatch (); printf ("% d \ n", (2 * P-ans) /2);} return 0 ;}
HDU--3829 -- cat vs dog [maximum vertex independent set]