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. 微信官方UI库—WeUI

    WeUI 为微信 Web 服务量身设计 概述 WeUI是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信 Web 开发量身设计,可以令用户的使用感知更加统一.包含button.cell ...

  2. BotFramework学习-02

    1.请求的Message格式 { "type": "Message", "id": "fd89606f8014453ca5587e ...

  3. 深度神经网络简述与Capsule介绍

    本人最近初学Hinton大神的论文<Dynamic Routing Between Capsules >,对深度神经网络的内容进行了简要总结,将观看“从传统神经网络的角度解读Capsule ...

  4. java关于时间的相关操作

    /** * 获取当天时间零点 * @return */ public Date gettoday(){ SimpleDateFormat sdf = new SimpleDateFormat(&quo ...

  5. 21反射、动态代理、枚举、Filter

    2018/10/08 1.反射 Constructor Class类的newInstance()方法是使用该类无参的构造函数创建对象, 如果一个类没有无参的构造函数, 就不能这样创建了,可以调用Cla ...

  6. [Algorithm] 1. A+B Problem

    Description Write a function that add two numbers A and B. Clarification Are a and b both 32-bit int ...

  7. 梦回----32位CPU和64位CPU的通用寄存器

    1 32位Intel的CPU通用寄存器 32位CPU所含有的寄存器有:4个数据寄存器(EAX.EBX.ECX和EDX):2个变址和指针寄存器(ESI和EDI):2个指针寄存器(ESP和EBP):6个段 ...

  8. Jquery validate自定义验证

    http://www.runoob.com/jquery/jquery-plugin-validate.html addMethod(name,method,message)方法 参数 name 是添 ...

  9. 单线制DS18B20温度传感器LED数码管显示当前的温度值

    /******************************************* 程序功能:利用单线制DS18B20温度传感器 LED数码管显示当前的温度值 ***************** ...

  10. HUAS Summer Contest#4 D题 DP

    Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的 ...