Question:
P. Each person has a favorite and a nasty animal. Suppose the animals selected include the animals that this person prefers, but not the animals he hates at the same time. Then this person will be happy to ask a few people at most.
Ideas:
In a bipartite graph, the largest independent set uses the conflict between people to build the edge and find the maximum matching.
Note:
The example in the question shows that the animal names such as D1 and C1 may be longer than the actual name... So the array length
Code:
# Include <cstdio> # include <iostream> # include <cstring> # include <string> # include <algorithm> # include <map> # include <set> # include <vector> # include <queue> # include <cstdlib> # include <ctime> # include <cmath> using namespace std; typedef unsigned long LL; # define N 510 struct edge {int v, next;} ed [N * N]; int vis [N], match [N], head [N]; int n, tot; char like [N] [10], dislike [N] [10]; void add (int u, int v) {ed [tot]. v = v ; Ed [tot]. next = head [u]; head [u] = tot ++;} bool dfs (int u) {int I, v; for (I = head [u]; ~ I; I = ed [I]. next) {v = ed [I]. v; if (! Vis [v]) {vis [v] = 1; if (! Match [v] | dfs (match [v]) {match [v] = u; return true ;}}return false ;}int bimatch () {int I, sol = 0; memset (match, 0, sizeof (match); for (I = 1; I <= n; I ++) {memset (vis, 0, sizeof (vis); if (dfs (I) sol ++;} return sol;} int main () {int I, j, ans; while (~ Scanf ("% d", & I, & j, & n) {for (I = 1; I <= n; I ++) scanf ("% s", like [I], dislike [I]); memset (head,-1, sizeof (head); tot = 0; for (I = 1; I <= n; I ++) {for (j = I + 1; j <= n; j ++) {if (! Strcmp (like [I], dislike [j]) |! Strcmp (like [j], dislike [I]) {add (I, j); add (j, I) ;}} ans = n-bimatch () /2; printf ("% d \ n", ans);} return 0 ;}
HDU 3829 Cat VS Dog