This is a code big problem. First read the wrong test instructions, and then understand the direct look of the kind of adjacent, and then do not want to pass a long time!!!
Separate the diagram of the different unicom, and then pre-preprocess the formation of the adjacent diagram after the state, and then according to 01 to determine which is the need to change the state, and then build a diagram, using the binary diagram km algorithm to match the optimal scheme. Then find each group of replacements, using the previously obtained path to store the answer.
#include <bits/stdc++.h>using namespacestd;Const intINF =0x3f3f3f3f;Const intMAXN = 5e2 +7; Vector<pair<int,int> >Ans;vector<int>GRA[MAXN], PATH[MAXN][MAXN], X, Y, left, right;CharSTR[MAXN];intG[MAXN][MAXN], COL[MAXN], *Pairr, PAIRR1[MAXN], PAIRR2[MAXN];intMP[MAXN][MAXN], LX[MAXN], LY[MAXN], LINKER[MAXN], SLACK[MAXN];intN, M, L, NX, NY;BOOLVIS[MAXN], VISX[MAXN], VISY[MAXN];BOOLDFS (intx) {Visx[x]=true; for(inty =0; Y < NY; y++) { if(Visy[y])Continue; intTMP = Lx[x] + ly[y]-Mp[x][y]; if(TMP = =0) {Visy[y]=true; if(Linker[y] = =-1||DFS (Linker[y])) {Linker[y]=x; return true; } } Else if(Slack[y] >tmp) Slack[y]=tmp; } return false;}intKM () {memset (linker,-1,sizeof(linker)); memset (ly,0,sizeof(ly)); for(inti =0; I < NX; i++) {Lx[i]= -inf; for(intj =0; J < NY; J + +) if(Mp[i][j] >Lx[i]) lx[i]=Mp[i][j]; } for(intx =0; x < NX; X + +) { for(inti =0; i < NY; i++) Slack[i]=inf; while(true) {memset (VISX,false,sizeof(VISX)); memset (Visy,false,sizeof(Visy)); if(DFS (x)) Break; intD =inf; for(inti =0; i < NY; i++) if(!visy[i] && d >Slack[i]) d=Slack[i]; for(inti =0; I < NX; i++) if(Visx[i]) lx[i]-=D; for(inti =0; i < NY; i++) { if(Visy[i]) ly[i]+=D; ElseSlack[i]-=D; } } } intres =0; for(inti =0; i < NY; i++) if(Linker[i]! =-1) Res-=Mp[linker[i]][i]; returnRes;}voidGetminroad (intu) {Queue<int>que; while(!que.empty ()) Que.pop (); Que.push (u); G[u][u]=0; Path[u][u].push_back (U); memset (Vis,false,sizeof(VIS)); Vis[u]=true; while(!Que.empty ()) { intSt =Que.front (); Que.pop (); for(Auto to:gra[st]) {if(Vis[to])Continue; Vis[to]=true; G[u][to]= G[u][st] +1; Path[u][to]=Path[u][st]; Path[u][to].push_back (to); Que.push (to); } }}BOOLColorintSt) {Queue<int> que; while(!que.empty ()) Que.pop (); X.push_back (ST); Que.push (ST); COL[ST]=0; while(!Que.empty ()) { intU =Que.front (); Que.pop (); for(Auto V:gra[u]) {if(Col[v] = = Col[u])return false; if(Col[v]! =-1)Continue; COL[V]= Col[u] ^1; if(Col[v]) y.push_back (v); ElseX.push_back (v); Que.push (v); } } return true;}voidGetans (intUintVintSETP) { if(U = = v)return ; Vector<int>&tmp =Path[u][v]; for(inti =0; I < tmp.size ()-1; i + +) if(Str[tmp[i]]! = str[tmp[i +1]]) {Ans.push_back (Make_pair (tmp[i), Tmp[i+1])); Swap (Str[tmp[i]), Str[tmp[i+1]]); Getans (U, tmp[i], SETP+1); Getans (Tmp[i+1], V, SETP +1); return ; }}intGetsum (vector<int>a, vector<int>b,intpair[]) {left.clear (); Right.clear (); for(Auto x:a)if(Str[x] = ='1') Left.push_back (x); for(Auto x:b)if(Str[x] = ='0') Right.push_back (x); NX= NY =left.size (); for(inti =0; I < NX; i + +) for(intj =0; J < NY; J + +) Mp[i][j]= -G[left[i]][right[j]]; intRET =KM (); for(inti =0; I < NX; i + +){ intU = right[i], V =Left[linker[i]]; Pair[u]= V; PAIR[V] =u; } returnret;}BOOLSolveintSt) { intZero =0, sum1 = inf, sum2 =inf; X.clear (), y.clear (); if(!color (ST))return 0; for(Auto x:x)if(Str[x] = ='0') Zero + +; for(Auto x:y)if(Str[x] = ='0') Zero + +; if(zero = = X.size ()) sum1 =getsum (X, Y, PAIRR1); if(zero = = Y.size ()) sum2 =getsum (Y, X, PAIRR2); if(sum1 = = inf && sum2 = = inf)return false; Pairr= sum1 > sum2?pairr2:pairr1; for(Auto x:x)if(pairr[x]! =-1) Getans (x, Pairr[x],1); return true;}voidinit () {memset (PAIRR1,-1,sizeof(PAIRR1)); memset (PAIRR2,-1,sizeof(PAIRR2)); memset (Slack,0,sizeof(slack)); for(inti =1; I <= N; i + +) {gra[i].clear (); Col[i]= -1; for(intj =1; J <= N; J + +) {G[i][j]=inf; Path[i][j].clear (); }} ans.clear ();}intMain () {intT,A,B;SCANF ("%d",&T); while(T--) {scanf ("%d%d%s", &n, &m, str +1); Init (); for(inti =0; I < m; i + +) {scanf ("%d%d",&a,&b); Gra[a].push_back (b); Gra[b].push_back (a); } for(inti =1; I <= N; i + +) getminroad (i); BOOLFlag =true; for(inti =1; I <= N; i + +){ if(Col[i] = =-1&&!solve (i)) {Flag=false; Break; } } if(!flag) {printf ("-1\n"); Continue; } printf ("%d\n", Ans.size ()); for(inti =0; I < ans.size (); i + +) {printf ("%d%d\n", Ans[i].first, Ans[i].second); } } return 0;}
Glorious Brilliance (Shortest path + weighted binary graph matching)