Question link: Click the open link
Question:
Undirected graph with M edge given n points
Ask if there is only one simple ring and some trees in the figure, and the root of these trees is on this simple ring.
Blind write a dot pair .. =
# Include <stdio. h> # include <iostream> # include <algorithm> # include <string. h> # include <queue> # include <vector> # include <set> using namespace STD; # define N 105 # define M 100005 # define INF 10000000 struct edge {int from, to, next;} edge [2 * m]; int head [N], edgenum; int low [N], dfn [N], stack [N]; // The value of the belong array is 1 ~ Blockint index, top; int belong [N], block; // The connected block number of the New Graph (1 ~ Block) bool instack [N]; int bridge; // Number of cut bridges vector <int> BCC [N]; void addedge (int u, int V) {edge e = {u, v, head [u]}; edge [edgenum] = E; head [u] = edgenum ++; edge e2 = {v, U, head [v]}; edge [edgenum] = e2; head [v] = edgenum ++;} void Tarjan (int u, int pre) {int V; low [u] = dfn [u] = ++ index; stack [top ++] = u; instack [u] = true; For (INT I = head [u]; ~ I; I = edge [I]. next) {v = edge [I]. to; // If the duplicate edge is valid, change the following sentence to: If (V = pre & pre_num = 0) {pre_num ++; continue ;} pre_num defines int pre_num = 0; If (V = pre) continue; If (! Dfn [v]) {Tarjan (v, U); If (low [u]> low [v]) low [u] = low [v]; if (low [v]> low [u]) Bridge ++;} else if (instack [v] & low [u]> dfn [v]) low [u] = dfn [v];} If (low [u] = dfn [u]) {block ++; BCC [block]. clear (); do {v = stack [-- top]; instack [v] = false; belong [v] = block; BCC [block]. push_back (V);} while (V! = U) ;}} void work (int l, int R) {memset (dfn, 0, sizeof (dfn); memset (instack, false, sizeof (instack )); index = Top = block = bridge = 0; For (INT I = L; I <= r; I ++) if (! Dfn [I]) Tarjan (I, I) ;}vector <int> G [N]; void suodian () {for (INT I = 1; I <= block; I ++) g [I]. clear (); For (INT I = 0; I <edgenum; I ++ = 2) {int u = belong [edge [I]. from], V = belong [edge [I]. to]; If (u = V) continue; G [u]. push_back (V), g [v]. push_back (u) ;}} void Init () {edgenum = 0; memset (Head,-1, sizeof (head);} int n, m, vis [N], siz [N], F [N]; int find (int x) {return x = f [x]? X: F [x] = find (F [x]);} void Union (int x, int y) {int FX = find (x ), FY = find (y); If (FX = FY) return; If (FX> FY) Swap (FX, FY); F [FX] = FY ;} bool DFS (int u) {vis [u] = 1; bool OK = true; For (INT I = 0; I <G [u]. size (); I ++) {int v = G [u] [I]; If (vis [v]) continue; If (bcc [v]. size ()> 1) OK = false; If (DFS (v) = false) OK = false;} Return OK;} set <int> myset; bool solve () {int I, U, V; Init (); for (I = 1; I <= N; I ++) f [I] = I; while (m --) {Scanf ("% d", & U, & V); addedge (u, v); Union (u, v);} myset. clear (); for (I = 1; I <= N; I ++) myset. insert (find (I); If (myset. size ()> 1) return false; Work (1, N); suodian (); memset (VIS, 0, sizeof vis); memset (siz, 0, sizeof siz ); for (I = 0; I <edgenum; I + = 2) siz [u] + = (belong [edge [I]. from] = belong [edge [I]. to]); for (I = 1; I <= block; I ++) if (! Vis [I] & BCC [I]. size ()> 1 & BCC [I]. size () = siz [I] & G [I]. size ()> = 3) if (DFS (I) return true; return false;} int main () {While (~ Scanf ("% d", & N, & M) solve ()? Puts ("fhtagn! "): Puts (" no "); Return 0;}/* 6 61 22 33 14 55 66 4 */
Codeforces 104C Cthulhu DFS violence | dual-connection deflation