天天看点

Redundant Paths POJ - 3177(tarjan+边双连通分量)题意:题目:分析:AC代码

题意:

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

题目:

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.

分析:

1.此题为将原本的树经过加边的方式,使之成为一个边双连通图,双连通分量又分点双连通分量和边双连通分量两种。若一个无向图中的去掉任意一个节点(一条边)都不会改变此图的连通性,即不存在割点(桥),则称作点(边)双连通图。一个无向图中的每一个极大点(边)双连通子图称作此无向图的点(边)双连通分量。求双连通分量可用Tarjan算法。

2.首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上所有点收缩到一起,因为一个形成的环一定是双连通的。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=5e3+10;
int n,m,a,b,tot,ans;
bool book[M][M]/**标记该条边是否存在*/;
int dfn[M];///时间戳
int low[M];///每个点在这颗树中的,最小的子树的根
int dp[M];///计算入度;           
bool vis[M];/**标记该点是否遍历过*/
void tarjan(int x,int y/**x的父节点,为了防止双向图陷入死循环*/)
{
    dfn[x]=low[x]=++tot;
    vis[x]=true;
    for(int i=1; i<=n; i++)
    {
        if(book[x][i])/**当该x~i边存在时*/
        {
            if(!vis[i])/**该点没有遍历过*/
            {
                tarjan(i,x);
                low[x]=min(low[x],low[i]);
            }
            else if(i!=y)/**因为是无向图,需要防止发生“往回走”的现象(即不能回到父节点)*/
                low[x]=min(low[x],dfn[i]);
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d",&a,&b);
        book[a][b]=book[b][a]=true;/**双向*/
    }
    tarjan(1,1);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(book[i][j])
            {
                if(low[i]!=low[j])
                    dp[low[j]]++;
            }
    ans=0;
    for(int i=1; i<=n; i++)
        if(dp[i]==1)
            ans++;
    printf("%d\n",(ans+1)/2);///找到叶节点,只要树不存在叶节点就可,一条边可以“消灭”两个叶节点,所以是(ans+)/2;
    return 0;
}
           
备战ing,题目分析简略,见谅,转载请注明出处。。。。。

继续阅读