HDU 4612 Warm up(边双联通求树的直径)
来源:互联网 发布:tv365网络电视安卓版 编辑:程序博客网 时间:2024/04/29 23:23
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
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;}
0 0
- HDU 4612 Warm up(边双联通求树的直径)
- HDU 4612 Warm up (边双联通,树的直径)
- HDU 4612 Warm up (树的直径 + 双联通)
- HDU 4612 Warm up (强连通,求树的直径)
- HDU 4612 Warm up 边双连通+树的直径
- HDU 4612 Warm up(边双联通+直径【手动扩栈】)
- HDOJ 题目4612Warm up(双联通求桥数,缩点求树的直径)
- HDU 4612 Warm up(边双连通分量+树的直径)
- HDU 4612 Warm up(边双连通、树的直径)
- hdu 4612 Warm up 双连通缩点+树的直径
- Hdu 4612 Warm up (双连通缩点+树的直径)
- hdu 4612 Warm up(缩点+树直径)
- hdu 4612 Warm up (边双联通分量缩点)
- HDU 4612 Warm up 边双联通分量,树宽
- hdu 4612 Warm up (带有重边的无向图Tarjan+树的直径)
- hdu 4612 Warm up(边-双连通+缩点+树的直径)
- hdu 4612 Warm up 边双连通分量+树的直径
- HDU 4612 Warm up (缩点+树的直径,有重边)
- web入门
- 面向对象原则(3)(4):依赖倒转原则与里氏代换原则
- 【树形dp】Long Live the Queen
- 程序人生之项目团队那些人与事(8)
- 网线的交叉线和直通线原理
- HDU 4612 Warm up(边双联通求树的直径)
- 海量数据处理算法—Bloom Filter
- setClickable(false)不起作用
- sgu203:Hyperhuffman(哈夫曼编码)
- 杭电2028
- 【USACO]】milk2
- 逻辑回归(logistic regression)
- VS2010问题 ->fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- CentOS 6.5 yum安装配置lnmp服务器(Nginx+PHP+MySQL)