[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 ...
随机推荐
- x5webview 微信H5支付
mWebView.setWebViewClient(new WebViewClient() { // @Override // public boolean shouldOverrideUrlLoad ...
- CSS3和动画
定位: z-index叠层 数字越大越往上层 注意:要用z-index属性必须设position属性 溢出:overflow 属性值:visible 不剪切内容也不添加滚动条 Auto ...
- UNC路径格式
\\192.168.3.66\c$ 访问本地网内计算机
- 如何在Git提交空文件夹
1,git clone url 拉取代码至本地 2,mkdir 文件夹名称 在本地创建文件夹 3,cd 文件夹名称 git init 初始化文件夹 vi .gitkeep 创建.gitkeep文件,内 ...
- mysql 报错 Operand should contain 1 column(s)
报错 Operand should contain 1 column(s) 原因 select 后面加了 () select (x,x,x)
- Scrapy分布式爬虫打造搜索引擎- (二)伯乐在线爬取所有文章
二.伯乐在线爬取所有文章 1. 初始化文件目录 基础环境 python 3.6.5 JetBrains PyCharm 2018.1 mysql+navicat 为了便于日后的部署:我们开发使用了虚拟 ...
- yum仓库及配置
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于服务器需求,需要在公司内网搭建内网yum源. 搭建内网yum源需要分以下几个步骤,如下: 1. yum是什么 2. repo文件是什么 3. r ...
- crontab -e 和/etc/crontab的区别
/etc/crontab文件和crontab -e命令区别/etc/crontab文件和crontab -e命令区别 1.格式不同 前者 # For details see man 4 crontab ...
- Python 正则表达式 匹配任意字符
.(句点)匹配除了换行之外的所有一个字符, .*(点-星)匹配除了换行外的所有字符 >>> >>> r=re.compile(r'.*')>>> ...
- tcl之内容