1. 程式人生 > >POJ 3177 Redundant Paths (tarjan+縮點)

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

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;
}