The main idea: a graph that requires you to add a minimum of edges, making the last image of a side-double connected branch. The so-called side of the two connected branches, that is, there is no bridge Connectivity branch (the title guarantee data of any two points are connected).
Problem-solving ideas: First use the Tarjan algorithm to set up a DAG map, and then the search degree of 1 points there are number x, then the need to add the edge is (x+1)/2;
At first I wrote this, WA, and then found the following two data, found that can not be too.
#include <stdio.h>#include<Set>#include<vector>#include<string.h>#include<algorithm>using namespacestd;Const intN =1003; Vector<int>G[n];vector<pair<int,int> >DAG;intDfn[n], low[n], mk[n];inttot;intN, M;voidinit () {tot=0; Dag.clear (); for(intI=1; i<=n; ++i) {Mk[i]=0; G[i].clear (); Dfn[i]= Low[i] =-1; }}voidTarjan (intUintf) {Dfn[u]= Low[u] = + +tot; for(inti =0; I < g[u].size (); ++i) {intv =G[u][i]; if(Dfn[v] = =-1) {Tarjan (V, u); Low[u]=min (Low[u], low[v]); if(Dfn[u] <Low[v]) Dag.push_back (Make_pair (Low[u], low[v])); } Else if(V! =f) Low[u]=min (Low[u], dfn[v]); }}voidsolve () {init (); for(intI=1; i<=m; ++i) {intu, v; scanf ("%d%d", &u, &v); G[u].push_back (v); G[v].push_back (U); } Tarjan (1, -1); for(intI=0; I<dag.size (); ++i) {pair<int,int> S =Dag[i]; Mk[s.first]+ +, Mk[s.second] + +; } intAns =0; for(intI=1; i<=n; ++i) {if(Mk[i] = =1) ans++; } printf ("%d\n", (ans +1) /2);}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF) solve (); return 0;}View Code
Special attention needs to be paid to two sets of data:
2 2
1 2
1 2
2 1
1 2
The answers were:
0
1
The code is as follows:
#include <stdio.h>#include<Set>#include<vector>#include<string.h>#include<algorithm>using namespacestd;Const intN =1003; Vector<int>G[n];intDfn[n], low[n], mk[n];inttot;intN, M;voidinit () {tot=0; for(intI=1; i<=n; ++i) {Mk[i]=0; G[i].clear (); Dfn[i]= Low[i] =-1; }}voidTarjan (intUintf) {Dfn[u]= Low[u] = + +tot; for(inti =0; I < g[u].size (); ++i) {intv =G[u][i]; if(Dfn[v] = =-1) {Tarjan (V, u); Low[u]=min (Low[u], low[v]); } Else if(V! =f) Low[u]=min (Low[u], dfn[v]); }}voidsolve () {init (); for(intI=1; i<=m; ++i) {intu, v; scanf ("%d%d", &u, &v); G[u].push_back (v); G[v].push_back (U); } Tarjan (1, -1); for(inti =1; I <= N; ++i) { for(intj =0; J < G[i].size (); ++j) {if(Low[i]! =Low[g[i][j]]) Mk[low[i]++; } } intAns =0; for(inti =1; I <= N; ++i)if(Mk[i] = =1) ans++; printf ("%d\n", (ans +1) /2);}intMain () { while(SCANF ("%d%d", &n, &m)! =EOF) solve (); return 0;}View Code
POJ 3352-road Construction (graph theory-bilateral unicom branch algorithm)