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;}