hdu 4612 Warm up 有重边缩点+树的直径
Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5353 Accepted Submission(s): 1195
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define fi first
#define se second
typedef pair<int, int> pll;
const int inf = ;
const int maxn = 2e5+;
const int maxe = 1e6+;
int head[maxn], head1[maxn], dis[maxn], num, num1, top, cnum, instack[maxn], st[maxn], dfn[maxn], low[maxn], s[maxn];
int maxx, pos, vis[maxe*], cnt;
struct node
{
int to, nextt;
}e[maxe*], e1[maxe*];
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void add1(int u, int v) {
e1[num1].to = v, e1[num1].nextt = head1[u], head1[u] = num1++;
}
void init() {
num = num1 = cnt = cnum = top = ;
mem1(head);
mem(s);
mem(vis);
mem1(head1);
mem(instack);
mem(st);
mem(dfn);
mem(low);
mem(dis);
}
pll edge[maxe];
void tarjan(int u) {
instack[u] = ;
st[top++] = u;
dfn[u] = low[u] = ++cnt;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(vis[i])
continue;
vis[i] = vis[i^] = ;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if(instack[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u]) {
++cnum;
int x;
do {
x = st[--top];
instack[x] = ;
s[x] = cnum;
} while(x != u);
}
}
void bfs(int u) {
queue <int> q;
q.push(u);
mem2(dis);
dis[u] = ;
mem(vis);
vis[u] = ;
maxx = , pos = u;
while(!q.empty()) {
int v = q.front(); q.pop();
for(int i = head1[v]; ~i; i = e1[i].nextt) {
int ve = e1[i].to;
if(vis[ve])
continue;
vis[ve] = ;
dis[ve] = dis[v]+;
if(dis[ve]>maxx) {
maxx = dis[ve];
pos = ve;
}
q.push(ve);
}
}
}
int main()
{
int n, m, x, y;
while(cin>>n>>m) {
if(n+m==)
break;
init();
for(int i = ; i<m; i++) {
scanf("%d%d", &x, &y);
edge[i].fi = x, edge[i].se = y;
add(x, y);
add(y, x);
}
tarjan();
int edgenum = ;
for(int i = ; i<m; i++) {
int x = edge[i].fi, y = edge[i].se;
if(s[x]!=s[y]) {
add1(s[x], s[y]);
add1(s[y], s[x]);
edgenum++;
}
}
bfs(s[]);
bfs(pos);
int ans = edgenum-maxx;
printf("%d\n", ans);
}
return ;
}
hdu 4612 Warm up 有重边缩点+树的直径的更多相关文章
- hdu 4612 Warm up 双连通缩点+树的直径
首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...
- Hdu 4612 Warm up (双连通分支+树的直径)
题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...
- HDU 4612 Warm up(双连通分量缩点+求树的直径)
思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...
- HDU 4612 Warm up 连通图缩点
题目大意:给出一个连通图,求再一个边后,剩余的最少桥数. 题目思路:首先进行缩点得到重构后的图,求出重构后树的直径(通过两次BFS求出相距最远的两点间的距离),ans=重构图边数-树的直径 //#pr ...
- F - Warm up HDU - 4612 tarjan缩点 + 树的直径 + 对tajan的再次理解
题目链接:https://vjudge.net/contest/67418#problem/F 题目大意:给你一个图,让你加一条边,使得原图中的桥尽可能的小.(谢谢梁学长的帮忙) 我对重边,tarja ...
- HDU 4612 Warm up —— (缩点 + 求树的直径)
题意:一个无向图,问建立一条新边以后桥的最小数量. 分析:缩点以后,找出新图的树的直径,将这两点连接即可. 但是题目有个note:两点之间可能有重边!而用普通的vector保存边的话,用v!=fa的话 ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- HDU 4612——Warm up——————【边双连通分量、树的直径】
Warm up Time Limit:5000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- 【HDU 4612 Warm up】BCC 树的直径
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:一个包含n个节点m条边的无向连通图(无自环,可能有重边).求添加一条边后最少剩余的桥的数 ...
随机推荐
- description方法介绍及重写
- 发布前,Bat Script备份服务器的Website
由于远程访问服务器,操作滞后验证,备份不方便.我试了两种方式,VBScript和利用7zip的脚本自动备份网站.下面有简单的说明供参考. 1. VBScript, 使用VB脚本打包,不稳定,在服务器上 ...
- 原来你是个这样的JVM
第一节 本文将与其它文章不同,我们采用章节制来讲述每个知识点,但每个章节之间只有较低的耦合度,只要了解大概主线思路,一般都能看懂! OK啦,进入主题!虚拟机的发展史就不讲啦,和java之间的关系也不言 ...
- [C++]KMP算法实现
KMP算法说明:http://zh.wikipedia.org/wiki/%E5%85%8B%E5%8A%AA%E6%96%AF-%E8%8E%AB%E9%87%8C%E6%96%AF-%E6%99% ...
- Tomcat教程
随着java的流行,其在web上的应用也越来越广,tomcat作为一个开源的servlet容器,应用前景越来越广,本文将向你讲述tomcat的一些知识. 一:简介 tomcat是jakar ...
- Linux学习之less命令
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- EcShop后台添加菜单[步骤]
1. 添加菜单的链接地址:打开文件[/后台目录/includes/inc_menu.php],在结尾加入例如:$modules['dashi']['dashi_list'] = 'join_dashi ...
- windows下python2和python3共存
相信很多朋友都在网上搜索过python多版本共存的问题. 多说的说法都是修改python.exe的名字为python2.exe或者python3.exe. 但是我按照这样的方法却总是不成功. 修改py ...
- FLAG_ACTIVITY_NEW_TASK和SingleInstance的设计思路(多task的应用)
这部分的想法都是基于以下两点: 1.Activity可能被复用,可能是复用Activity的功能,还可能是复用Activity的状态: 2.Task的作用:target,同一个task中的Activi ...
- SQL Server用户自定义类型与统计信息
用户自定义数据类型不支持统计信息! 所以查询对它的查询会慢一些.