POJ 2771 Guardian of decency (maximum independent number)

Source: Internet
Author: User

Test instructions: One of the 4 conditions that can be met between people is not a pair (also said that these 4 conditions are not satisfied to become a pair), the most likely single number.

Idea: divides the male and female into two parts, next is the binary graph matching question. You can make a pair of edges and then find the maximum match. The topic requires the maximum number of independent numbers.

Maximum independent number = number of vertices-maximum number of matches

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;#defineMAXN 1024structperson{inth; Charmusic[ the]; Charsport[ the];} MALE[MAXN],FEMALE[MAXN];intN,M,K,X,Y,PRE[MAXN];//The number of nodes in the X and Y sets is N, M, and the number of sides is k; the matching edge set is the pre, where the matching edge of node I is (pre[i],i)BOOLV[MAXN],A[MAXN][MAXN];//set two the adjacent matrix is the a,y set of the node's access flag is V, if the Y set of node J has been accessed, then v[j]=trueBOOLDfsinti) {//determines whether an augmented path with the start of node I in the X collection exists    intJ;  for(j=0; j<m; J + +){        if(!v[j]&&a[i][j]) {//search for all inaccessible points adjacent to Iv[j]=1;//Access Node J            if(pre[j]==-1||DFS (Pre[j])) {                //if the precursor of J is an open point or there is an augmented path from the precursor of J, the set (I,J) is the matching edge and the success flag is returned .pre[j]=i; return true; }        }    }    return false;//return failure Flag}intMain () {intt,num,h; Charsex[2]; intI,j,ans; scanf ("%d",&t);  while(t--) {memset (A,0,sizeof(a));//the initialization of adjacent matrices for binary graphsmemset (pre,-1,sizeof(pre));//Match edge set initialized to nulln=m=0; scanf ("%d",&num);  while(num--) {scanf ("%d%s",&h,sex); if(sex[0]=='M') {male[n].h=h; scanf ("%s%s", Male[n].music,male[n].sport); ++N; }            Else{female[m].h=h; scanf ("%s%s", Female[m].music,female[m].sport); ++m; }        }         for(i=0; i<n;++i) {             for(j=0; j<m;++j) {                if(ABS (MALE[I].H-FEMALE[J].H) <= +&AMP;&AMP;STRCMP (male[i].music,female[j].music) = =0&AMP;&AMP;STRCMP (male[i].sport,female[j].sport)! =0) A[i][j]=1; }} ans=0;//number of matched edges initialized to 0         for(i=0; i<n; i++) {//Enumerate each node of the X-setmemset (V,0,sizeof(v));//sets the non-access flag for all nodes in the Y collection            if(Dfs (i)) ans++;//if node I is overwritten by a matching edge, match the number of edges +1} printf ("%d\n", n+m-ans); }    return 0;}
View Code

POJ 2771 Guardian of decency (maximum independent number)

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.