tarjan算法求缩点+树形DP求直径
来源:互联网 发布:免费网管软件推荐 编辑:程序博客网 时间:2024/04/27 22:34
hdu4612
Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3184 Accepted Submission(s): 720
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
给定一个联通图,问加入一条边后,最少还余下多少个割边
分析:先求强连通分量个数num,然后缩点形成一棵树,再求树的直径cnt,答案就是num-1-cnt;
程序:
#pragma comment(linker, "/STACK:10240000000000,10240000000000")#include"stdio.h"#include"string.h"#include"stdlib.h"#include"stack"#include"iostream"#define M 201009#define inf 99999999using namespace std;stack<int>q;struct st{ int u,v,w,next;}edge[M*10];int head[M],use[M],t,dis[M][3],in[M],index,num,belong[M],dfn[M],low[M];void init(){ t=0; memset(head,-1,sizeof(head));}void add(int u,int v,int w){ edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].next=head[u]; head[u]=t++;}void tarjan(int u,int id){ dfn[u]=low[u]=++index; q.push(u); use[u]=1; int i; for(i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(i==(id^1))continue; if(!dfn[v]) { tarjan(v,i); low[u]=min(low[u],low[v]); } low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { int vv; num++; do { vv=q.top(); q.pop(); belong[vv]=num; use[vv]=0; }while(vv!=u); }}void dfs(int u){ use[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!use[v]) { dfs(v); //更新最大值和次大值 if(dis[u][0]<dis[v][0]+edge[i].w) { int tt=dis[u][0]; dis[u][0]=dis[v][0]+edge[i].w; dis[u][1]=tt; } else if(dis[u][1]<dis[v][0]+edge[i].w) dis[u][1]=dis[v][0]+edge[i].w; } } if(in[u]==1&&u!=1)//注意 dis[u][0]=dis[u][1]=0;}void solve(int n){ index=num=0; memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(use,0,sizeof(use)); memset(belong,0,sizeof(belong)); tarjan(1,-1);}int uu[M],vv[M];int main(){ int n,m,i; while(scanf("%d%d",&n,&m),m||n) { init(); while(m--) { int a,b; scanf("%d%d",&a,&b); add(a,b,1); add(b,a,1); } solve(n); int cnt=0; for(i=0;i<t;i+=2) { int u=edge[i].u; int v=edge[i].v; if(belong[u]!=belong[v]) { uu[cnt]=belong[u]; vv[cnt]=belong[v]; cnt++; } } init(); memset(in,0,sizeof(in)); memset(use,0,sizeof(use)); memset(dis,0,sizeof(dis)); for(i=0;i<cnt;i++) { //printf("%d %d\n",uu[i],vv[i]); add(uu[i],vv[i],1); add(vv[i],uu[i],1); in[uu[i]]++; in[vv[i]]++; } dfs(1); int ans=0; for(i=1;i<=num;i++) { if(ans<dis[i][0]+dis[i][1]) ans=dis[i][1]+dis[i][0]; } printf("%d\n",num-1-ans); } return 0;}
0 0
- tarjan算法求缩点+树形DP求直径
- HDU 4612 and 4607 (tarjan求桥+树形dp求树的直径)
- 树形DP求树的直径
- 【poj1985】Cow Marathon 树形DP求树的直径
- hdu2196(求树的直径或树形dp)
- bzoj3124 [Sdoi2013]直径 直径+树形dp
- 【解题报告】HDU 4679 Terrorist’s destroy -- 树形dp 删一边求两子树直径
- 【NOI2003T4】逃学的小孩-贪心+树形DP求直径+倍增LCA
- CCF CSP 201503-4 网络延时 (求树的直径-树形DP)
- hdu4612Warm up tarjan+树形dp
- bzoj2427-tarjan+树形dp+背包
- 树形dp 基环树直径 bzoj1791 ioi2008island
- hdu 4607 树形dp 树的直径
- hdu2196 树的直径或者树形dp
- [BZOJ3124][Sdoi2013]直径(树形dp)
- hdu2196-树形dp|树的直径-Computer
- [BZOJ3124][Sdoi2013]直径(树形dp)
- tarjan算法求桥
- 【JavaWeb】基础知识总结01 准备知识
- [LeetCode]Linked List Cycle
- Spring MVC list的一点笔记
- oracle 数据被删除闪回操作
- ubuntu安装 jdk-7u10-linux-i586.gz
- tarjan算法求缩点+树形DP求直径
- Ubuntu搭建LAMP环境
- 分治、动态规划、贪婪 之 算法分析
- 文法的类型
- Android 的Margin和Padding属性以及支持的长度单位
- VC的「附加依赖项」、「库目录」和「包含目录」分别有什么用?
- 快速在centos下用yum搭建svn服务
- Android系统五大布局详解Layout
- JAVA默认排序算法问题