Topic links
Test instructions: N tunnels are connected by a number of points, each of which links two connection points. There is a maximum of one tunnel between any two connection points. The task is to install as few Taiping Wells and escape devices as possible in these connection points, so that no matter which connection point collapses, workers can escape from other Taiping Wells for minimum installation quantities and solutions.
Analysis: The subject is equivalent to a non-map on the selection of the minimum point to black (corresponding to the peace well), so that any point is deleted, each connected component has at least one black spot. Different connected components at most have a common point is cut point, will cut point painted is not cost-effective, because delete cut point, to ensure that each connected component also have black spot, so also in other connected components of black spot, if not to cut point, also can save one, in a point connected component of two black spots is not cost-effective, So only if a point connected component contains a cut point, then the other points of the cut point are applied, because if you delete this cut point, you have to make sure that there is a black dot left. For a connected component containing >= 2 cut points, it is not necessary to paint, because he has two cut points will not be all removed, can lead to other connected components of the Taiping well,
1#include <iostream>2#include <cstring>3#include <algorithm>4#include <cstdio>5#include <vector>6#include <stack>7 using namespacestd;8typedefLong LongLL;9 Const intMax =50005;Ten structEdge One { A intu, v; - }; -vector<int>G[max], Bcc[max]; the intPre[max], Bccno[max], Iscut[max]; - intDfs_clock, bcc_cnt; -Stack<edge>S; - intDfsintUintFA) + { - intLowu = Pre[u] = + +Dfs_clock; + intSize =g[u].size (); A intChild =0; at for(inti =0; i < Size; i++) - { - intv =G[u][i]; - Edge E; -E.U =u; -E.V =v; in if(!Pre[v]) - { to S.push (e); +child++; - intLOWV =Dfs (v, u); theLowu =min (Lowu, LOWV); * if(LOWV >=Pre[u]) $ {Panax NotoginsengIscut[u] =true; -bcc_cnt++; the bcc[bcc_cnt].clear (); + for (; ;) A { theEdge x =s.top (); + S.pop (); - if(BCCNO[X.U]! =bcc_cnt) $ { $BCCNO[X.U] =bcc_cnt; - Bcc[bcc_cnt].push_back (x.u); - } the if(BCCNO[X.V]! =bcc_cnt) - {WuyiBCCNO[X.V] =bcc_cnt; the Bcc[bcc_cnt].push_back (X.V); - } Wu if(x.u = = U && x.v = =v) - Break; About } $ } - } - Else if(Pre[v] < Pre[u] && v! =FA) -Lowu =min (Lowu, pre[v]); A } + if(Child = =1&& FA <0) theIscut[u] =0; - returnLowu; $ } the voidFIND_BCC (intN) the { the if(!s.empty ()) the S.pop (); -memset (PRE,0,sizeof(pre)); inmemset (Bccno,0,sizeof(BCCNO)); thememset (Iscut,0,sizeof(Iscut)); theDfs_clock = bcc_cnt =0; About for(inti =1; I <= N; i++) the { the if(!Pre[i]) theDFS (I,-1); + } - } the intMain ()Bayi { the intTest =0, N; the while(SCANF ("%d", &n)! = EOF &&N) - { - intU, V, temp =-1; the for(inti =1; i < Max; i++) theG[i].clear ();//Clear the for(inti =1; I <= N; i++) the { -scanf"%d%d", &u, &v); the G[u].push_back (v); the g[v].push_back (u); thetemp = max (temp, max (U, v));//find the biggest point94 } theFIND_BCC (temp);//Find Connectivity Components theLL ans1 =0, Ans2 =1; the for(inti =1; I <= bcc_cnt; i++)98 { About intcut_cnt =0; - intSize =bcc[i].size ();101 for(intj =0; J < Size; J + +)102 {103 if(iscut[bcc[i][j])104cut_cnt++; the }106 if(cut_cnt = =1)107 {108ans1++;109Ans2 *= (LL) (Size-1); the }111 } the if(bcc_cnt = =1)113 { theANS1 =2; theAns2 = (LL) bcc[1].size () * (LL) (bcc[1].size ()-1) /2; the }117printf"Case %d:%lld%lld\n", ++test, ans1, ans2);118 }119 return 0; -}
View Code
UVA5135 Mining Your Own Business (undirected graph dual connected component)