1 /**********************************************************2 Title: Entrance Examination Road boundless--air-conditioning classroom (HDU 2242)3 Links:http://acm.hdu.edu.cn/showproblem.php?pid=22424 algorithm: Double unicom5 idea: Find the bridge and then calculate the difference on both sides of the bridge, on the side of the bridge6 the value is compressed to the number adjacent to the other side. 7 8 ***********************************************************/9 Ten#include <cstdio> One#include <iostream> A#include <cstring> -#include <algorithm> -#include <vector> the#include <stack> - using namespacestd; - - Const intmx=100005; + intA[MX]; -stack<int>s; +vector<int>G[MX]; A intFlag,dfs_cut; at intDFN[MX],LOW[MX],VS[MX]; - intans,sum; - - voidInit (intN) - { - inti; insum=0; - for(i=0; i<n;i++) to { + g[i].clear (); -scanf"%d",&a[i]); thesum+=A[i]; * } $memset (DFN,0,sizeof(DFN));Panax Notoginsengmemset (Low,0,sizeof(Low)); -Memset (VS,0,sizeof(VS)); theflag=dfs_cut=0; +ans=sum; A } the + voidDfsintUintFA) - { $dfn[u]=low[u]=++Dfs_cut; $vs[u]=1; - s.push (u); - intp=1;///Go heavy the for(intI=0; I<g[u].size (); i++) - {Wuyi the intv=G[u][i]; - if(v==fa&&p) Wu { -p=0; About Continue; $ } - if(!Vs[v]) - { - DFS (v,u); Alow[u]=min (low[u],low[v]); + if(Low[v]>dfn[u])///judging the bridge the { -flag=1; $ inttemp=0, X; the the ///to compress the bridge once again the while(1) the { -x=s.top (); in S.pop (); thetemp+=A[x]; the if(X==V) Break; About } theAns=min (Ans,abs (sum-2*temp)); thea[u]+=temp; the } + - } the Elselow[u]=min (low[u],dfn[v]);Bayi } the } the - intMain () - { the intn,m; the while(~SCANF ("%d%d",&n,&m)) the { the Init (n); - while(m--) the { the intu,v; thescanf"%d%d",&u,&v);94 G[u].push_back (v); the g[v].push_back (u); the } theDfs0,-1);98 if(flag) printf ("%d\n", ans); About Elseprintf"impossible\n"); - }101}
HDU 2242 Entrance Examination Road boundless-air-conditioning classroom (double connected entry)