1. 程式人生 > >【bzoj1718】Redundant Paths 分離的路徑

【bzoj1718】Redundant Paths 分離的路徑

1718: [Usaco2006 Jan] Redundant Paths 分離的路徑

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 964  Solved: 503
[Submit][Status][Discuss]

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.

    為了從F(1≤F≤5000)個草場中的一個走到另一個,貝茜和她的同伴們有時不得不路過一些她們討厭的可怕的樹.奶牛們已經厭倦了被迫走某一條路,所以她們想建一些新路,使每一對草場之間都會至少有兩條相互分離的路徑,這樣她們就有多一些選擇.     每對草場之間已經有至少一條路徑.給出所有R(F-1≤R≤10000)條雙向路的描述,每條路連線了兩個不同的草場,請計算最少的新建道路的數量, 路徑由若干道路首尾相連而成.兩條路徑相互分離,是指兩條路徑沒有一條重合的道路.但是,兩條分離的路徑上可以有一些相同的草場. 對於同一對草場之間,可能已經有兩條不同的道路,你也可以在它們之間再建一條道路,作為另一條不同的道路.

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.

    第1行輸入F和R,接下來R行,每行輸入兩個整數,表示兩個草場,它們之間有一條道路.

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

Source

Gold

 

題意:

給你一個無向圖$G$,求至少加幾條邊能使它變成一個邊雙聯通分量。

 

題解:

首先把原圖中所有邊雙縮點後連邊,原圖變成一棵樹。

注意$u->v$這條邊是割邊當且僅當$dfn[u]<low[v]$,既然是無向圖,搜尋樹中以$v$為根的子樹就可以被認為是一個邊雙的起點了。

(割邊溝通的兩點必定不在一個邊雙中;無向圖搜尋樹沒有橫叉邊)

那麼現在問題變成在樹上新增幾條邊使其變成一個邊雙(說是環也行),相當於最終葉子節點個數為$0$。

看起來我們每次應該選擇兩個葉子節點$u,v$連邊,那麼應該如何選擇呢?

考慮貪心,只要保證每次連邊後儘量不產生葉子節點即可。

那麼顯然連線一對$u,v$使得$u->v$的簡單路徑上有樹枝即可不產生葉子節點。

最後連完可能會剩下一個,再多一條邊即可。設葉子節點個數為$k$,所求答案為$\frac{k+1}{2}$。

(問題也可轉化成每次選兩個點覆蓋它們簡單路徑上的所有點,至少多少次覆蓋整棵樹)

總之這道題實現簡單,但結論難推也更難證。

 

程式碼:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>

using namespace std;
#define MAXN 5005
#define MAXM 500005
#define INF 0x7fffffff
#define ll long long

int hd[MAXN],to[MAXM<<1];
int nxt[MAXM<<1],cnt,num,tot;
int dfn[MAXN],low[MAXN];
int cl[MAXN],deg[MAXN];
bool ins[MAXN],vis[MAXN][MAXN];
stack<int> s;

inline int read(){
    int x=0,f=1;
    char c=getchar();
    for(;!isdigit(c);c=getchar())
        if(c=='-')
            f=-1;
    for(;isdigit(c);c=getchar())
        x=x*10+c-'0';
    return x*f;
}

inline void addedge(int u,int v){
    to[++cnt]=v,nxt[cnt]=hd[u];
    hd[u]=cnt;return;    
}

inline void tarjan(int u,int fa){
    dfn[u]=low[u]=++num;
    s.push(u);ins[u]=1;
    for(int i=hd[u];i;i=nxt[i]){
        int v=to[i];
        if(v==fa) continue;
        if(!dfn[v]){
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
        }
        else if(ins[v]) 
            low[u]=min(low[u],dfn[v]);
    } 
    if(dfn[u]==low[u]){
        tot++;
        while(s.top()!=u){
            ins[s.top()]=0;
            cl[s.top()]=tot;
            s.pop();
        }
        ins[s.top()]=0;
        cl[s.top()]=tot;
        s.pop(); 
    }
    return;
}

int main(){
    int N=read(),M=read();
    for(int i=1;i<=M;i++){
        int u=read(),v=read();
        addedge(u,v);addedge(v,u);
    } 
    for(int i=1;i<=N;i++)
        if(!dfn[i])
            tarjan(i,0);
    for(int u=1;u<=N;u++)
        for(int i=hd[u];i;i=nxt[i]){
            int v=to[i];
            if(cl[u]!=cl[v] && !vis[cl[u]][cl[v]] && !vis[cl[v]][cl[u]]){
                deg[cl[u]]++,deg[cl[to[i]]]++;
                vis[cl[u]][cl[v]]=1;
                vis[cl[v]][cl[u]]=1;
            }
        }
    int ans=0;
    //for(int i=1;i<=N;i++) cout<<deg[i]<<":"<<cl[i]<<endl;
    for(int u=1;u<=N;u++)
        if(deg[u]==1)
            ans++;
    printf("%d\n",(ans+1)/2);
    return 0;
}