The idea is to find the side of the two connected components, and then shrink the point, and then use the tree-shaped DP to engage.
Code and strong connectivity very similar, a bit magical, =_=, slowly digest it
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <vector>5#include <stack>6 using namespacestd;7 8 Const intMAXN =10000+Ten;9 Const intMAXM =20000+Ten;Ten One intN, M; A intA[MAXN]; - - structEdge the { - intV, NXT; -}EDGES[MAXM *2]; - intecnt; + intHEAD[MAXN]; - + BOOLVIS[MAXN]; A at voidAddedge (intUintv) - { -EDGES[ECNT].V =v; -EDGES[ECNT].NXT =Head[u]; -Head[u] = ecnt++; - } in -stack<int>S; to intDfs_clock, scc_cnt; + intLOW[MAXN], PRE[MAXN], SCCNO[MAXN], W[MAXN]; - the voidDfsintUintFA) * { $Low[u] = Pre[u] = + +Dfs_clock;Panax Notoginseng s.push (u); - the for(inti = Head[u]; ~i; i =edges[i].nxt) + { A if(i = = (fa ^1))Continue; the intv =edges[i].v; + if(!Pre[v]) - { $ Dfs (v, i); $Low[u] =min (Low[u], low[v]); - } - Else if(!sccno[v]) low[u] =min (Low[u], pre[v]); the } - Wuyi if(Pre[u] = =Low[u]) the { -scc_cnt++; Wu for(;;) - { About intx =S.top (); S.pop (); $SCCNO[X] =scc_cnt; -W[SCC_CNT] + =A[x]; - if(x = = u) Break; - } A } + } the - voidFIND_SCC () $ { thescc_cnt = Dfs_clock =0; theMemset (W,0,sizeof(w)); thememset (PRE,0,sizeof(pre)); thememset (Sccno,0,sizeof(SCCNO)); - for(inti =0; I < n; i++)if(!pre[i]) DFS (i,-1); in } the thevector<int>G[MAXN]; About the voidDFS2 (intu) the { theVis[u] =true; + for(inti =0; I < g[u].size (); i++) - { the intv =G[u][i];Bayi if(Vis[v])Continue; the DFS2 (v); theW[u] + =W[v]; - } - } the the intMain () the { the while(SCANF ("%d%d", &n, &m) = =2&&N) - { the intsum =0; the for(inti =0; I < n; i++) {scanf ("%d", A + i); Sum + =a[i];} the 94Memset (Head,-1,sizeof(head)); theECNT =0; the the while(m--)98 { About intU, v; scanf"%d%d", &u, &v); - Addedge (U, v); Addedge (V, u);101 }102 103 FIND_SCC ();104 the if(scc_cnt = =1) {puts ("Impossible");Continue; }106 107 for(inti =1; I <= scc_cnt; i++) g[i].clear ();108 for(intU =0; U < n; u++)109 for(inti = Head[u]; ~i; i =edges[i].nxt) the {111 intv =edges[i].v; the if(Sccno[u]! =Sccno[v]) G[sccno[u]].push_back (Sccno[v]);113 } the thememset (Vis,false,sizeof(Vis)); theDFS2 (1);117 118 intAns =1000000000;119 for(inti =1; I <= scc_cnt; i++) ans = min (ans, ABS (sum-w[i) *2)); -printf"%d\n", ans);121 }122 123 return 0;124}
code June
HDU 2242 Double connected components The boundless--air-conditioned classroom