Required edges for row-column matching and maximum matching in a Bipartite Graph

Source: Internet
Author: User

Hdu1287

A: placing a car on the Board requires that the car cannot attack each other. That is, the car must be in different rows and columns, and match the rows and columns in the bipartite graph.

However, if the vertices are not placed in the vehicle, the maximum matching cannot be formed, that is, the vertices are the required edges for the maximum matching.

Determine whether the edge is a required edge with the largest match. If the edge is deleted and matched, compare the number of matches with the original number to determine whether the edge is a required edge with the largest match.

 

 1 #include <stdio.h> 2 #include <string.h> 3 const int N = 100 + 10; 4 int Map[N][N]; 5 int n,m,k; 6 bool vis[N]; 7 int cy[N],cx[N]; 8  9 bool dfs(int u)10 {11     for(int i=0; i<m; ++i)12     {13         if(!vis[i] && Map[u][i]==1)14         {15             vis[i] = true;16             if(cy[i]==-1 || dfs(cy[i]))17             {18                 cy[i] = u;19                 cx[u] = i;20                 return true;21             }22         }23     }24     return false;25 }26 int MaxMatch()27 {28     memset(cx, -1, sizeof(cx));29     memset(cy, -1, sizeof(cy));30     int cnt = 0;31     for(int i=0; i<n; ++i)32     {33         if(cx[i] == -1)34         {35             memset(vis, 0, sizeof(vis));36             cnt += dfs(i);37         }38     }39     return cnt;40 }41 int main()42 {43     int i,j,x,y;44     int tCase = 1;45     while(scanf("%d%d%d",&n,&m,&k)!=EOF)46     {47         memset(Map, 0, sizeof(Map));48         for(i=0; i<k; ++i)49         {50             scanf("%d%d",&x,&y);51             x-=1;y-=1;52             Map[x][y] = 1;53         }54         int ans = MaxMatch();55         int ans2 = 0;56         for(i=0; i<n; ++i)57             for(j=0; j<m; ++j)58                 if(Map[i][j] == 1)59                 {60                     Map[i][j] = 0;//删边61                     int cnt = MaxMatch();62                     if(cnt != ans)63                         ans2++;64                     Map[i][j] = 1;65                 }66         printf("Board %d have %d important blanks for %d chessmen.\n",tCase++,ans2,ans);67     }68     return 0;69 }

 

Required edges for row-column matching and maximum matching in a Bipartite Graph

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.