Ultraviolet A 10972-revolc faelon
Question Link
Given an undirected graph (not necessarily fully connected), we can now direct the edges and add several more edges to make the graph strongly connected.
Idea: first find out the edge-dual connected component, each connected component can be oriented, and then shrink the point into an Euler loop. If each vertex degree is an even number greater than or equal to 2, it is the loop, that is, it is strongly connected, so the number of degrees calculated is 0 and 1. One side can increase two degrees, so the answer is to add (a + 1) /2 + B sides. Note that the edge-Dual-connected component is used at the beginning. The answer should be 0.
Code:
# Include <cstdio> # include <cstring> # include <vector> using namespace STD; const int n = 1005; struct edge {int U, V, ID; int fan; bool iscut, used; edge () {}edge (int u, int V, int ID, int fan) {This-> U = u; this-> V = V; this-> id = ID; this-> fan = fan; used = false; iscut = false ;}}; int pre [N], low [N], dfs_clock; vector <edge> G [N]; vector <edge> cut; int DFS (int u, int FA) {int Lowu = pre [u] = ++ dfs_clock; For (int I = 0; I <G [u]. size (); I ++) {int v = G [u] [I]. v; int id = G [u] [I]. ID; If (ID = FA) continue; If (! Pre [v]) {int lowv = DFS (v, ID); Lowu = min (Lowu, lowv); If (lowv> pre [u]) {cut. push_back (G [u] [I]); G [u] [I]. iscut = true; G [v] [G [u] [I]. fan]. iscut = true ;}} else Lowu = min (Lowu, pre [v]);} return low [u] = Lowu;} int bcc_cnt, bccno [N]; void dfs2 (int u, int bcc_cnt) {bccno [u] = bcc_cnt; For (INT I = 0; I <G [u]. size (); I ++) {If (G [u] [I]. iscut) continue; int v = G [u] [I]. v; If (bccno [u] = bccno [v]) continue; dfs2 (V, bcc_cnt) ;}} void find_cut (int n) {cut. clear (); memset (PRE, 0, sizeof (pre); dfs_clock = 0; For (INT I = 0; I <n; I ++) {If (! Pre [I]) {DFS (I, 0) ;}} void find_bcc (INT N) {find_cut (n); bcc_cnt = 0; memset (bccno, 0, sizeof (bccno); For (INT I = 0; I <n; I ++) {If (bccno [I]) continue; dfs2 (I, + + bcc_cnt) ;}} int n, m, Du [N]; int main () {While (~ Scanf ("% d", & N, & M) {for (INT I = 0; I <n; I ++) g [I]. clear (); int U, V; For (INT I = 1; I <= m; I ++) {scanf ("% d", & U, & V); U --; V --; G [u]. push_back (edge (u, v, I, G [v]. size (); G [v]. push_back (edge (v, U, I, G [u]. size ()-1);} find_bcc (n); If (bcc_cnt = 1) {printf ("0 \ n"); continue;} memset (DU, 0, sizeof (DU); For (INT I = 0; I <cut. size (); I ++) {int u = cut [I]. u, v = cut [I]. v; If (bccno [u]! = Bccno [v]) {du [bccno [u] ++; Du [bccno [v] ++ ;}} int A = 0, B = 0; for (INT I = 1; I <= bcc_cnt; I ++) {If (Du [I] = 1) A ++; if (Du [I] = 0) B ++;} printf ("% d \ n", (a + 1)/2 + B) ;}return 0 ;}
Ultraviolet A 10972-revolc faelon (edge-Dual-Connected Component)