http://acm.hdu.edu.cn/showproblem.php?pid=4612

Warm up

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

Problem 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
题目大意:给一个连通图,求加一条边之后最少还有多少个桥。
题目分析:首先看到要求是"改造桥",则先进行缩点,由于是改造桥,所以进行边双连通的缩点,然后求树的直径【树上桥最多的一条路】,则最后结果ans=原来桥数-直径。
题解:Tarjan缩点求桥数+两次bfs求树的直径
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
queue<int>pq;
struct edge {
int fr;
int to;
int next;
}e[], e2[];
int cnt, scc_cnt, ccnt, tim;
int head[], head2[], dfn[], low[], stk[], col[],d[], tp,dis,node;
bool in_stk[], vis[];
int ans = ;
void dfs(int x, int fa) {
d[x] = dis++;
for (int i = head2[x]; i != -; i = e2[i].next) {
int v = e2[i].to;
if (v == fa)continue;
if (!vis[v]) {
vis[v] = ;
dfs(v, x);
}
}
return;
}
void bfs(int x) {
while (!pq.empty())pq.pop();
pq.push(x);
memset(d, , sizeof(d));
memset(vis, , sizeof(vis));
while (!pq.empty()) {
int s = pq.front(); pq.pop();
for (int i = head2[s]; i != -; i = e2[i].next) {
int v = e2[i].to;
if (!vis[v]) {
vis[v] = ;
d[v] = d[s] + ;
pq.push(v);
if (d[v] > ans) {
node = v;
ans = d[v];
}
}
}
}
}
void Tarjan(int u, int fa, int id) {
dfn[u] = low[u] = ++tim;
in_stk[u] = ;
stk[tp++] = u;
for (int i = head[u]; i != -; i = e[i].next) {
int v = e[i].to;
if (!dfn[v]) {
Tarjan(v, u, i);
if (low[v] < low[u])low[u] = low[v];
}
else if (in_stk[v] && ((i ^ ) != id)) {
if (dfn[v] < low[u])low[u] = dfn[v];//这里使用dfn[v]和low[v]都可以
}
}
if (dfn[u] == low[u]) {
scc_cnt++;
do {
tp--;
int tt = stk[tp];
col[tt] = scc_cnt;
in_stk[tt] = ;
} while (stk[tp] != u);
}
}
void add(int x, int y) {
e[cnt].fr = x;
e[cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt++;
}
void add2(int x, int y) {
e2[ccnt].fr = x;
e2[ccnt].to = y;
e2[ccnt].next = head2[x];
head2[x] = ccnt++;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
while (n || m) {
cnt = ;
tp = ;
scc_cnt = ;
ccnt = ;
ans = ;
tim = ;
memset(vis, , sizeof(vis));
memset(in_stk, , sizeof(in_stk));
memset(col, , sizeof(col));
memset(head, -, sizeof(head));
memset(head2, -, sizeof(head2));
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
}
for (int i = ; i <= n; i++) {
if (!dfn[i]) {
Tarjan(i, -, -);
}
}
for (int i = ; i < cnt; i += ) {
if (col[e[i].fr] != col[e[i].to]) {
add2(col[e[i].fr], col[e[i].to]);
add2(col[e[i].to], col[e[i].fr]);
}
}
bfs();
bfs(node);
cout << ccnt / - ans << endl;
scanf("%d%d", &n, &m);
}
return ;
}
 

【HDOJ4612】【双连通分量缩点+找树的直径】的更多相关文章

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

    思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...

  2. HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)

    Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...

  3. POJ3177 Redundant Paths(边双连通分量+缩点)

    题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...

  4. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

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

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

  6. Gym - 100676H H. Capital City (边双连通分量缩点+树的直径)

    https://vjudge.net/problem/Gym-100676H 题意: 给出一个n个城市,城市之间有距离为w的边,现在要选一个中心城市,使得该城市到其余城市的最大距离最短.如果有一些城市 ...

  7. hdu4612 Warm up[边双连通分量缩点+树的直径]

    给你一个连通图,你可以任意加一条边,最小化桥的数目. 添加一条边,发现在边双内是不会减少桥的.只有在边双与边双之间加边才有效.于是,跑一遍边双并缩点,然后就变成一棵树,这样要加一条非树边,路径上的点( ...

  8. poj 3177 Redundant Paths(边双连通分量+缩点)

    链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...

  9. 图论-桥/割点/双连通分量/缩点/LCA

    基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...

随机推荐

  1. 重绘(Repaint)和回流(Reflow)

    重绘(Repaint)和回流(Reflow) 1.回流和重绘只是渲染步骤的一小节,是怎么做到影响性能的? css 会影响 javascrip 执行时间导致 javascript 脚本变慢 浏览器渲染一 ...

  2. 隔行变色&&鼠标移入变色

    <html lang="en"> <head> <meta charset="UTF-8"> <title>Do ...

  3. java字符串根据空格截取并存进list,并在每个元素前后加上/

    public class List1 { public static void main(String[] args) { String s = "abc nnn ooo/xzsxc bs& ...

  4. 【转载】Qt中图像的显示与基本操作

    Qt可显示基本的图像类型,利用QImage.QPxmap类可以实现图像的显示,并且利用类中的方法可以实现图像的基本操作(缩放.旋转). 参考:Qt中图像的显示与基本操作 - ykm0722的专栏 - ...

  5. Python数据分析中对重复值、缺失值、空格的处理

    对重复值的处理 把数据结构中,行相同的数据只保留一行 函数语法: drop_duplicates() from pandas import read_csv df = read_csv(文件位置) n ...

  6. 不管你是否已经准备面试, 这45道Python面试题都对你非常有帮助!(mark!)

    1)什么是Python?使用Python有什么好处? Python是一种编程语言,包含对象,模块,线程,异常和自动内存管理.蟒蛇的好处在于它简单易用,可移植,可扩展,内置数据结构,并且它是一个开源的. ...

  7. 使用DLL在进程间共享数据

    0x01 DLL在进程间共享数据理论 1.可以在Dll中使用#pragma data_seg建立共享类型的数据段将需要共享的数据分离出来,放置在一个独立的数据段里,并把该段的属性设置为共享,从而实现不 ...

  8. 《图解HTTP》读书笔记(转)

    reference:https://www.cnblogs.com/edisonchou/p/6013450.html   目前国内讲解HTTP协议的书是在太少了,记忆中有两本被誉为经典的书<H ...

  9. ThreadPoolExcutor

    先保存一个链接,后续更新 JAVA进阶----ThreadPoolExecutor机制 ExecutorService生命周期 理解ThreadPoolExecutor源码(一)线程池的corePoo ...

  10. lvm创建和快照

    查看磁盘 创建分区 新建1G的1分区 新建1G的2分区 新建1G的3分区 查看新建的分区 因标准分区是83交换分区是82做lv是8e所以要改变类型 查看: 保存退出: 创建物理卷pv 将物理卷pv创建 ...