[POJ3352]Road Construction
[POJ3352]Road Construction
试题描述
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
给定一个连通无重边的无向图,求至少添加几条无向边使得改后的图不存在桥(改后的图不要求无重边)。
输入
The first line of input will consist of positive integers \(n\) and \(r\), separated by a space, where \(3 \le n \le 1000\) is the number of tourist attractions on the island, and \(2 \le r \le 1000\) is the number of roads. The tourist attractions are conveniently labelled from \(1\) to \(n\). Each of the following \(r\) lines will consist of two integers, \(v\) and \(w\), separated by a space, indicating that a road exists between the attractions labelled \(v\) and \(w\). Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
输出
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
输入示例1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10
输出示例1
2
输入示例2
3 3
1 2
2 3
1 3
输出示例2
0
数据规模及约定
见“输入”
题解
首先肯定边双缩点,因为我们发现题目和“桥”有很大关系。
缩点后的图就是一颗树,那么如何加入最少的边,把树变成一个边双呢?你可以尝试想树形 dp,但是细细想一下转移似乎不太好做,因为对于一个叶子它可能将这条链延伸到很高的时候才被缩到一个双连通分量中去。但,这个困扰我们做树形 dp 的地方正是这题最后的突破口,我们发现只要找到所有叶子,在它们之间加边就好了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define rep(i, s, t) for(int i = (s); i <= (t); i++)
#define dwn(i, s, t) for(int i = (s); i >= (t); i--)
const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
}
#define maxn 1010
#define maxm 2010
int n;
struct Graph {
int m, head[maxn], nxt[maxm], to[maxm];
Graph(): m(0) { memset(head, 0, sizeof(head)); }
void AddEdge(int a, int b) {
to[++m] = b; nxt[m] = head[a]; head[a] = m;
swap(a, b);
to[++m] = b; nxt[m] = head[a]; head[a] = m;
return ;
}
} G, tr;
int clo, dfn[maxn], low[maxn], cntb, bcno[maxn], S[maxn], top, deg[maxn];
void dfs(int u, int fa) {
dfn[u] = low[u] = ++clo;
S[++top] = u;
for(int e = G.head[u]; e; e = G.nxt[e]) if(G.to[e] != fa) {
if(dfn[G.to[e]]) low[u] = min(low[u], dfn[G.to[e]]);
else dfs(G.to[e], u), low[u] = min(low[u], low[G.to[e]]);
}
if(dfn[u] == low[u]) {
cntb++;
while(S[top] != u) bcno[S[top--]] = cntb;
bcno[S[top--]] = cntb;
}
return ;
}
int main() {
n = read(); int M = read();
rep(i, 1, M) {
int a = read(), b = read();
G.AddEdge(a, b);
}
dfs(1, 0);
// rep(i, 1, n) printf("%d%c", bcno[i], i < n ? ' ' : '\n');
rep(u, 1, n)
for(int e = G.head[u]; e; e = G.nxt[e]) if(bcno[G.to[e]] < bcno[u])
deg[bcno[G.to[e]]]++, deg[bcno[u]]++;
int cnt = 0;
rep(i, 1, cntb) if(deg[i] == 1) cnt++;
printf("%d\n", cnt + 1 >> 1);
return 0;
}
[POJ3352]Road Construction的更多相关文章
- POJ3352 Road Construction(边双连通分量)
...
- POJ3352 Road Construction (双连通分量)
Road Construction Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- POJ-3352 Road Construction,tarjan缩点求边双连通!
Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
- [POJ3352]Road Construction(缩点,割边,桥,环)
题目链接:http://poj.org/problem?id=3352 给一个图,问加多少条边可以干掉所有的桥. 先找环,然后缩点.标记对应环的度,接着找桥.写几个例子就能知道要添加的边数是桥的个数/ ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- 边双联通问题求解(构造边双连通图)POJ3352(Road Construction)
题目链接:传送门 题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图 题目思路:tarjan算法加缩点,缩点后求出度数为1的叶子节点个数,需要加边数为(leaf+1)/2 #include ...
- POJ3352 Road Construction Tarjan+边双连通
题目链接:http://poj.org/problem?id=3352 题目要求求出无向图中最少需要多少边能够使得该图边双连通. 在图G中,如果任意两个点之间有两条边不重复的路径,称为“边双连通”,去 ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
随机推荐
- 2017.12.13 Java中是怎样通过类名,创建一个这个类的数组
先在类方法中定义数组的方法: public int[] method6(int[] arr){ for(int i = 0; i<arr.length;i++){ arr[i] = (int)( ...
- 解决wget下载https时报错 --no-check-certificate (不检查证书)
如果使用 wget下载https开头的网址域名 时报错,你需要加上 --no-check-certificate (不检查证书)选项 例如: wget https://pypi.python.org/ ...
- 运行自己的shell脚本
shell脚本可以直接./**.sh,也可以bash **.sh 我用./**.sh运行自己写的一个脚本,会出现如下的错误: bnrc@bnrc:~$ ./pixel.sh bash: ./pixel ...
- 启动Jmeter时遇到的几种错误
1.权限不够 解决办法:用管理员权限运行 2.sdk版本太低 解决办法:1)查看当前sdk版本:java -version 2)安装sdk1.7或以上版本(jmeter3.0版本要用sdk1.7及以上 ...
- linux 环境能变量配置
1, 3.配置环境变量 在/etc/profile文件末尾中添加以下环境变量:(我上面的JDK目录是jdk1.6.0_45,所以下面JAVA_HOME中也是这个) export JAVA_HOME=/ ...
- Vim编辑器基础命令
Linux系统中都默认安装了vi或vim编辑器,两种命令基本一致.vim为Vi IMproved,功能更强大. vim有命令模式,输入模式,和末行模式三种. ➢ 命令模式:控制光标移动,可对文本进行复 ...
- 数据库引擎InnoDB和myisam的区别和联系
1.ENGINE=InnoDB 数据库存储引擎,DEFAULT 默认,CHARSET=utf8 数据库字符编码 2.数据库的存储引擎, mysql中engine=innodb和engine=myisa ...
- Mysql忘记密码找回步骤
Mysql密码忘记找回步骤: 1.首先停止数据库 [root@localhost ~]# /etc/init.d/mysqld stop 2.使用--skip-grant-tables启动mysql, ...
- ATM-lib-common
import logging.configfrom conf import settingsfrom core import src def get_logger(name): logging.con ...
- Python知识点入门笔记——基本控制流程
复合赋值语句 在Python中,可以使用一次赋值符号,给多个变量同时赋值: 划重点:age_1,age_2 = age_2,age_1这种操作是Python独有的 i ...