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