Warm up

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612

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 <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 2e5+,M = 1e6+;
int n,m;
int head[N];
struct Edge{
int u,v,next;
bool operator < (const Edge &A){
if(u==A.u) return v<A.v;
return u<A.u;
}
}e[M<<],g[M<<];
int T,tot,cnt;
int dfn[N],low[N],cut[N],num[N],f[N];
void adde(int u,int v){
e[tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot++;
}
void init(){
T=;tot=;cnt=;
memset(head,-,sizeof(head));
memset(cut,,sizeof(cut));
memset(dfn,,sizeof(dfn));
memset(num,,sizeof(num));
for(int i=;i<=n+;i++) f[i]=i;
}
int find(int x){
return f[x]==x?f[x]:f[x]=find(f[x]);
}
void Union(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy) f[fx]=fy;
}
void Tarjan(int u,int pre){
dfn[u]=low[u]=++T;
int k=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(v==pre && !k){
k=;
continue ;
}
if(!dfn[v]){
Tarjan(v,u);
low[u]=min(low[u],low[v]);
}else{
low[u]=min(low[u],dfn[v]);
}
if(low[v]>dfn[u]){
cut[v]=;
}else Union(u,v);
}
}
int mx=,node=;
void dfs(int u,int d,int pa){
if(d>mx){
mx=d;
node=u;
}
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(v==pa) continue ;
dfs(v,d+,u);
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(!n&&!m) break ;
init();
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
adde(u,v);adde(v,u);
if(u>v)swap(u,v);
g[i].u=u;g[i].v=v;
}
sort(g+,g+m+);
Tarjan(,);
memset(head,-,sizeof(head));tot=;
for(int i=;i<=m;i++){
int u=g[i].u,v=g[i].v;
if(g[i].u==g[i-].u&&g[i].v==g[i-].v) continue ;
int fx=find(u),fy=find(v);
if(!num[fx]) num[fx]=++cnt;
if(!num[fy]) num[fy]=++cnt;
if(num[fx]==num[fy]) continue ;
adde(num[fx],num[fy]);adde(num[fy],num[fx]);
}
mx=;
dfs(,,-);
mx=;
dfs(node,,-);
cout<<cnt--mx<<endl;
}
return ;
}

HDU4612:Warm up(缩点+树的直径)的更多相关文章

  1. hdu4612 Warm up 缩点+树的直径

    题意抽象后为:给定一个无向图 问添加一条边的情况下最少能有多少个桥. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(2<=N<=200000),边数M(1<=M< ...

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

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

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

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

  4. hdu4612(双连通缩点+树的直径)

    传送门:Warm up 题意:询问如何加一条边,使得剩下的桥的数目最少,输出数目. 分析:tarjan缩点后,重新建图得到一棵树,树上所有边都为桥,那么找出树的直径两个端点连上,必定减少的桥数量最多, ...

  5. hdu-4612(无向图缩点+树的直径)

    题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...

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

    <题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...

  7. F - Warm up HDU - 4612 tarjan缩点 + 树的直径 + 对tajan的再次理解

    题目链接:https://vjudge.net/contest/67418#problem/F 题目大意:给你一个图,让你加一条边,使得原图中的桥尽可能的小.(谢谢梁学长的帮忙) 我对重边,tarja ...

  8. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  9. 【HDU 4612 Warm up】BCC 树的直径

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:一个包含n个节点m条边的无向连通图(无自环,可能有重边).求添加一条边后最少剩余的桥的数 ...

随机推荐

  1. CodeForces 838B Diverging Directions 兼【20180808模拟测试】t3

    描述 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点到达. 2. 接下来的 N-1 ...

  2. maven 安装、配置

    简介: maven 大大提高项目开发速度     编译---打包---测试--安装   一条龙 maven将项目构建的过程标准化,每一个阶段使用一个命令完成,下面是构建过程一些阶段 清理 mvn cl ...

  3. 官方文档 恢复备份指南一 Introduction to Backup and Recovery

    1.备份分为:物理备份和逻辑备份    物理备份:备份数据文件  控制文件  归档日志文件     逻辑备份:EXP EXPDP备份等 物理备份为主,逻辑做补充     2.错误的类型         ...

  4. Fafa and the Gates(模拟)

    Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens ...

  5. UVALive - 6868 Facility Locations 想法题

    题目链接: http://acm.hust.edu.cn/vjudge/problem/88634 Facility Locations Time Limit: 3000MS 题意 给你一个m*n的矩 ...

  6. 分页查询es时,返回的数据不是自己所期望的问题

    在进行es分页查询时,一般都是用sql语句转成es查询字符串,在项目中遇到过不少次返回的数据不是自己所期望的那样时,多半原因是自己的sql拼接的有问题. 解决办法:务必要保证自己的sql语句拼接正确.

  7. HTML页面垂直滚动条不见

    <body style="overflow-y:scroll;"> </body>

  8. intellij idea 如何将一个普通项目转换为maven项目

    1.工程文件下新建文件pom.xml,并填写好内容. 2.在pom.xml 文件上右键 Add as Maven Project.

  9. Android基础------通知栏

    前言:Android通知栏提示笔记 通知几乎是每一款app都拥有的功能 1.发送通知 发送一个通知栏必须用到两个类:  NotificationManager . Notification. Noti ...

  10. SpringBoot2.0(一) mybatis

    使用mybatis springboot使用mybatis主要依赖 mybatis-spring-boot-starter 来实现.其提供了2中解决方案,一种是使用注解:另一种是简化后的传统的xml方 ...