POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)

时间:2024-01-05 11:40:14

Description

In order to get from one of the F (1 <= F <= 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 descri_ption of the current set of R (F-1 <= R <= 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.

Input

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.

Output

Line 1: A single integer that is the number of new paths that must be built.

Description

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.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 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 andw, 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.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

题目大意:给一个n个点m条边的无向连通图,问最少加上多少条边,可以使这幅图每两点之间都有条以上的路径。

思路:求双连通分量,答案为(度数为1的双连通分量+1)/2。证明略。

PS:由于题目所求的东西导致数据有点水代码正确性未知。

PS2:3177有重边,3352没有重边,我直接拿3177的ACcode砸上3352了。至于重边的问题,个人认为删掉一条是不对的,都能AC只能说明数据水(我删重边AC过,下面的代码是没有删重边的)

代码(3177 0MS / 3352 16MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std; const int MAXN = ;
const int MAXE = ; int head[MAXN], sccno[MAXN];
int to[MAXE], next[MAXE];
bool vis[MAXE];
int pre[MAXN], lowlink[MAXN];
int stk[MAXN], top;
int dfs_clock, scc_cnt, ecnt, n, m; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} void dfs(int u) {
pre[u] = lowlink[u] = ++dfs_clock;
stk[top++] = u;
for(int p = head[u]; p; p = next[p]) {
if(vis[p]) continue;
vis[p] = vis[p ^ ] = true;
int &v = to[p];
if(!pre[v]) {
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
} else lowlink[u] = min(lowlink[u], pre[v]);
}
if(lowlink[u] == pre[u]) {
++scc_cnt;
while(true) {
int x = stk[--top];
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} int deg[MAXN]; int solve() {
for(int i = ; i <= n; ++i)
if(!pre[i]) dfs(i);
int ret = ;
for(int u = ; u <= n; ++u) {
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(sccno[u] == sccno[v]) continue;
++deg[sccno[v]];
}
}
for(int i = ; i <= scc_cnt; ++i)
ret += (deg[i] == );
return (ret + ) >> ;
} int main() {
scanf("%d%d", &n, &m);
init();
while(m--) {
int a, b;
scanf("%d%d", &a, &b);
add_edge(a, b);
}
printf("%d\n", solve());
}