hdu 2768 Cat vs. Dog 最大独立集
2016-10-11 22:27
417 查看
hdu 2768 Cat vs. Dog 最大独立集
Cat vs. Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 967 Accepted Submission(s): 356
Problem Description
The latest reality show has hit the TV: “Cat vs. Dog”. In this show, a bunch of cats and dogs compete for the very prestigious Best Pet Ever title. In each episode, the cats and dogs get to show themselves off, after which the viewers 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.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
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
Output
Per testcase:
One line with the maximum possible number of satisfied voters for the show.
Sample Input
2
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1
Sample Output
1
3
分别用两个集合装住喜欢猫的和喜欢狗的人,然后将有冲突的相连,直接求独立集
Cat vs. Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 967 Accepted Submission(s): 356
Problem Description
The latest reality show has hit the TV: “Cat vs. Dog”. In this show, a bunch of cats and dogs compete for the very prestigious Best Pet Ever title. In each episode, the cats and dogs get to show themselves off, after which the viewers 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.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
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' orD’, 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.
Output
Per testcase:
One line with the maximum possible number of satisfied voters for the show.
Sample Input
2
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1
Sample Output
1
3
分别用两个集合装住喜欢猫的和喜欢狗的人,然后将有冲突的相连,直接求独立集
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #define N 510 using namespace std; int vist ,link ; int edge ; int cnt_cat,cnt_dog; int c,d,v; struct node { char a[5],b[5]; }cat ,dog ; int find(int u) { int i; for(i=0; i<cnt_dog; i++) { if(!vist[i]&&edge[u][i]) { vist[i]=1; if(link[i]==-1||find(link[i])) { link[i]=u; return 1; } } } return 0; } /*bool find (int u) //匈牙利算法 { for (int i = 0; i < cnt_dog; i++) if (edge[u][i] && !vist[i]) { vist[i] = true; if (link[i] == -1 || find(link[i])) { link[i] = u; return true; } } return false; }*/ int solve() { int i; int ans=0; memset(link,-1,sizeof(link)); for(i=0; i<cnt_cat; i++) { memset(vist,0,sizeof(vist)); ans+=find(i); } return ans; } int main() { int cases; char a[5]; char b[5]; scanf("%d", &cases); while (cases--) { scanf("%d%d%d", &c, &d, &v); cnt_cat = 0; cnt_dog = 0; for (int i = 0; i < v; i++) { scanf("%s%s", a, b); if (a[0] == 'C') { strcpy(cat[cnt_cat].a, a); strcpy(cat[cnt_cat].b, b); cnt_cat++; } else { strcpy(dog[cnt_dog].a, a); strcpy(dog[cnt_dog].b, b); cnt_dog++; } } memset(edge, false, sizeof(edge)); for (int i = 0; i < cnt_cat; i++) for (int j = 0; j < cnt_dog; j++) if (strcmp(cat[i].a, dog[j].b) == 0 || strcmp(cat[i].b, dog[j].a) == 0) edge[i][j] = true; int ans = 0; memset(link, -1, sizeof(link)); for (int i = 0; i < cnt_cat; i++) { memset(vist, false, sizeof(vist)); if (find(i)) ans++; } printf("%d\n", v-ans); } return 0; }
相关文章推荐
- hdu 2768 Cat vs. Dog 最大独立集(最大匹配)
- 【HDU】2768 Cat vs. Dog 最大独立集
- HDU 2768 Cat vs Dog(最大独立集)
- hdu 2768 Cat vs. Dog (最大独立集)
- hdu_2768 Cat vs. Dog 最大独立集
- hdu 2768 Cat vs. Dog 最大独立集
- HDU 2768 Cat vs. Dog 【二分匹配之最大独立集】
- hdu 2768 Cat vs. Dog(最大独立集)
- HDU 2768 Cat vs. Dog 最大独立集+匈牙利算法(提高题)
- hdu 2768 Cat vs. Dog【最大独立集】
- hdu 2768 Cat vs. Dog (最大独立集)
- HDU--3829--Cat VS Dog【最大点独立集】
- hdu 3829 Cat VS Dog (二分图求最大独立集)
- hdu 3829 Cat VS Dog 【二分图求最大独立集】
- hdu 2768 Cat vs. Dog (最大独立)
- hdu 2768 Cat vs. Dog(二分图最大匹配--最大独立点集)
- HDU 3829——Cat VS Dog——————【最大独立集】
- hdu-2768-Cat vs. Dog(二分图-最大匹配数)
- hdu 3829 Cat VS Dog【最大独立集】
- hdu 3829 Cat VS Dog 【二分图求最大独立集】