Question:
P. Each person has a favorite and a nasty animal. If the selected animal contains a person's favorite animal and does not contain the animal he hates, the person will be happy to ask at most a few people to be happy.
Ideas:
The maximum independence set in the bipartite graph 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 this one... 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 long LL;#define N 510struct 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%d%d", &i, &j, &n)) {for (i = 1; i <= n; i++)scanf("%s%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