Cat vs. Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1648 Accepted Submission(s): 623
vote on which pets should stay and which should be forced to leave the show.
Each viewer gets to cast a vote on two things: one pet which should be kept on the show, and one pet which should be thrown out. Also, based on the universal fact that everyone is either a cat lover (i.e. a dog hater) or a dog lover (i.e. a cat hater), it has
been decided that each vote must name exactly one cat and exactly one dog.
Ingenious as they are, the producers have decided to use an advancement procedure which guarantees that as many viewers as possible will continue watching the show: the pets that get to stay will be chosen so as to maximize the number of viewers who get both
their opinions satisfied. Write a program to calculate this maximum number of viewers.
* One line with three integers c, d, v (1 ≤ c, d ≤ 100 and 0 ≤ v ≤ 500): the number of cats, dogs, and voters.
* v lines with two pet identifiers each. The first is the pet that this voter wants to keep, the second is the pet that this voter wants to throw out. A pet identifier starts with one of the characters `C' or `D', indicating whether the pet is a cat or dog,
respectively. The remaining part of the identifier is an integer giving the number of the pet (between 1 and c for cats, and between 1 and d for dogs). So for instance, ``D42'' indicates dog number 42.
* One line with the maximum possible number of satisfied voters for the show.
2 1 1 2 C1 D1 D1 C1 1 2 4 C1 D1 C1 D1 C1 D2 D2 C1
1 3
最大独立集,还是比较简单的
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 550; int head[N], mark[N]; bool vis[N]; char like[N][10], hate[N][10]; int tot; struct node { int to; int next; }edge[N * N]; void addedge(int from, int to) { edge[tot].to = to; edge[tot].next = head[from]; head[from] = tot++; } bool dfs(int u) { for (int i = head[u]; ~i; i = edge[i].next) { int v = edge[i].to; if (!vis[v]) { vis[v] = 1; if (mark[v] == -1 || dfs(mark[v])) { mark[v] = u; return 1; } } } return 0; } int hungry(int n) { memset (mark, -1, sizeof(mark)); int ans = 0; for (int i = 1; i <= n; ++i) { memset (vis, 0, sizeof(vis)); if (dfs(i)) { ans++; } } return n - ans / 2; } int main() { int t, x, y, n; scanf("%d", &t); while (t--) { memset (head, -1, sizeof(head)); tot = 0; scanf("%d%d%d", &x, &y, &n); for (int i = 1; i <= n; ++i) { scanf("%s%s", like[i], hate[i]); } for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { if (!strcmp(like[i], hate[j]) || !strcmp(hate[i], like[j])) { addedge(i, j); addedge(j, i); } } } printf("%d\n", hungry(n)); } }