【HDU461】【tarjan】【桥】【树的直径】

来源:互联网 发布:剑灵灵男捏脸详细数据 编辑:程序博客网 时间:2024/04/29 13:05

Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4971    Accepted Submission(s): 1105


Problem Description
  N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
  If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
  Note that there could be more than one channel between two planets.
 

Input
  The input contains multiple cases.
  Each case starts with two 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 two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
  A line with two 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
4 41 21 31 42 30 0
 

Sample Output
0
 

Author
SYSU
 

Source
2013 Multi-University Training Contest 2
 

Recommend
zhuyuanchen520




#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <assert.h>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000")#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define mp push_backint n,m;const int N = 200100;const int M = 3000010;struct Edge{int to,next,id;}edge[M];Edge edge2[M];int H[N],H2[N];int myblock[N];int vis[N];int low[N];int myst[N];int inst[N];int dist[N];int ok[N];int times;int len;int len2;int top;int sccnum;int maxs;int end_p;int brinum;void addedge(int u,int v,int id){edge[len].to = v;edge[len].id = id;edge[len].next = H[u];H[u] = len++;}void addedge2(int u,int v){edge2[len2].to = v;edge2[len2].next = H2[u];H2[u] = len2++;}void init(){assert(sizeof(H) >= 5);len = 0;len2 = 0;times = 0;top = 0;sccnum = 0;maxs = 0;brinum = 0;memset(inst,0,sizeof(inst));memset(myst,0,sizeof(myst));memset(H,-1,sizeof(H));memset(H2,-1,sizeof(H2));memset(vis,-1,sizeof(vis));memset(low,0,sizeof(low));memset(ok,0,sizeof(ok));memset(myblock,0,sizeof(myblock));}void tarjan(int u,int ei){vis[u] = low[u] = ++times;inst[u] = true;myst[top++] = u;for(int i=H[u];~i;i=edge[i].next){int v = edge[i].to;if(ei == edge[i].id) continue;if(vis[v] == -1){tarjan(v,edge[i].id);low[u] = min(low[u],low[v]);if(low[v] > vis[u]) brinum ++;}else if(inst[v]){low[u] = min(low[u],vis[v]);}}int t;if(low[u] == vis[u]){sccnum ++;do{t = myst[--top];inst[t] = false;myblock[t] = sccnum;} while(t != u);}}void bfs(int st){queue<int> q;while(!q.empty()) q.pop();memset(ok,0,sizeof(ok));dist[st] = 0;ok[st] = true;q.push(st);maxs = 0;while(!q.empty()){int now = q.front();q.pop();for(int ei=H2[now];~ei;ei=edge2[ei].next){int v = edge2[ei].to;if(ok[v]) continue;ok[v] = true;q.push(v);dist[v] = dist[now] + 1;if(dist[v] > maxs){maxs = dist[v];end_p = v;}}}}void solve(int n){for(int i=1;i<=n;i++){if(vis[i] == -1)tarjan(i,-1);}for(int u=1;u<=n;u++){for(int i=H[u];~i;i=edge[i].next){int v = edge[i].to;if(myblock[u] != myblock[v]){addedge2(myblock[u],myblock[v]); // 之前加了双向边的.}}}bfs(1);bfs(end_p);printf("%d\n",brinum - maxs);}int main(){while(scanf("%d%d",&n,&m) ){if(n == 0 && m == 0) break;init();for(int i=1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);addedge(u,v,i);addedge(v,u,i);}solve(n);}return 0;}


0 0