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 Input
Sample Output
0
Source2013 multi-university Training Contest 2Test Instructions: The minimum number of bridges after an edge is added. The diameter of the edge double-link shrinking point is the diameter of the tree, and the maximum number of bridges is reduced. Heavy edge problem: Pay attention to the heavy edges. And there is to expand the stack!!
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <cstring> #include < algorithm> #include <vector> #include <string> #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack> #include <bitset>using namespace std; #define REPF (I, A, b ) for (int i = A; I <= B; + + i) #define REP (i, n) for (int i = 0; i < n; + + i) #define CLEAR (A, x) Memse T (A, X, sizeof a) typedef long long ll;typedef pair<int,int>pil;const int maxn=200000+100;const int maxm=3000000 +100;struct node{int to,next; BOOL col;//for bridge}e[maxm],e1[maxm];int head[maxn],cnt,cnte,cnte;int Dfn[maxn],low[maxn];int s[maxn],instack[maxn];int Idex,top,bridge;int Belong[maxn],h[maxn];int d[maxn],vis[maxn];//for cut; add unicom fast int n,m;void init () {cnt=top=idex=0] after deletion point; cnte=bridge=cnte=0; CLEAR (head,-1); CLEAR (dfn,0); CLEAR (low,0); CLEAR (instack,0); CLEAR (belong,0); CLEAR (h,-1); CLEAR (Vis,0); CLEAR (d,0);} void Addedge (int u,int v) {E[cnte].to=v;e[cnte].next=head[u]; e[cnte].col=false;head[u]=cnte++;} void Addedge1 (int u,int v) {E1[cnte].to=v;e1[cnte].next=h[u]; h[u]=cnte++;} void Tarjan (int u,int pre) {int V; Low[u]=dfn[u]=++idex; S[top++]=u; Instack[u]=1; int pre_num=0; for (int i=head[u];i!=-1;i=e[i].next) {v=e[i].to; if (v==pre&&!pre_num) {pre_num++; Continue } if (! Dfn[v]) {Tarjan (v,u); if (Low[u]>low[v]) low[u]=low[v]; if (Low[v]>dfn[u])//bridge {bridge++; E[i].col=true; E[i^1].col=true; }} else if (Instack[v]&&low[u]>dfn[v]) low[u]=dfn[v]; } if (Low[u]==dfn[u]) {cnt++; do{V=s[--top]; instack[v]=0; belong[v]=cnt; }while (V!=u); }}void dfs (int u,int depth) {D[u]=depth; Vis[u]=1; for (int i=h[u];i!=-1;i=e1[i].next) {int v=e1[i].to; if (!vis[v]) DFS (v,depth+1); }}void work () {REPF (i,1,n) if (! Dfn[i]) Tarjan (i,-1); REPF (u,1,n) for (int i=head[u];i!=-1;i=e[i].next) {int v=e[i].to; if (Belong[u]!=belong[v]) {addedge1 (belong[u],belong[v]); Addedge1 (Belong[v],belong[u]); }} dfs (1,0); int Pos=1; for (int i=1;i<=cnt;i++) if (D[i]>d[pos]) pos=i; CLEAR (vis,0); CLEAR (d,0); DFS (pos,0); int ans=0; REPF (i,1,cnt) Ans=max (Ans,d[i]); printf ("%d\n", Bridge-ans);} int main () {int u,v; while (~SCANF ("%d%d", &n,&m) && (n+m)) {init (); REPF (i,1,m) {scanf ("%d%d", &u,&v); if (u==v) continue; Addedge (U,V); Addedge (V,u); } work (); } return 0;}
HDU 4612 Warm up (side double link for tree diameter)