POJ3177 双连通分量

Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18580   Accepted: 7711

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 description 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.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

Hint

Explanation of the sample: 

One visualization of the paths is: 
   1   2   3
+---+---+
| |
| |
6 +---+---+ 4
/ 5
/
/
7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 
   1   2   3
+---+---+
: | |
: | |
6 +---+---+ 4
/ 5 :
/ :
/ :
7 + - - - -
Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7 
Every pair of fields is, in fact, connected by two routes. 

It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

Source

题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点。

分析:在同一个边双连通分量中看做同一个点,缩点后,新图是一棵树,树的边就是原无向图的桥。

问题转化为:在树中至少添加多少条边能使图变为双连通图。

结论:添加边数=(树中度为1的节点数+1)/2

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include "algorithm"
 4 using namespace std;
 5 const int N = 5000 + 5;
 6 const int M = 10000 + 5;
 7 struct P {
 8     int to, nxt;
 9 } e[M * 2];
10 int head[N], low[N], dfn[N], beg[N], du[N], st[M], ins[M];
11 int cnt, id, top, num;
12 
13 void add(int u, int v) {
14     e[cnt].to = v;
15     e[cnt].nxt = head[u];
16     head[u] = cnt++;
17 }
18 
19 void tarjan(int u, int fa) {
20     low[u] = dfn[u] = ++id;
21     st[++top] = u;
22     ins[u] = 1;
23     for (int i = head[u]; i != -1; i = e[i].nxt) {
24         int v = e[i].to;
25         if (i == (fa ^ 1)) continue;
26         if (!dfn[v]) tarjan(v, i), low[u] = min(low[u], low[v]);
27         else if (ins[v]) low[u] = min(low[u], dfn[v]);
28     }
29     if (dfn[u] == low[u]) {
30         int v;
31         do {
32             v = st[top--];
33             ins[v] = 0;
34             beg[v] = num;
35         } while (u != v);
36         num++;
37     }
38 }
39 
40 void init() {
41     cnt = id = top = num = 0;
42     memset(head, -1, sizeof(head));
43     memset(low, 0, sizeof(low));
44     memset(dfn, 0, sizeof(dfn));
45     memset(ins, 0, sizeof(ins));
46     memset(du, 0, sizeof(du));
47 }
48 
49 int n, m;
50 int main() {
51     scanf("%d%d", &n, &m);
52     init();
53     for (int i = 0; i < m; i++){
54         int u, v;
55         scanf("%d%d", &u, &v);
56         add(u, v), add(v, u);
57     }
58     for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i, -1);
59     for (int i = 1; i <= n; i++) {
60         for (int j = head[i]; j != -1; j = e[j].nxt){
61             int v = e[j].to;
62             if (beg[i] != beg[v]) du[beg[i]]++;
63         }
64     }
65     int ans = 0;
66     for (int i = 0; i < num; i++)
67         if (du[i] == 1) ans++;
68     printf("%d\n", (ans + 1) / 2);
69     return 0;
70 }

猜你喜欢

转载自www.cnblogs.com/mj-liylho/p/9556225.html