HDU 2444 the accomodation of students makes up a bipartite graph + maximum matching

Source: Internet
Author: User

This is to find the maximum match. However, you need to determine whether a bipartite graph is formed. The method of determination is to mark a person with a red color (0) and a black color (1) as the adjacent vertex. If there is a conflict, a bipartite graph cannot be formed. Declare a vis [] and initialize it to-1. Through deep search, adjacent points will end if they do not satisfy the difference or relationship. If it is not marked, it is marked as an exclusive or adjacent vertex.

  

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

 

HDU 2444 the accomodation of students makes up a bipartite graph + maximum matching

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.