HDU 3829 Cat VS Dog (maximum standalone set)

Source: Internet
Author: User

The main topic: There are n cats, there are M dogs. Now there are P students to visit the zoo. Each child has a favorite animal and does not like animals. If he likes cats then he must not like dogs (and vice versa). If a child likes an animal, the animal is not removed and removed if it is not liked. Now the administrator wants to know which animals to remove to make the maximum number of children happy. Input data: The input contains multiple sets of test instances. The first line is three digits n, M, p. Next P line. CX per row, DX for he likes the X-Cat, hates the X-dog (and vice versa) the topic of thinking: the composition of ideas: We put all the people in the composition, if there are contradictions between two people to build a side. And then we can find the maximum independent set.the maximal independent set of the dichotomy graph:maximum independent set of binary graph = number of points in graph-Maximum matchIt can be understood that in the total point set, the least points are removed, so that the remaining points have no edges between each other. Cover all edges with the fewest points, which is the minimum coverage.
#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<vector>#include<queue>#include<cmath>using namespacestd;#defineINF 0X3FFFFFFF#defineMAXN 505intN, M, p, P[MAXN], COLOR[MAXN];///n cat m dog p personalBOOLG[MAXN][MAXN], VIS[MAXN];///compositionvector<vector<int> > Likedog;///people like K-Dogs adjacency tablevector<vector<int> > Disdog;///don't like the collection of people who are just enoughvector<vector<int> > Likecat;///Ibid .vector<vector<int> >Discat;voidInit () {likedog.clear (); Likedog.resize (M+1);    Likecat.clear (); Likecat.resize (n+1);    Disdog.clear (); Disdog.resize (M+1);    Discat.clear (); Discat.resize (n+1); memset (G,false,sizeof(G));}voidMakemap () { for(intI=1; i<=m; i++)    {        intLen1 =likedog[i].size (); intLen2 =disdog[i].size ();  for(intj=0; j<len1; J + +)        {            intV1 =Likedog[i][j];  for(intk=0; k<len2; k++)            {                intV2 =Disdog[i][k]; G[V1][V2]=true; G[V2][V1]=true; }        }    }     for(intI=1; i<=n; i++)    {        intLen1 =likecat[i].size (); intLen2 =discat[i].size ();  for(intj=0; j<len1; J + +)        {            intV1 =Likecat[i][j];  for(intk=0; k<len2; k++)            {                intV2 =Discat[i][k]; G[V1][V2]=true; G[V2][V1]=true; }        }    }}BOOLFind (intu) {     for(intI=1; i<=p; i++)    {        if(!vis[i] &&G[u][i]) {Vis[i]=true; if(P[i] = =-1||Find (P[i])) {P[i]=u; return true; }        }    }    return false;}voidDFS (intUintColor) {Color[u]=Color;  for(intI=1; i<=p; i++)    {        if(G[u][i] &&!Color[i]) {DFS (i,-Color); }    }}intsolve () {intAns =0; memset (P,-1,sizeof(P)); memset (color,0,sizeof(color));  for(intI=1; i<=p; i++)    {        if(Color[i] = =0) DFS (i,1); }     for(intI=1; i<=p; i++) {memset (Vis,false,sizeof(VIS)); if(Color[i] = =1&&Find (i)) ans++; }    returnP-ans;}intMain () { while(SCANF (" %d%d%d", &n, &m, &p)! =EOF) {        CharCh1, CH2; intA, B;        Init ();  for(intI=1; i<=p; i++) {scanf ("%c%d%c%d", &ch1, &a, &AMP;CH2, &b);            GetChar (); if(Ch1 = ='C') {likecat[a].push_back (i);            Disdog[b].push_back (i); }            Else{likedog[a].push_back (i);            Discat[b].push_back (i);        }} makemap (); printf ("%d\n", Solve ()); }    return 0;}

HDU 3829 Cat VS Dog (maximum standalone 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.