(Hdu step 6.3.7) Cat vs. Dog (when construction party rules: build edges when audiences clash with other viewers ' favorite things, seeking maximum independent set)

Source: Internet
Author: User

Title:

Cat vs. Dog
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 219 Accepted Submission (s): 86
Problem DescriptionThe Latest reality show had 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 is forced to leave the show.

Each viewer gets to cast a vote on Things:one pet which should is kept on the show, and one pet which should is throw N 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 the vote must name exactly one cat and exactly one dog.

Ingenious as they is, the producers has decided to use a advancement procedure which guarantees that as many viewers as Possible would continue watching the show:the pets that get to stay would be chosen so as to maximize the number of viewer s who get both their opinions satisfied. Write a program to calculate this maximum number of viewers. 
Inputon the first line one positive number:the number of testcases in 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 the pet identifiers each. The first is the pet-this voter wants to keep, and the second is the pet-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 are an integer giving the number of the pet (between 1 and C for Cats, and between 1 a nd d for Dogs). So for instance, ' D42 ' indicates dog number 42.
Outputper testcase:

* One line with the maximum possible number of satisfied voters for the show.
Sample Input
1 2C1 d1d1 C11 2 4c1 d1c1 d1c1 d2d2 C1
Sample Output
13
SOURCENWERC 2008
Recommendlcy


Main topic:

There are v spectators, who cast out to their favorite animals and nasty animals.

If a spectator's favorite animal conflicts with another animal that the audience likes, let an audience leave and ask for the last few viewers to leave.


Example Analysis:

Input analysis:

1 2C1 d1//The first audience like is Cat1 nasty is dog1d1 C11 2 4c1 d1c1 d1c1 d2d2 C1

Output Analysis:

In a second example. How is the output 3 obtained? We put the cat on one side and like the dog. Then we build the edge based on the situation of the conflict, and we can get:



The maximum match is 1----4, 2----4, 3-----4 One of these three edges. 1----4 have been selected this edges example. If you want to be unclear, let's review the concepts here.

Match: is a set of edges, arbitrarily two edges do not exist common points.

Maximum match: The most variable match.




Topic Analysis:

Put on the side of the cat, like the dog on the side, if there is a conflict. An edge is built. The problem is transformed into the maximum independent set problem of the binary graph.


It is necessary to pay attention to the rules of the building in this problem.

This problem is implemented using adjacency matrices. Time-consuming seems to be 390ms. Using adjacency tables may be faster.



The code is as follows:

/* * g.cpp * * Created on:2015 March 14 * author:administrator * * #include <iostream> #include <cstdio> #inc Lude <cstring>using namespace Std;const int maxn = 501;int Map[maxn][maxn];bool useif[maxn];int link[maxn];int N;bo OL can (int t) {int i;for (i = 1; I <= n; ++i) {if (useif[i] = = False && Map[t][i] = True) {Useif[i] = True;if (lin K[i] = =-1 | | Can (Link[i])) {Link[i] = T;return true;}}} return false;} int Max_match () {int num = 0;int i;for (i = 1; I <= n; ++i) {memset (useif,false,sizeof (Useif)); if (can (i) = = true) {num++; }}return num;} int main () {string loves[maxn];string hates[maxn];int t;scanf ("%d", &t), while (t--) {memset (map,false,sizeof (map)) memset, link,-1,sizeof (link), int cats,dogs;scanf ("%d%d%d", &cats,&dogs,&n), int i;for (i = 1; I <= N; ++i ) {cin >> loves[i] >> hates[i];} /** * This problem is different from other problems in the building of the side part. * So the rest of the part no longer stares, here only for the build-up process plus gaze */int j;for (i = 1; I <= n; ++i) {//traversal of each spectator's favorite animal for (j = 1; j <= N; ++j) {//traverse every spectator annoying animal/ /Assume that sectionI spectator favorite Animal = = J Spectator Nasty Animal | | The I-viewer hates animals = = J-Viewers like animals.

Prove that there is a conflict between I and J if (loves[i] = = Hates[j] | | hates[i] = = Loves[j]) {//For I and J build edge map[i][j] = true;map[j][i] = true;}}} printf ("%d\n", N-max_match ()/2);} return 0;}






Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

(Hdu step 6.3.7) Cat vs. Dog (when construction party rules: build edges when audiences clash with other viewers ' favorite things, seeking maximum independent set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.