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. js内置对象总结

    在js里,一切皆为或者皆可以被用作对象.可通过new一个对象或者直接以字面量形式创建变量(如var i="aaa"),所有变量都有对象的性质. 注意:通过字面量创建的对象在调用属性 ...

  2. Android常规布局(网络异常布局、空数据布局,未登录布局等)切换工具类,Layout切换

    本人已整理好发布到github,已优化. github地址:https://github.com/buhuiming/StatusLayoutManager 使用:compile 'com.bhm.s ...

  3. window下phpstudy开启redis扩展

    注:一定要注意自己PHP的版本结构是64还是32位的!其次查看PHP Extension Build是NTS or TS! 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本(特 ...

  4. MySQL学习随笔--视图

    视图概念 数据库中的视图指的是一个虚拟表,其内容由查询定义.同真实的表一样,视图也是由行与列构成的.视图的数据来源由SQL语句查询得到,不存储数据 视图创建方法 格式 : create view 视图 ...

  5. JavaScript——responseType

    https://www.cnblogs.com/cdemo/p/5225848.html https://blog.csdn.net/wkyseo/article/details/78232485 异 ...

  6. mysql 插入多条记录,重复值不插入

    只去除主键与唯一索引的字段,字段为null时 是可以重复插入的domo: insert ignore into table_name(email,phone,user_id) values('test ...

  7. 【译】x86程序员手册34-9.7错误代码

    9.7 Error Code 错误代码 With exceptions that relate to a specific segment, the processor pushes an error ...

  8. iOS微信页面 长按图片出现【存储图像】和【拷贝】不出现【发送朋友】【保存图片】

    最近遇到一大坑.微信加载的页面中出现图片,长按图片时不出现默认的菜单[发送朋友]等而是[存储图像]和拷贝. 原因:正常在页面中长按图片是没有问题的,但是如果你的页面嵌入了ifram然后又长按在ifra ...

  9. linux 配置Java、Mysql、Tomcat、Redis开发环境

    1.安装四个依赖 以下四个依赖必须按顺序联网安装:yum install glibc.i686yum -y install libaio.so.1 libgcc_s.so.1 libstdc++.so ...

  10. badblocks - 查询设备的坏区块

    语法(SYNPSIS) badblocks [ -svwnf ] [ -b block-size ] [ -c blocks_at_once ] [ -i input_file ] [ -o outp ...