[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 ...
随机推荐
- 机器学习中正则化项L1和L2的直观理解
正则化(Regularization) 概念 L0正则化的值是模型参数中非零参数的个数. L1正则化表示各个参数绝对值之和. L2正则化标识各个参数的平方的和的开方值. L0正则化 稀疏的参数可以防止 ...
- Java环境变量搭建(Windows环境)
变量名:JAVA_HOME 变量值:C:\Program Files (x86)\Java\jdk1.8.0_91 // 要根据自己的实际路径配置 变量名:CLASSPATH 变量值:. ...
- Linux内核参数min_free_kbytes
1. min_free_kbytes 先看官方解释: This is used to force the Linux VM to keep a minimum number of kilobytes ...
- python 线程的调用方式
python 线程的调用方式 #!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/5/24 9:44 # ...
- oc字符串截取 数组字典运用
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS_ ...
- Vue之Vue-touch的使用
最近项目中,有的页面发现设置返回键看起来怪怪的,感觉与整体不协调,于是就考虑使用手势滑动事件来实现返回功能~ 开叉查阅资料~找到了vue-touch,使用起来可谓是简单粗暴啊,适合我这样的快速开发人员 ...
- Linux更改文件权限(二)
更改文件权限(二)============================== (参考于千锋教育教学笔记) 命令umask [root@aminglinux ~]# umask 0022 [root@ ...
- Yii2.X 如何避开pathinfo不能处理中文名开头的bug
/** * @return string original file base name */ public function getBaseName() { // https://github.co ...
- jQuery具体实例介绍什么时候用ajax,ajax应该在什么地方使用
网站开发时,ajax是一个非常方便的工具,它具有和表单相同的功能完成前端和后台之间的交互!它起到局部刷新的功能!那什么时候用ajax呢?下面给大家介绍几个实例,首先应该分为两类: 一.在用表单和aj ...
- javaWeb开发中常见的问题
1.修改表单提交的时候不好使可能是因为没写对应隐藏域的ID 2.el表达式在js代码中要加“”,例如 "${}" 3.JavaScript中的函数也有重载的特性.如果两个input ...