POJ 3177 Redundant Paths (tarjan+缩点)

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.

Sample Input

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

Sample Output

2

题意:找出最少能连多少条线使得任意两点间的路径不止一条。

坑点:这题的路径中有重边,如:

2 2

1 2

扫描二维码关注公众号,回复: 4113819 查看本文章

2 1

这个样例,如果不算重边那就是1,但是这题答案是0.

思路:跑一遍tarjan,缩点,找到叶子节点个数,答案等于(叶子节点个数+1)/2.如何找到叶子节点,因为叶子节点的度为一,所以遍历全部边,如果度为1,则这个点就是叶子节点。


#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stack>
using namespace std;
struct path
{
    int to,nextt;
} A[23333];
stack<int>q;
int head[6666],DFN[6666],LOW[6666],book[6666],in[6666],re[6666];
int n,m,x,y,tot,carry,indox,ans;
int init()
{
    tot=ans=indox=carry=0;
    memset(head,-1,sizeof(head));
    memset(DFN,-1,sizeof(DFN));
    memset(LOW,-1,sizeof(LOW));
    memset(in,0,sizeof(in));
    memset(book,0,sizeof(book));
    return 0;
}
int add(int u,int v)
{
    A[tot].to=v;
    A[tot].nextt=head[u];
    head[u]=tot++;
    return 0;
}
int tarjan(int u,int v)
{
    int tem;
    q.push(u);
    book[u]=1;
    DFN[u]=LOW[u]=++indox;
    for(int i=head[u]; i!=-1; i=A[i].nextt)
    {
        tem=A[i].to;
        if(i==v) continue;
        if(DFN[tem]==-1)
        {
            tarjan(tem,i^1);
            LOW[u]=min(LOW[tem],LOW[u]);
        }
        else if(book[tem])
            LOW[u]=min(LOW[u],DFN[tem]);
    }
    if(DFN[u]==LOW[u])
    {
        ++carry;
        do
        {
            tem=q.top();
            q.pop();
            re[tem]=carry;
            book[tem]=0;
        }
        while(tem!=u);
    }
    return 0;
}
int slove()
{
    int v;
    for(int i=1; i<=n; i++)
    {
        for(int j=head[i]; j!=-1; j=A[j].nextt)
        {
            v=A[j].to;
            if(re[i]!=re[v])
                in[re[v]]++;
        }
    }
    for(int i=1; i<=carry; i++)
    {
        if(in[i]==1)
        {
            ans++;
        }
    }
    printf("%d\n",(ans+1)/2);
    return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        tarjan(1,-1);
        slove();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/84035584
今日推荐