POJ3177 Redundant Paths —— 边双联通分量 + 缩点

题目链接:http://poj.org/problem?id=3177


 

Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15852   Accepted: 6649

 

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

 

 

题解:

1. 已知题目给出的图是联通图,那么我们可以用Tarjan算法求出每个边双联通分量。为什么是边双联通分量而不是点双联通分量或者其他?答:因为题目要求每个点之间至少有两条路径(并且路径中不能有重叠的边),位于同一个边双联通分量的点之间都至少有两条路径,但是位于不同边双联通分量的点只有一条路径。所以需要根据边双联通对原图进行划分。

2.对每个边双连通分量进行缩点,得到的是一棵无根树。那么无根树怎样才能变成边双联通图呢?至少需要添加几条边呢?答:只需要把所有叶子结点都“消灭”掉就可以了。那么最少需要添加:(叶子结点数+1)/2 条边。


代码如下:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 using namespace std;
  6 typedef long long LL;
  7 const int INF = 2e9;
  8 const LL LNF = 9e18;
  9 const int MOD = 1e9+7;
 10 const int MAXN = 5e3+10;
 11 
 12 struct Edge
 13 {
 14     int to, next;
 15     bool cut;
 16 }edge[MAXN<<2];
 17 int head[MAXN], tot;
 18 
 19 int index, dfn[MAXN], low[MAXN];
 20 int block, belong[MAXN];
 21 int top, Stack[MAXN], instack[MAXN];
 22 int degree[MAXN];
 23 
 24 void addedge(int u, int v)
 25 {
 26     edge[tot].to = v;
 27     edge[tot].cut = false;
 28     edge[tot].next = head[u];
 29     head[u] = tot++;
 30 }
 31 
 32 void Tarjan(int u, int pre)
 33 {
 34     low[u] = dfn[u] = ++index;
 35     Stack[top++] = u;
 36     instack[u] = true;
 37     for(int i = head[u]; i!=-1; i = edge[i].next)
 38     {
 39         int v = edge[i].to;
 40         if(v==pre) continue;
 41         if(!dfn[v])
 42         {
 43             Tarjan(v, u);
 44             low[u] = min(low[u], low[v]);
 45             if(low[v]>dfn[u])
 46             {
 47                 edge[i].cut = true;
 48                 edge[i^1].cut = true;
 49             }
 50         }
 51         else if(instack[v])
 52             low[u] = min(low[u], dfn[v]);
 53     }
 54 
 55     if(low[u]==dfn[u])
 56     {
 57         block++;
 58         int v;
 59         do
 60         {
 61             v = Stack[--top];
 62             instack[v] = false;
 63             belong[v] = block;
 64         }while(v!=u);
 65     }
 66 }
 67 
 68 void init()
 69 {
 70     tot = 0;
 71     memset(head,-1,sizeof(head));
 72 
 73     index = 0;
 74     memset(dfn,0,sizeof(dfn));
 75     memset(low,0,sizeof(low));
 76 
 77     block = top = 0;
 78     memset(instack,0,sizeof(instack));
 79 
 80     memset(degree,0,sizeof(degree));
 81 }
 82 
 83 int main()
 84 {
 85     int n, m;
 86     while(scanf("%d%d",&n,&m)!=EOF)
 87     {
 88         init();
 89         for(int i = 1; i<=m; i++)
 90         {
 91             int u, v;
 92             scanf("%d%d",&u,&v);
 93             addedge(u, v);
 94             addedge(v,u);
 95         }
 96 
 97         Tarjan(1, 1);
 98         for(int u = 1; u<=n; u++)
 99         for(int i = head[u]; i!=-1; i = edge[i].next)
100             if(edge[i].cut) //不需要两端都加,因为一条割边被标记了两次。一次正好对应一个端点。
101                 degree[belong[u]]++;
102 
103         int leaf = 0;
104         for(int i = 1; i<=block; i++)
105             if(degree[i]==1) leaf++;
106 
107         printf("%d\n", (leaf+1)/2);
108     }
109 }
View Code

 

 


 

 


 

posted on 2017-10-17 14:31  h_z_cong  阅读(237)  评论(0编辑  收藏  举报

导航