HDU 3829 Cat VS Dog (Maximum Independent Set = number of vertices-Maximum number of matching)

Source: Internet
Author: User

Cat VS DogTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Total Submission (s): 2148 Accepted Submission (s ): 748 Problem DescriptionThe zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa. now the zoo Dministrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. so the administrator wants to know which animals he shoshould remove to make maximum number of happy children. inputThe input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500. next P lines, Each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details) OutputFor each case, output a single integer: the maximum number of happy children. sample Input1 1 2C1 D1D1 C1 1 2 4C1 D1C1 D1C1 D2D2 C1 C1 Sample Output13 meaning: There are N cats, M dogs in the zoo, P children go to the zoo, now the animal administrator, how many children can be happy to remove animals that children do not like. Idea: After a bipartite graph is created, it is equivalent to finding the Maximum Independent Set of the Bipartite Graph = the number of vertices-the maximum matching number.

Import java. io. *; import java. util. *; public class Main {int n, m, p; int [] [] map; int [] link = new int [600]; boolean [] mark = new boolean [600]; public static void main (String [] args) {new Main (). work ();} void work () {vertex SC = new vertex (new BufferedInputStream (System. in); while (SC. hasNext () {n = SC. nextInt (); m = SC. nextInt (); p = SC. nextInt (); Node node [] = new Node [p]; map = new int [600] [600]; for (int I = 0; I <p; I ++) {String like = SC. next (); String dis = SC. next (); node [I] = new Node (like, dis);} // create a bipartite graph for (int I = 0; I <p; I ++) {for (int j = I + 1; j <p; j ++) {if (node [I]. like. equals (node [j]. dis) | node [I]. dis. equals (node [j]. like) {map [I] [j] = 1; map [j] [I] = 1 ;}} hungary ();}} // hunid hungary () {Arrays. fill (link, 0); int ans = 0; for (int I = 0; I <p; I ++) {Arrays. fill (mark, false); if (DFS (I) ans ++;} System. out. print Ln (p-ans/2); // each vertex is used twice, so divide by 2} boolean DFS (int x) {for (int I = 0; I <p; I ++) {if (map [x] [I] = 1 &&! Mark [I]) {mark [I] = true; if (link [I] = 0 | DFS (link [I]) {link [I] = x; return true ;}}return false;} class Node {String like; String dis; Node (String like, String dis) {this. like = like; this. dis = dis ;}}}

 


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.