HDOJ 2768-Cat vs. Dog

Source: Internet
Author: User

Question:

There is a TV program called "Cat vs Dog ".. every guest will either like a dog or a cat or a dog .. ask how to arrange for the most guests to be satisfied with cats and dogs )..

Question:

This question gives us a better understanding of the Maximum Independent Set of the Bipartite Graph .. in turn .. divide the audience into two parts .. some like cats and some like dogs .. there is a conflict between the two audiences .. obtain the Maximum Independent Set (N-maximum matching ).. we get the most vertices without edges .. according to the relationship of the diagram .. the number of these points is the answer...

This type of problem can be summarized as follows: the graph is a bipartite graph, and there are some point-to-point conflicts. The most commonly found points make it non-contradictory...

Program:

#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stack>#include<string.h>#include<queue>#define ll long long#define esp 1e-5#define MAXN 505#define MAXM 50000000#define oo 100000007using namespace std; int n,match[MAXN],P[MAXN][2],color[MAXN];bool used[MAXN],g[MAXN][MAXN];bool dfs(int x){       for (int i=1;i<=n;i++)          if (g[x][i] && !used[i])          {                used[i]=true;                if (!match[i] || dfs(match[i]))                {                       match[i]=x;                       return true;                }          }       return false;}int getmax(){       int sum=0;       memset(match,0,sizeof(match));       for (int i=1;i<=n;i++)           if (color[i]==1)          {                memset(used,false,sizeof(used));                sum+=dfs(i);          }       return sum;}int main()  {                int cases,C,D,i,x,y;       char c;        scanf("%d",&cases);       while (cases--)         {                  scanf("%d%d%d",&C,&D,&n);                for (i=1;i<=n;i++)               {                       do { c=getchar(); }while (c!='C' && c!='D');                       scanf("%d",&x);                        if (c=='D') x+=C,color[i]=2;                                   else color[i]=1;                       do { c=getchar(); }while (c!='C' && c!='D');                       scanf("%d",&y);                           if (c=='D') y+=C;                       P[i][0]=x,P[i][1]=y;               }               memset(g,false,sizeof(g));               for (x=1;x<=n;x++)                  if (color[x]==1)                    for (y=1;y<=n;y++)                       if (P[x][0]==P[y][1] || P[y][0]==P[x][1])                           g[x][y]=true;               printf("%d\n",n-getmax());       }         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.