hdu-4612(无向图缩点+树的直径)
题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么
解题思路:无向图缩点和树的直径,用并查集缩点;
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxm=;
const int maxn=;
struct Edge
{
int next;int id;int to;
}edge[maxm];
int head[maxn],dist[maxn],f[maxn],low[maxn],dfn[maxn],visit[maxn];
int n,m,cnt,ans,step;
void init()
{
memset(dist,,sizeof(dist));
memset(visit,,sizeof(visit));
memset(head,-,sizeof(head));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
for(int i=;i<=n;i++)
f[i]=i;
step=ans=cnt=;
}
int findf(int u)
{
if(u==f[u])
return u;
else
{
f[u]=findf(f[u]);
return f[u];
}
}
void join(int u,int v)
{
int t1=findf(u);int t2=findf(v);
if(t1!=t2)
f[t2]=t1;
}
void add(int u,int v,int id)
{
edge[cnt].next=head[u];edge[cnt].to=v;edge[cnt].id=id;head[u]=cnt++;
edge[cnt].next=head[v];edge[cnt].to=u;edge[cnt].id=id;head[v]=cnt++;
}
void tarjan(int u,int fa)
{
low[u]=dfn[u]=++step;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;int id=edge[i].id;
if(fa==id)
continue;
if(!dfn[v])
{
tarjan(v,id);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])
{
ans++;
}
else
{
join(u,v);
}
}
else
{
low[u]=min(low[u],dfn[v]);
}
}
}
void bfs(int u)
{
visit[u]=;dist[u]=;
queue<int>q;
q.push(u);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(visit[v])
continue;
if(findf(x)==findf(v))
{
q.push(v);dist[v]=dist[x];visit[v]=;
}
else
{
q.push(v);dist[v]=dist[x]+;visit[v]=;
}
}
}
}
int main()
{
int x,y;
while(scanf("%d%d",&n,&m)&&n&&m)
{
init();
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x,y,i);
}
tarjan(,);
bfs();
int maxx=;int pos=;
for(int i=;i<=n;i++)
{
if(maxx<dist[i])
{
maxx=dist[i];pos=i;
}
}
memset(visit,,sizeof(visit));memset(dist,,sizeof(dist));
bfs(pos);
maxx=;
for(int i=;i<=n;i++)
{
if(maxx<dist[i])
maxx=dist[i];
}
printf("%d\n",ans-maxx);
}
}
hdu-4612(无向图缩点+树的直径)的更多相关文章
- F - Warm up HDU - 4612 tarjan缩点 + 树的直径 + 对tajan的再次理解
题目链接:https://vjudge.net/contest/67418#problem/F 题目大意:给你一个图,让你加一条边,使得原图中的桥尽可能的小.(谢谢梁学长的帮忙) 我对重边,tarja ...
- Hdu 4612 Warm up (双连通分支+树的直径)
题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...
- HDU 4612 Warm up tarjan 树的直径
Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...
- hdu 4612 边连通度缩点+树的最长路径
思路:将以桥为分界的所有连通分支进行缩点,得到一颗树,求出树的直径.再用树上的点减去直径,再减一 #pragma comment(linker, "/STACK:1024000000,102 ...
- CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)
Your friend is developing a computer game. He has already decided how the game world should look lik ...
- hdu 4612 Warm up 有重边缩点+树的直径
题目链接 Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tot ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- HDU4612:Warm up(缩点+树的直径)
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- HDU4612 Warm up 边双(重边)缩点+树的直径
题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...
随机推荐
- [Javascript] js的类和对象
类 graph LR 类-->构造函数 类-->prototype对象 类-->instanceof运算符 类-->constructor属性 类-->isPrototy ...
- 用tornado实现图片标记
背景介绍 在文章Keras入门(四)之利用CNN模型轻松破解网站验证码中,其中的验证码图片标记是采用tornado实现的网页实现的.本文将会讲述如何利用tornado来实现图片标记. 我们的示 ...
- [翻译]在Windows版或MacOS版的Microsoft Edge上安装一个谷歌浏览器拓展
原文:Install a Chrome Web Store extension on Microsoft Edge for Windows and MacOS 拓展阅读:What to expect ...
- 001. Java内存中的字符编码
Java内存中的字符编码 Unicode字符集及utf-8 .utf-16.utf-32 等字符编码方式 字符集:字符表示的数字集合,元素称为码点或码位: 字符编码:字符实际的储存表示: 码点:一个码 ...
- spring boot整合Hadoop
最近需要用spring boot + mybatis整合hadoop,其中也有碰到一些坑,记录下来方便后面的人少走些弯路. 背景呢是因为需要在 web 中上传文件到 hdfs ,所以需要在spring ...
- [20190423]简单测试latch nowilling等待模式.txt
[20190423]简单测试latch nowilling等待模式.txt --//我对这个问题的理解就是如果参数willing=0,表示无法获取该latch,直接退出,再寻找类似的latch.--/ ...
- 如何将外部数据库 导入到系统的SQL中
打开数据库sql管理 在数据库中新建查询 如何输入: exec sp_attach_db @dbname='YourDataBaseName', @filename1='mdf文件路径', @fi ...
- C# 内插字符串与字符串复合格式
var name = "Tom"; ; string aa = string.Format("name:{0},age:{1}", name, age);//字 ...
- Oracle函数——日期函数
Oracle中的时间类型只有date和TIMESTAMP,TIMESTAMP是比date更精确的类型.日期时间函数用于处理时间类型的数据,Oracle以7位数字格式来存放日期数据,包括世纪.年.月.日 ...
- androidkiller连接模拟器并修改源码调试
首先需要连接模拟器,首先在模拟器的bin目录下运行命令:nox_adb.exe connect 127.0.0.1:62001(可以disconnect关闭): 之后在androidkiller的bi ...