[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 ? ' ' : '
');
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
", cnt + 1 >> 1);
return 0;
}