HDU 4612 Warm up tarjan缩环+求最长链
Warm up
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.
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.
1 2
1 3
1 4
2 3
0 0
题意:
给你一个n点m条边的无向图,你可以连接任意两个点形成新边,求连边后最小的桥的数量
题解:
最直接的思路就是先缩环构成一个新的图
要让桥的数量最少
显然连接新图中链最长的两个点
图论的基础题
但是注意 求最长链dfs会超时,尽量用bfs
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std; const int N = 2e6+, M = 2e6+, inf = 2e9, mod = 1e9+; int n,m,dfn[N],low[N],top,vis[N],cnt,from,head[N],t,xx[N],yy[N],q[N],inq[N],mx,belong[N],scc,all; struct ss{int to,next,id;}G[M];
void add(int u,int v) {G[t].next=head[u];G[t].to=v;G[t].id = ;head[u]=t++;} void init()
{
memset(head,-,sizeof(head));
scc=;cnt=;top=;
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
t=;
} void dfs(int x,int fa) {
dfn[x] = low[x] = ++cnt;
q[++top] = x;
inq[x]=;
for(int i=head[x];i!=-;i=G[i].next) {
int to = G[i].to;
if(G[i].id) continue;
G[i].id = G[i^].id=;
if(!dfn[to]) {
dfs(to,x);
low[x] = min(low[x],low[to]);
}
else if(inq[to])low[x] = min(low[x],dfn[to]);
}
if(low[x]==dfn[x]) {
scc++;
do {
inq[q[top]]=;
belong[q[top]]=scc;
}while(x!=q[top--]);
}
} void Tarjan() {
for(int i=;i<=n;i++)
if(!dfn[i]) dfs(i,-);
} void rebuild()
{
t=;
memset(head,-,sizeof(head));
for(int i=;i<=m;i++)
{
int x = xx[i];
int y = yy[i];
if(belong[x]==belong[y]) continue;
add(belong[x],belong[y]);
add(belong[y],belong[x]);
// cout<<"dsadas "<<belong[x]<<" "<<belong[y]<<endl;
}
} void bfs(int x)
{
memset(vis,,sizeof(vis));
queue<int >Q;
Q.push(x);
vis[x]=;
mx = ;
while(!Q.empty())
{
int k = Q.front();
Q.pop();
for(int i=head[k];i!=-;i=G[i].next)
{
int to = G[i].to;
if(vis[to]) continue;
Q.push(to);
vis[to] = vis[k] + ;
if(vis[to]>mx) { mx = vis[to];
from = to;
}
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
init();
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
xx[i]=a;
yy[i]=b;
}
Tarjan();
rebuild();
mx=;from = ;
bfs();
bfs(from);
printf("%d\n",scc-mx);
}
return ;
}
HDU 4612 Warm up tarjan缩环+求最长链的更多相关文章
- hdu 4612 Warm up(缩点+树上最长链)
本来就是自己负责图论,结果水了= = 题目其实很裸,就是求桥的数量,只是要新加上一条边罢了.做法:先缩点.再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲 ...
- HDU 4612 Warm up (边双连通分量+DP最长链)
[题意]给定一个无向图,问在允许加一条边的情况下,最少的桥的个数 [思路]对图做一遍Tarjan找出桥,把双连通分量缩成一个点,这样原图就成了一棵树,树的每条边都是桥.然后在树中求最长链,这样在两端点 ...
- HDU 4612 Warm up —— (缩点 + 求树的直径)
题意:一个无向图,问建立一条新边以后桥的最小数量. 分析:缩点以后,找出新图的树的直径,将这两点连接即可. 但是题目有个note:两点之间可能有重边!而用普通的vector保存边的话,用v!=fa的话 ...
- cf374C Inna and Dima dfs判环+求最长链
题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...
- Grouping ZOJ - 3795 (tarjan缩点求最长路)
题目链接:https://cn.vjudge.net/problem/ZOJ-3795 题目大意:给你n个人,m个关系, 让你对这个n个人进行分组,要求:尽可能的分组最少,然后每个组里面的人都没有关系 ...
- HDU 4612 Warm up tarjan 树的直径
Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...
- hdu 4612 Warm up 桥缩点
4612Warm hdu up 题目:给出一个图,添加一条边之后,问能够在新图中得到的最少的桥的数量. 分析:我们可以双联通分量进行缩点,原图变成了一棵树.问题变成了:求树中添加一条边之后,使得不在圈 ...
- HDU 4612 Warm up 连通图缩点
题目大意:给出一个连通图,求再一个边后,剩余的最少桥数. 题目思路:首先进行缩点得到重构后的图,求出重构后树的直径(通过两次BFS求出相距最远的两点间的距离),ans=重构图边数-树的直径 //#pr ...
- HDU 4612 Warm up(Tarjan)
果断对Tarjan不熟啊,Tarjan后缩点,求树上的最长路,注意重边的处理,借鉴宝哥的做法,开标记数组,标记自己的反向边. #pragma comment(linker, "/STACK: ...
随机推荐
- html中input输入框屏蔽鼠标右键
<label> <input id="ckdestinationId" type="text" oncontextmenu="ret ...
- WPF RichTextBox的使用总结
RichTextBox内容模型 RichTextBox 支持基于块的内容模型. RichTextBox 的内容属性为 Blocks,这是 Paragraph 元素的集合Paragraph元素可包含 ...
- 史上最浅显易懂的Git分布式版本控制系统教程
从零起步的Git教程,让你无痛苦上手世界上最流行的分布式版本控制系统Git! 既然号称史上最浅显易懂的Git教程,那这个教程有什么让你怦然心动的特点呢? 首先,本教程绝对面向初学者,没有接触过版本控制 ...
- php-jquery-json-3
memcache redis缓存技术mysql中的int和text是有区别的, , 按字节长度来记忆jquery中的选择器中的空格是运算符, 所以不能多也不能少, 非常严格层次运算符: 空格 大于 等 ...
- PHP初学者必须掌握的10个知识点
这里总结了PHP初学者容易感到困惑的10个问题,供大家参考.1.页面之间无法传递变量get,post,session在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用1 ...
- this prototype 闭包 总结
this对象 整理下思路: 一般用到this中的情景: 1.构造方法中 function A(){ this.name="yinshen"; } var a=new A(); co ...
- 小白科普之JavaScript的JSON
一.对json的理解 json是一种数据格式,不是一种编程语言,json并不从属于javascript. json的语法可以表示以下三种类型的值 1)简单值 ...
- C#关键字base
例子: public CustomStroke(SharpType type) :base() { this.type = type; } 这里的CustomStroke继承与基类Stroke类,用关 ...
- yum 配置
1.配置yum本地源 # mount /dev/cdrom /mnt/ # vim /etc/yum.repos.d/rhel-source.repo 1 [rhel-source] 2 name=R ...
- iOS 和 Android 触摸事件传递
先看文章,写得很好 ios 触摸事件传递 http://www.cnblogs.com/Quains/p/3369132.html 另外一篇 http://blog.csdn.net/yongyinm ...