[POJ3177]Redundant Paths
试题描述
In order to get from one of the (F (1 le F le 5,000)) grazing fields (which are numbered (1..F)) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.
Given a description of the current set of (R (F-1 le R le 10,000)) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.
There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
输入
Line (1): Two space-separated integers: (F) and (R)
Lines (2..R+1): Each line contains two space-separated integers which are the fields at the endpoints of some path.
输出
Line (1): A single integer that is the number of new paths that must be built.
输入示例
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
输出示例
2
数据规模及约定
见“试题描述”
题解
同POJ3352。
#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--)
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 5010
#define maxm 20010
int n, m, head[maxn], nxt[maxm], to[maxm], id[maxm];
void AddEdge(int a, int b, int _id) {
to[++m] = b; nxt[m] = head[a]; id[m] = _id; head[a] = m;
swap(a, b);
to[++m] = b; nxt[m] = head[a]; id[m] = _id; head[a] = m;
return ;
}
int clo, dfn[maxn], low[maxn], cntb, bcno[maxn], S[maxn], top, deg[maxn];
void dfs(int u, int fae) {
dfn[u] = low[u] = ++clo;
S[++top] = u;
for(int e = head[u]; e; e = nxt[e]) if(id[e] != fae) {
if(dfn[to[e]]) low[u] = min(low[u], dfn[to[e]]);
else dfs(to[e], id[e]), low[u] = min(low[u], low[to[e]]);
}
if(low[u] == dfn[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();
AddEdge(a, b, i);
}
dfs(1, 0);
rep(u, 1, n)
for(int e = head[u]; e; e = nxt[e]) if(bcno[u] < bcno[to[e]])
deg[bcno[u]]++, deg[bcno[to[e]]]++;
int cnt = 0;
rep(u, 1, n) cnt += deg[u] == 1;
printf("%d
", cnt + 1 >> 1);
return 0;
}