http://acm.hdu.edu.cn/showproblem.php?pid=4612

题目大意:求加一条边最小的桥数

先用Tarjin缩点求出一棵树,然后用bfs求出树的直径,树的直径就是加一条边桥最多的呢条边。

最后就用桥减去直径就行了

Warm up

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

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 4
1 2
1 3
1 4
2 3
0 0
 
Sample Output
0
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stack>
#include<queue>
#include<cstdlib> using namespace std;
const int N=;
struct node
{
int next,to;
}edge[N*],Edge[N*];
int Stack[N],low[N],dfn[N],belong[N],sum,Time,top;
int Head[N],head[N],ans,Ans,Max,dis[N],vis[N],End; void Inn()
{
memset(Stack,,sizeof(Stack));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(Head,-,sizeof(Head));
memset(head,-,sizeof(head));
memset(belong,,sizeof(belong));
memset(dis,,sizeof(dis));
sum=Time=top=ans=Ans=Max=End=;
} void add(int from,int to)
{
edge[ans].to=to;
edge[ans].next=head[from];
head[from]=ans++;
} void Add(int from,int to)
{
Edge[Ans].to=to;
Edge[Ans].next=Head[from];
Head[from]=Ans++;
} void Tarjin(int u,int f)
{
int k=,v;
low[u]=dfn[u]=++Time;
Stack[top++]=u;
for(int i=head[u];i!=-;i=edge[i].next)
{
v=edge[i].to;
if(v==f && !k)
{
k++;
continue;
}
if(!dfn[v])
{
Tarjin(v,u);
low[u]=min(low[u],low[v]);
}
else
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
sum++;
do
{
v=Stack[--top];
belong[v]=sum;
}while(v!=u);
}
} void dfs(int s)
{
queue<int>Q;
int u,v;
memset(vis,,sizeof(vis));
dis[s]=;
vis[s]=;
Q.push(s);
while(Q.size())
{
u=Q.front();
Q.pop();
for(int i=Head[u];i!=-;i=Edge[i].next)
{
v=Edge[i].to;
if(!vis[v])
{
vis[v]=;
dis[v]=dis[u]+;
Q.push(v);
if(Max<dis[v])
{
Max=dis[v];
End=v;
}
}
}
}
} void slove(int n)
{
int SUM=;
Tarjin(,);
for(int i=;i<=n;i++)
{
for(int j=head[i];j!=-;j=edge[j].next)
{
int u=belong[i];
int v=belong[edge[j].to];
if(u!=v)
{
Add(u,v);
SUM++;
}
}
}
SUM/=;
dfs();
dfs(End);
printf("%d\n",SUM-Max);
}
int main()
{
int n,m,a,b;
while(scanf("%d %d",&n,&m),n+m)
{
Inn();
while(m--)
{
scanf("%d %d",&a,&b);
add(a,b);
add(b,a);
}
slove(n);
}
return ;
}

Warm up-HUD4612(树的直径+Tarjin缩点)的更多相关文章

  1. Hdu 4612 Warm up (双连通分支+树的直径)

    题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...

  2. HDU 4612 Warm up tarjan 树的直径

    Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...

  3. hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  4. hdu 4612 Warm up 有重边缩点+树的直径

    题目链接 Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tot ...

  5. HDU4612:Warm up(缩点+树的直径)

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  6. HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边

    Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...

  7. HDU4612 Warm up 边双(重边)缩点+树的直径

    题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...

  8. F - Warm up - hdu 4612(缩点+求树的直径)

    题意:有一个无向连通图,现在问添加一条边后最少还有几个桥 分析:先把图缩点,然后重构图为一棵树,求出来树的直径即可,不过注意会有重边,构树的时候注意一下 *********************** ...

  9. HDU 4612 Warm up(双连通分量缩点+求树的直径)

    思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...

随机推荐

  1. AJPFX总结泛型概念和使用

    泛型泛型(generic)概述和基本使用                泛型把明确数据类型的操作放到创建对象或者调用方法的时候再明确.                                J ...

  2. vue-router: $router.push遇到的问题

    如下图使用path时,跳转路由后的页面this.$route.params为undefined 改为name时,this.$route.params可以正确获取数据,如下图: 此为官方定义的固定搭配: ...

  3. 从单机到2000万 QPS 并发的 Redis 高性能缓存实践之路

    1.引言 知乎存储平台团队基于开源Redis 组件打造的知乎 Redis 平台,经过不断的研发迭代,目前已经形成了一整套完整自动化运维服务体系,提供很多强大的功能.本文作者陈鹏是该系统的负责人,本次文 ...

  4. (转)淘淘商城系列——发布dubbo服务

    http://blog.csdn.net/yerenyuan_pku/article/details/72758639 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入, ...

  5. Linux 控制终端转义和控制序列

    DESCRIPTION 描述 Linux控制台实现了VT102和ECMA-48/ISO 6429/ANSI X3.64终端控制的子集, 这个子集很庞大,当然还有Linux自己私有的控制序列,以改变颜色 ...

  6. apm - 查询高级电源管理(APM) BIOS

    总览 apm [ - vVmsS ] 描述 apm 读取 /proc/apm 并用人能看懂的格式输出.因为提供了首要的电池状态,这个命令在有兼容的 APM BIOS 的笔记本电脑上非常有用. apm ...

  7. C++运行外部exe并判断exe返回值

    有三个API函数可以运行可执行文件WinExec.ShellExecute和CreateProcess.CreateProcess因为使用复杂,比较少用. WinExec主要运行EXE文件. ⑴ 函数 ...

  8. 【转】UpdateData()函数

    一.总结UpdateData()函数 UpdateData(true);//用于将屏幕上控件中的数据交换到变量中. UpdateData(false);//用于将数据在屏幕中对应控件中显示出来. 当你 ...

  9. 第1节 yarn:15、关于yarn中常用的参数设置

    第一个参数:container分配最小内存 yarn.scheduler.minimum-allocation-mb     1024   给应用程序container分配的最小内存 第二个参数:co ...

  10. SQL Server中 sysobjects、sysolumns、systypes

    1.sysobjects    系统对象表. 保存当前数据库的对象,如约束.默认值.日志.规则.存储过程等 在大多数情况下,对你最有用的两个列是Sysobjects.name和Sysobjects.x ...