Maximum Independent Set of HDU 2768 cat vs. Dog

Source: Internet
Author: User

I began to misunderstand the meaning of the question and thought that the audience should meet both conditions. Later, I can see the report on understanding the question. When the audience wants to leave their pets out, the audience thinks they will leave.

The audience is divided into two sets by cat_lover and dog_lover. If there is a conflict between the two sets, the two vertices are connected, the edges represent a conflict, and then the largest independent set is obtained.

Maximum Independent Set = number of vertices-minimum vertex overwrite (maximum matching)

 

# Include <iostream> using namespace STD; const int n = 505; struct option {char a [5], B [5] ;}; int maze [N] [N]; option cat [N], dog [N]; int isvisit [N]; int match [N]; int C, D, V; int cnt_cat, cnt_dog; bool find (int u) {for (INT I = 0; I <cnt_dog; I ++) if (maze [u] [I] &! Isvisit [I]) {isvisit [I] = true; If (Match [I] =-1 | find (Match [I]) {match [I] = u; return true ;}} return false ;}int main () {INT cases; char a [5]; char B [5]; scanf ("% d", & cases); While (cases --) {scanf ("% 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 (maze, false, sizeof (maze); 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) maze [I] [J] = true; int ans = 0; memset (match,-1, sizeof (MATCH )); for (INT I = 0; I <cnt_cat; I ++) {memset (isvisit, false, sizeof (isvisit); If (find (I) ans ++ ;} printf ("% d \ n", V-ans);} return 0 ;}

 

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.