HDU 4612 Warm up(边双联通求树的直径)
2015-01-29 20:23
309 查看
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
Sample Output
Source
2013 Multi-University Training Contest 2
题意:加入一条边后的最小桥数。
边双联通缩点求树的直径,将直径两端连接就是减少的最多桥数。
重边问题:注意重边。又有就是要扩栈!!
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 4 1 2 1 3 1 4 2 3 0 0
Sample Output
0
Source
2013 Multi-University Training Contest 2
题意:加入一条边后的最小桥数。
边双联通缩点求树的直径,将直径两端连接就是减少的最多桥数。
重边问题:注意重边。又有就是要扩栈!!
#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 ) memset ( 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;//为桥 }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];//为割;删除点后增加的联通快 int n,m; void init() { cnt=top=idex=0; 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++; 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(边双联通+直径【手动扩栈】)
- HDU 4612 Warm up (边双联通,树的直径)
- HDU 4612 Warm up (树的直径 + 双联通)
- Warm up HDU - 4612 无向图缩点+树的直径
- Hdu 4612 Warm up (双连通分支+树的直径)
- Hdu 4612 Warm up (双连通缩点+树的直径)
- Warm up HDU - 4612 无向图缩点+树的直径
- Warm up HDU - 4612 无向图缩点+树的直径
- Warm up HDU - 4612 无向图缩点+树的直径
- HDU 4612 Warm up(边双连通分量+树的直径)
- hdu 4612 Warm up (边双联通分量缩点)
- hdu 4612 Warm up(边-双连通+缩点+树的直径)
- HDU 4612 Warm up(边双连通、树的直径)
- HDU 4612——Warm up——————【边双连通分量、树的直径】
- [kuangbin带你飞]专题九 连通图 F - Warm up HDU - 4612 缩点+树的直径
- Warm up HDU - 4612 无向图缩点+树的直径
- Warm up HDU - 4612 无向图缩点+树的直径
- hdu 4612 Warm up (带有重边的无向图Tarjan+树的直径)
- Warm up HDU - 4612 无向图缩点+树的直径
- Warm up HDU - 4612 无向图缩点+树的直径