The main idea: there are n mines, connected by some tunnels, now to build as few safe passages as possible, so that wherever accidents occur, all can escape, to build the minimum number of safe channels and the number of programs
Problem-solving ideas: To build a safe passage, it must not be built in the cutting top, because if the collapse of the top, cut the top of the double connected components within the point of running away, but also in the double-connected components inside the building point (the above is a double-connected component inside only a cut top case), so not cost, it is better
If a two-connected component of the internal cutting top has multiple words, then in this dual-connected component can not be built safe channel, because a cut roof collapsed, there are other points can be connected to the outside, so, only consider the internal only one cut the double connected components how to build a secure channel
How to build it, to eliminate all the points of the cutting roof can be built
Assuming that the ANS1 represents the number of secure channels that need to be established, ANS2 represents the number of schemes established, then there is only one double connected component in the internal node (the internal nodes of the two connected components have N, excluding the cut-off, and the n-1-based scheme)
Then ans1 = ans1 + 1; Ans2 *= (n-1)
If the given graph is a two-connected component (assuming there are n points), there is only one, then 2 secure channels need to be established (one collapsed and the other), the number of scenarios is n * (n-1)/2
#include <cstdio>#include <cstring>#include <algorithm>#include <stack>#include <queue>#include <stack>using namespace STD;#define N 100010#define INF 0x3f3f3f3f#define LL Long Longstructedge{intTo, next;} E[n];structNode {intU, v; Node () {} node (intUintV): U (U), V (v) {}};intPre[n], Iscut[n], bccno[n], head[n], Dfs_clock, bcc_cnt;intN, tot, Max; vector<int>Bcc[n]; Stack<Node>SvoidAddedge (intFromintTo) {e[tot].to = to; E[tot].next = Head[from]; Head[from] = tot++;}voidInit () {memset(Head,-1,sizeof(head)); tot =0;intU, v; Max =-inf; for(inti =0; I < n; i++) {scanf("%d%d", &u, &v); u--; v--; Addedge (U, v); Addedge (V, u);if(U > Max | | v > MAX) {max = max (U, v); } }}intDfsintUintFA) {intLowu = Pre[u] = ++dfs_clock;intChild =0; for(inti = Head[u]; I! =-1; i = e[i].next) {intv = e[i].to; Node e = node (u, v);if(!pre[v]) {S.push (e); child++;intLOWV = DFS (v, u); Lowu = min (Lowu, LOWV);if(LOWV >= Pre[u]) {Iscut[u] =true; bcc_cnt++; Bcc[bcc_cnt].clear (); while(1) {Node t = s.top (); S.pop ();if(bccno[t.u]! = bcc_cnt) {Bcc[bcc_cnt].push_back (T.U); BCCNO[T.U] = bcc_cnt; }if(BCCNO[T.V]! = bcc_cnt) {Bcc[bcc_cnt].push_back (T.V); BCCNO[T.V] = bcc_cnt; }if(T.u = = U && T.V = = v) Break; } } }Else if(Pre[v] < Pre[u] && v! = FA) {Lowu = min (Pre[v], LOWU); S.push (e); } }if(FA <0&& Child = =1) Iscut[u] =false;returnLowu;}intCAS =1;voidSolve () {memset(Pre,0,sizeof(pre));memset(Iscut,0,sizeof(Iscut));memset(Bccno,0,sizeof(BCCNO)); Dfs_clock = bcc_cnt =0; for(inti =0; I <= Max; i++)if(!pre[i]) DFS (i,-1); ll ans1 =0, Ans2 =1; for(inti =1; I <= bcc_cnt; i++) {intCnt_cut =0; for(intj =0; J < Bcc[i].size (); J + +) {if(Iscut[bcc[i][j]]) cnt_cut++; }if(Cnt_cut = =1) {ans1++; Ans2 *= (LL) (Bcc[i].size ()-1); } }if(bcc_cnt = =1) {ans1 =2; Ans2 = (ll) bcc[1].size () * (bcc[1].size ()-1) /2; }printf("Case%d:%lld%lld\n", cas++, ans1, ans2);}intMain () { while(scanf("%d", &n)! = EOF && N) {init (); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVALive-5135 Mining Your Own Business (dual connected components)