HDU4612:Warm up(缩点+树的直径)
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(缩点+树的直径)的更多相关文章
- hdu4612 Warm up 缩点+树的直径
题意抽象后为:给定一个无向图 问添加一条边的情况下最少能有多少个桥. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(2<=N<=200000),边数M(1<=M< ...
- HDU4612 Warm up 边双(重边)缩点+树的直径
题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...
- hdu 4612 Warm up 有重边缩点+树的直径
题目链接 Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tot ...
- hdu4612(双连通缩点+树的直径)
传送门:Warm up 题意:询问如何加一条边,使得剩下的桥的数目最少,输出数目. 分析:tarjan缩点后,重新建图得到一棵树,树上所有边都为桥,那么找出树的直径两个端点连上,必定减少的桥数量最多, ...
- hdu-4612(无向图缩点+树的直径)
题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- F - Warm up HDU - 4612 tarjan缩点 + 树的直径 + 对tajan的再次理解
题目链接:https://vjudge.net/contest/67418#problem/F 题目大意:给你一个图,让你加一条边,使得原图中的桥尽可能的小.(谢谢梁学长的帮忙) 我对重边,tarja ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- 【HDU 4612 Warm up】BCC 树的直径
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:一个包含n个节点m条边的无向连通图(无自环,可能有重边).求添加一条边后最少剩余的桥的数 ...
随机推荐
- 使用es6总结笔记
1. let.const 和 block 作用域 在ES6以前,var关键字声明变量.无论声明在何处,都会被视为声明在函数的最顶部(不在函数内即在全局作用域的最顶部). let 关键词声明的变量不具备 ...
- 最短路径算法(II)
什么??你问我为什么不在一篇文章写完所有方法?? Hmm…其实我是想的,但是博皮的加载速度再带上文章超长图片超多的话… 可能这辈子都打不开了吧… 上接https://www.cnblogs.com/U ...
- 创建https证书
第一个里程碑:创建https证书 创建文件认证目录 mkdir /application/nginx/key/ -p 在认证目录下创建认证文件 openssl req -new -x509 -node ...
- Alpha发布文案+美工
文案: Alpha发布文稿 我们是Hello World!团队,下面由我来简要介绍一下我们组的作品,我们组做的是一个飞机射击类游戏,名字叫做空天猎.这个游戏是基于JAVA平台创建的,那么接下来让我给大 ...
- Java学习个人备忘录之接口
abstract class AbsDemo { abstract void show1(); abstract void show2(); } 当一个抽象类中的方法都是抽象的时候,这时可以将该抽象类 ...
- 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解
. 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...
- python学习笔记06:操作文件
调用内置的open函数打开文件,传递两个参数:文件路径(绝对路径或相对路径),打开模式('r':读,'r+':读写,'w':写,'b':二进制): f = open('data.txt','w') f ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺-2
一.当天站立式会议照片: 二.项目进展 昨天已完成的工作: 微信公众号平台注册"小程序"的账号; 下载微信官方的小程序开发工具,这个是编辑小程序和上传审核小程序必须的工具; 大家一 ...
- 【Linux】- 文件基本属性
Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定. 在Linux中我们可 ...
- C#Color颜色表
Color.AliceBlue 240,248,255 Color.LightSalmon 255,160,122 Color.AntiqueWhite 250,235,215 Color.Light ...