Scheme of the minimum Lexicographic Order of the maximum matching output in a bipartite graph poj 3715

Source: Internet
Author: User

Http://poj.org/problem? Id = 3715

Violence can also be avoided: first obtain the maximum match, then remove the enumeration points from the smallest to the largest, and then find the maximum match again. If the maximum number of matches is reduced, this point can be removed.

The best practice should be to make full use of the nature of the binary matching Hungarian algorithm. If the augmented path cannot be found from the point that the vertex matches after removing a vertex, the vertex can be deleted, in this way, you don't need to find the global maximum match every time, like one ..

#include<cstdio>#include<cstring>const int M  =  210;bool g[M][M] ,  vis[M] , deleted[M] , grp[M];int px[M] , py[M] ;int n;bool canx(int x){if(deleted[x]) return false;for(int i = 0; i < n; i++){if(!g[x][i] || vis[i]  || deleted[i] || !grp[i]) continue;vis[i] = true;if(py[i]==-1 || canx(py[i])){px[x] = i; py[i] = x;return true;}}return false;}bool cany(int y){if(deleted[y]) return false;for(int i = 0; i < n; i++){if(!g[y][i] || vis[i] || deleted[i] || grp[i]) continue;vis[i] = true;if(px[i]==-1 || cany(px[i])){px[i] = y;py[y] = i;return true;}}return false;}int main(){int t,m,i,j,k;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);memset(g,false,sizeof(g));memset(deleted,false,sizeof(deleted));memset(px,-1,sizeof(px));memset(py,-1,sizeof(py));for(i = 0; i < n; i++) scanf("%d",&grp[i]);for(i = 0; i < m; i++){scanf("%d%d",&j,&k);if(grp[j] != grp[k]){g[j][k] = g[k][j] = true;}}int ans = 0;for(i = 0; i < n; i++) if(grp[i]==0){memset(vis,false,sizeof(vis));ans += canx(i);}printf("%d",ans);int x,y;for(i = 0; i < n && ans; i++){if(!grp[i]){if(px[i] == -1 ) continue;y = px[i];deleted[i] = true;px[i] = -1;py[y] = -1;memset(vis,false,sizeof(vis));if(cany(y)){deleted[i] = false;}else printf(" %d",i);}else {if(py[i] == -1) continue;x = py[i];deleted[i] = true;py[i] = -1;px[x] = -1;memset(vis,false,sizeof(vis));if(canx(x)){deleted[i] = false;}else printf(" %d",i);}}puts("");}return 0;}
Related Article

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.