Warm up
Time limit:10000/5000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 4437 Accepted Submission (s): 1001
Problem Description N Planets is connected by M bidirectional channels the Allow instant transportation. It's always possible to travel between any and planets through these channels.
If We can isolate some planets from others to breaking only one channel, the channel is called a Bridge of the Transporta tion System.
People don ' t like is isolated. So they ask what ' s the minimal number of bridges they can has if they decide to build a new channel.
Note that there could is more than one channel between and one planets.
Input the input contains multiple cases.
Each case starts with positive integers N and M, indicating the number of planets and the number of channels.
(2<=n<=200000, 1<=m<=1000000)
Next M lines each contains-positive integers a and B, indicating a channel between planet A and B in the system. Planets is numbered by 1..N.
A line with the integers ' 0 ' terminates the input.
Output for each case, output the minimal number of bridges after building a new channel in a line.
Sample INPUT4 41 21 31 42 30 0
Sample Output0
AUTHORSYSU test instructions An image, now add an edge, so that the number of bridges is minimal and output. The first side to be added is definitely the two-point diameter of the tree, so we just need to ask for the total bridge-tree diameter.
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <string> #include <algorithm># include<queue> #include <vector> #include <set> #include <stack>using namespace std;vector< int> E[200005],mp[200005];int N,m,use[200005],top,newflag,dfs[200005],low[200005],isstack[200005];int dp[ 200005],vis[200005],ans,bridge;stack<int> s;void init () {ans=0,bridge=0; memset (use,0,sizeof (use)); memset (dfs,0,sizeof (DFS)); memset (low,0,sizeof (Low)); memset (isstack,0,sizeof (isstack)); top=newflag=0; while (!s.empty ()) S.pop (); for (int i=1;i<=n;i++) e[i].clear (), Mp[i].clear ();} int BFS (int x) {int pos; Pos=x; memset (vis,0,sizeof (VIS)); Memset (Dp,0,sizeof (DP)); Queue<int> Q; Q.push (x); Vis[x]=1; while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=0;i<Mp[u].size (); i++) {int v=mp[u][i]; if (Vis[v]) continue; Vis[v]=1; dp[v]=dp[u]+1; Q.push (v); if (Dp[v]>ans) {ans=dp[v]; Pos=v; }}} return pos;} void Tarjan (int u,int father) {dfs[u]=low[u]=++top; S.push (U); Isstack[u]=1; int cnt=0; for (int i=0;i<e[u].size (); i++) {int v=e[u][i]; if (! Dfs[v]) {Tarjan (v,u); Low[u]=min (Low[u],low[v]); if (Low[v]>dfs[u]) bridge++; } else if (V==father) {if (CNT) low[u]=min (Low[u],dfs[v]); cnt++; } else if (Isstack[v]) low[u]=min (Low[u],dfs[v]); } if (Low[u]==dfs[u]) {newflag++; int x; do {x=s.top (); S.pop (); isstack[x]=0; Use[x]=newflag; }while (X!=u); }}int Main () {WHILe (scanf ("%d%d", &n,&m)!=eof) {if (n==0&&m==0) break; Init (); for (int i=1;i<=m;i++) {int x, y; scanf ("%d%d", &x,&y); E[x].push_back (y); E[y].push_back (x); } for (int i=1;i<=n;i++) {if (! Dfs[i]) Tarjan (i,-1); } for (int i=1;i<=n;i++) {for (int j=0;j<e[i].size (); j + +) {int u, V U=I,V=E[I][J]; if (Use[u]!=use[v]) mp[use[u]].push_back (Use[v]); }} BFS (BFS (1)); printf ("%d\n", Bridge-ans); } return 0;}
(Side double Unicom + tree diameter) hdu 4612