Transfer from http://www.cnblogs.com/whatbeg/p/3765624.html
First, the original image of the connected components of the contraction point, it is possible to shrink the original image into a tree form, and then count the number of leaf nodes of the tree, the answer is (leaf+1)/2. It is no longer proof that you can draw a picture to see it.
(Briefly, first connect the two closest public ancestors to the two leaf nodes that are farthest from each other, so that the two points can be shrunk a little to the path of the ancestors, because a formed ring must be doubly connected.) Then find the two nearest public ancestor the furthest two leaf nodes, so that a pair to find out, happens to be (leaf+1)/2 times, all the points are shrunk together. --byvoid)
How do you count it? Use and check the set of shrinkage points, you can know that the edge left after the contraction point is all the original bridge, this is we can use Tarjan to find all the original image of the bridge, and then enumerate each bridge, the point of the bridge at each end of the degree of +1, you can find each point (the point after the contraction point) of the degree, to
How to use Tarjan to beg the bridge? According to the nature of the Tarjan algorithm, if Low[v]>dfn[u], then the Edge (U,V) is the bridge (V is blocked in the subtree)
, if Low[v]>dfn[u], then V is sealed in the subtree of u, delete point u, or delete Edge (u,v), will make V and U's ancestor W not connect.
About Tarjan bridge visible: http://hi.baidu.com/lydrainbowcat/item/f8a5ac223e092b52c28d591c
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <string>6#include <queue>7#include <algorithm>8#include <map>9#include <iomanip>Ten#include <climits> One#include <string.h> A#include <cmath> -#include <stdlib.h> -#include <vector> the#include <stack> - #defineINF 1000000007 - #defineMAXN 40010 - #defineMod 1000007 + #defineN 10010 - #defineNN 30 + #defineSigma_size 3 A Const intMAXN = 6e5 +Ten; at using namespacestd; -typedefLong LongLL; - - structBridge { - intu, v; -}bg[2*N]; in -vector<int>G[n]; to intVis[n], low[n], dfn[n], time; + intFa[n], deg[n]; - intN, M, CNT; the intu, v; * $ intFindset (intx)Panax Notoginseng { - returnFA[X] = fa[x] = = x?X:findset (fa[x]); the } + A voidInit () the { + for(inti =0; I <= N; ++i) { - g[i].clear (); $Fa[i] =i; $ } -memset (DFN,0,sizeof(DFN)); -memset (Low,0,sizeof(Low)); thememset (Vis,0,sizeof(Vis)); -memset (deg,0,sizeof(deg));WuyiCNT = time =0; the for(inti =0; I < m;++i) { -CIN >> U >>v; Wu G[u].push_back (v); - g[v].push_back (u); About } $ } - - voidTarjan (intUintfather) - { ALow[u] = Dfn[u] = + +Time ; +Vis[u] =1; the for(inti =0; I < g[u].size (); ++i) { - intv =G[u][i]; $ if(v = = father)Continue; the if(!Vis[v]) { the Tarjan (V, u); theLow[u] =min (Low[u], low[v]); the if(Low[v] > Dfn[u]) {//U->v for the bridge -BG[CNT].U =u; inBG[CNT].V =v; thecnt++; the } About Else{//otherwise the u,v belong to a connected component, merging the intFX =findset (u); the intFY =Findset (v); the if(FX! =fy) +FA[FX] =fy; - } the }Bayi Else { theLow[u] =min (low[u],dfn[v]); the } - } - } the the intMain () the { the while(Cin >> N >>m) { - init (); theTarjan (1,-1); the for(inti =0; I < CNT; ++i) { the intFX =Findset (bg[i].u);94 intFY =Findset (BG[I].V); thedeg[fx]++; thedeg[fy]++; the }98 intleaf =0; About for(inti =1; I <= N; ++i) - if(Deg[i] = =1) leaf++;101cout << (leaf +1) /2<<Endl;102 }103 //System ("pause");104 return 0; the}
UESTC Teacher and Farm