POJ3177 Redundant Paths(双连通分量)

Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17297   Accepted: 7201

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.
题意:有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场。奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以选择至少两条独立的路。现在F个牧场的任何两个牧场之间已经至少有一条路了,奶牛们需要至少有两条。
给定现有的R条直接连接两个牧场的路,F-1<=R<=10000,计算至少需要新修多少条直接连接两个牧场的路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指没有公共边的路,但可以经过同一个中间顶点
思路:在一个无向连通图中,最少加几条边使得任意两个点都有边连通,即双连通图。
因为这是无向图,那么缩点的话就可以理解为双连通分量缩成一个点,因为没有环,这样一来整个图就变成了一棵树,想让这棵树都是双连通,那我们必须要把叶子结点连接起来,而最少要连接的边就等于【(叶子结点+1)/2】

接下来就是怎么缩点,求叶子节点了,tarjan就可以实现这个功能,缩点后那些度为1的点就是叶子结点。因为是在无向图里边所以没有出度入读只有读,那么只有一个度的就是叶子结点了。

#include
#include
#include
using namespace std;
const int maxn=5005;
int low[maxn],dfn[maxn],cut[maxn],first[maxn];
bool mapp[maxn][maxn];
int r,cnt,f;
struct node
{
    int to;
    int next;
} e[maxn*2];
int add(int u,int v)
{
    e[r].to=v;
    e[r].next=first[u];
    first[u]=r++;
}
void init()
{
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(cut,0,sizeof(cut));
    memset(first,-1,sizeof(first));
    memset(mapp,0,sizeof(mapp));
    r=0;
    cnt=1;
}
void tarjan(int u,int from)
{
    dfn[u]=low[u]=cnt++;
    for(int i=first[u]; i!=-1; i=e[i].next)
    {
        int v=e[i].to;
        if(!dfn[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
        }
        else if(v!=from)
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
}
void output()
{
    int sum=0;
    for(int i=1; i<=f; i++)
    {
        for(int j=first[i]; j!=-1; j=e[j].next)
        {
            int v=e[j].to;
            if(low[v]!=low[i])
            {
                cut[low[i]]++;
            }
        }
    }
    for(int i=1; i<=f; i++)
    {
        if(cut[i]==1)
            sum++;
    }
    printf("%d\n",(sum+1)/2);
}
int main()
{
    int rr,uu,vv;
    while(~scanf("%d%d",&f,&rr))
    {
        init();
        for(int i=0; i

你可能感兴趣的:(强联通tarjan)