Question:
Some tunnels form a mine. Now we need to build as few escape channels as possible so that everyone can escape wherever an accident occurs.
Ideas:
This question is easy to think of as a cut point, because only when such an accident occurs will the mine be unable to connect, so we should first find all the cut points.
The analysis requires at least several escape channels. Of course, each connected block has one. Therefore, you need to obtain the product of the number of connected block vertices.
Finally, considering that there are no cut points in special cases, we can just build two places. We can't build 10 thousand, so there's an accident there...
Code:
# Include <cstdio> # include <cstring> # include <algorithm> # include <set> using namespace STD; # define n 50010 typedef long ll; int m, n, TOT, t, ans1, res, idx; int head [N], dfn [N], low [N], cut [N], vis [N]; struct edge {int V, flag, next;} ed [N * 2]; ll ans2; set <int> next; void add (int u, int v) {ed [tot]. V = V; Ed [tot]. flag = 0; Ed [tot]. next = head [u]; head [u] = tot ++;} void Tarjan (int u, int FA) {int I, V, son = 0; dfn [u] = low [u] = ++ idx; (I = head [u]; ~ I; I = ed [I]. next) {v = ed [I]. v; If (Ed [I]. flag | dfn [v]> = dfn [u]) continue; Ed [I]. flag = ed [I ^ 1]. flag = 1; if (dfn [v] =-1) {son ++; Tarjan (v, U); low [u] = min (low [u], low [v]); If (u! = Fa & dfn [u] <= low [v]) Cut [u] = 1;} else low [u] = min (low [u], dfn [v]);} If (u = fa & Son> 1) Cut [u] = 1;} void DFS (int u) {int I, V; res ++; vis [u] = 1; for (I = head [u]; ~ I; I = ed [I]. Next) {v = ed [I]. V; If (cut [v]) Next. insert (V); If (! Cut [v] &! Vis [v]) DFS (v) ;}} int main () {int I, U, V; while (~ Scanf ("% d", & M) {If (! M) break; Tot = n = idx = 0; memset (Head,-1, sizeof (head); memset (dfn,-1, sizeof (dfn )); memset (CUT, 0, sizeof (CUT); memset (VIS, 0, sizeof (VIS); for (I = 1; I <= m; I ++) {scanf ("% d", & U, & V); n = max (n, U); n = max (n, V); add (u, v); add (v, U);} Tarjan (1, 1); ans1 = 0; ans2 = 1; for (I = 1; I <= N; I ++) {If (! Cut [I] &! Vis [I]) {res = 0; next. clear (); DFS (I); If (next. size () = 1) {ans1 ++; ans2 * = res ;}} if (ans1 = 0) {ans1 = 2; ans2 = (LL) (n) * (n-1)/2;} printf ("case % d:", ++ T); printf ("% d % LLD \ n", ans1, ans2 );} return 0 ;}
Uvalive 5135 mining your own business