Question Link
A undirected connected graph, with at least several sides added to it to make it an edge connected component.
Idea: first use Tarjan to scale down a vertex. The figure after the scale-down is a tree with the edge connectivity 1. Then find all the leaf nodes, that is, the number of nodes with a degree of 1 leaf, and the number of edges to be added is (leaf + 1)/2;
1 // 3177 2 # include <cstdio> 3 # include <cstring> 4 # include <iostream> 5 # include <algorithm> 6 7 using namespace STD; 8 9 int head [101010], FB [101010], low [101010], dfn [101010], degree [101010]; 10 int CNT, timee, ans; 11 struct node12 {13 int U; 14 int V; 15 int next; 16} p [101010]; 17 18 void Tarjan (int u) 19 {20 dfn [u] = low [u] = ++ timee; 21 for (INT I = head [u]; I! =-1; I = P [I]. next) 22 {23 int v = P [I]. v; 24 if (FB [I ^ 1]) continue; 25 FB [I] = 1; 26 if (dfn [v]) low [u] = min (low [u], dfn [v]); 27 else28 {29 Tarjan (V); 30 low [u] = min (low [v], low [u]); 31 // If (low [v]> dfn [u]) 32 // ANS/++; 33} 34} 35} 36 void Init () 37 {38 CNT = timee = ans = 0; 39 memset (Head,-1, sizeof (head); 40 memset (dfn, 0, sizeof (dfn )); 41 memset (low, 0, sizeof (low); 42 memset (FB, 0, sizeof (FB); 43 Memset (degree, 0, sizeof (degree); 44} 45 void addedge (int u, int v) 46 {47 P [CNT]. U = u; 48 p [CNT]. V = V; 49 p [CNT]. next = head [u]; 50 head [u] = CNT ++; 51 P [CNT]. U = V; 52 p [CNT]. V = u; 53 p [CNT]. next = head [v]; 54 head [v] = CNT ++; 55} 56 int main () 57 {58 int F, R; 59 While (scanf ("% d", & F, & R )! = EOF) 60 {61 Init (); 62 int X, Y; 63 for (INT I = 0; I <r; I ++) 64 {65 scanf ("% d", & X, & Y); 66 addedge (x-1, Y-1); 67} 68 Tarjan (0 ); 69 for (INT I = 0; I <F; I ++) 70 {71 for (Int J = head [I]; J! =-1; j = P [J]. Next) 72 {73 If (low [I]! = Low [p [J]. v]) 74 degree [low [I] ++; 75 76} 77} 78 for (INT I = 1; I <= f; I ++) // The timestamp starts from 1, so the low value can reach F, 79 if (degree [I] = 1) 80 ans ++; 81 printf ("% d \ n", (ANS + 1)/2); 82} 83 return 0; 84}View code