天天看點

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,題目分析簡略,見諒,轉載請注明出處。。。。。

繼續閱讀