【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.
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.
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
- 【HDU461】【tarjan】【桥】【树的直径】
- HDU 4612 and 4607 (tarjan求桥+树形dp求树的直径)
- Warm up 【tarjan 求EBC+求桥+缩点+树的直径】
- hdu 4612 Warm up (带有重边的无向图Tarjan+树的直径)
- hdu 4612 Warm up(无向图Tarjan+树的直径)
- hdu 4612 Warm up(无向图Tarjan+树的直径)
- bzoj3124 [Sdoi2013]直径 树的直径
- 【树的直径】
- 树的直径
- 求树的直径
- 树的最长直径
- HDU4607 树的直径
- URAL1056(树的直径)
- 树的直径hdu3721
- 树的直径
- 树的直径
- 树的重心、直径
- 树的直径
- 【HDU1233】【最小生成树水题】
- CLOCK_MONOTONIC与CLOCK_REALTIME区别
- 【HDU1875】【最小生成树水题】
- 服务器自动切换
- 【POJ1287】【最小生成树水题】
- 【HDU461】【tarjan】【桥】【树的直径】
- mysql 错误注意事项
- spring的aop配置
- 英语总结系列(八):回顾八月展望九月
- websocket+webrtc+tomcat 实现视频监考功能
- UCI提供给shell和lua使用的配置接口
- android 支付简谈
- 2015 Multi-University Training Contest 10(hdu 5406 - hdu 5416)
- android 动画