HDU 2768 cat vs. Dog

Source: Internet
Author: User

Question Analysis:

A person is not a dog or a dog. If a person's favorite animal leaves, he will also leave. Ask the maximum number of people left.

Ideas:

People who love cats and dogs are two independent sets. If two people like and hate animals, they can build an edge. The number of people left is the largest independent set.

Maximum Independent Set = number of vertices-Maximum number of matching

  1 #include<iostream>  2 #include<cstdio>  3 #include<cstring>  4 #include<algorithm>  5 #include<queue>  6 using namespace std;  7 const int N=505,INF=0x3f3f3f3f;  8 int bmap[N][N],cx[N],cy[N],dx[N],dy[N];  9 bool bmask[N]; 10 int nx,ny,dis,ans; 11 bool searchpath() 12 { 13     queue<int> q; 14     dis=INF; 15     memset(dx,-1,sizeof(dx)); 16     memset(dy,-1,sizeof(dy)); 17     for(int i=1;i<=nx;i++) 18     { 19         if(cx[i]==-1){ q.push(i); dx[i]=0; } 20         while(!q.empty()) 21         { 22             int u=q.front(); q.pop(); 23             if(dx[u]>dis) break; 24             for(int v=1;v<=ny;v++) 25             { 26                 if(bmap[u][v]&&dy[v]==-1) 27                 { 28                     dy[v]= dx[u] + 1; 29                     if(cy[v]==-1) dis=dy[v]; 30                     else 31                     { 32                         dx[cy[v]]= dy[v]+1; 33                         q.push(cy[v]); 34                     } 35                 } 36             } 37         } 38     } 39     return dis!=INF; 40 } 41 int findpath(int u) 42 { 43     for(int v=1;v<=ny;v++) 44     { 45         if(!bmask[v]&&bmap[u][v]&&dy[v]==dx[u]+1) 46         { 47             bmask[v]=1; 48             if(cy[v]!=-1&&dy[v]==dis) continue; 49             if(cy[v]==-1||findpath(cy[v])) 50             { 51                 cy[v]=u; cx[u]=v; 52                 return 1; 53             } 54         } 55     } 56     return 0; 57 } 58 void maxmatch() 59 { 60     ans=0; 61     memset(cx,-1,sizeof(cx)); 62     memset(cy,-1,sizeof(cy)); 63     while(searchpath()) 64     { 65         memset(bmask,0,sizeof(bmask)); 66         for(int i=1;i<=nx;i++) 67             if(cx[i]==-1) ans+=findpath(i); 68     } 69 } 70 void init() 71 { 72     memset(bmap,0,sizeof(bmap)); 73 } 74 struct node 75 { 76     int x,y; 77 }c[N],d[N]; 78 int main() 79 { 80     //freopen("test.txt","r",stdin); 81     int cas,i,j,k,n,a,b; 82     char ch; 83     scanf("%d",&cas); 84     while(cas--) 85     { 86         scanf("%d%d%d",&a,&b,&n); 87         init(); 88         j=k=0; 89         for(i=1;i<=n;i++) 90         { 91             getchar(); 92             ch=getchar(); 93             if(ch==‘C‘) { 94                 scanf("%d",&a); 95                 c[j].x=a; 96             } 97             if(ch==‘D‘) { 98                 scanf("%d",&a); 99                 d[k].y=a;100             }101             scanf(" %c",&ch);102             if(ch==‘C‘) {103                 scanf("%d",&a);104                 d[k++].x=a;105             }106             if(ch==‘D‘) {107                 scanf("%d",&a);108                 c[j++].y=a;109             }110         }111         nx=j;ny=k;112         for(i=0;i<nx;i++){113             for(j=0;j<ny;j++){114                 if(c[i].x==d[j].x||c[i].y==d[j].y)115                     bmap[i+1][j+1]=1;116             }117         }118         maxmatch();119         printf("%d\n",n-ans);120     }121     return 0;122 }
View code

 

HDU 2768 cat vs. Dog

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.