程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3177 Redundant Paths(邊雙聯通圖)

POJ 3177 Redundant Paths(邊雙聯通圖)

編輯:C++入門知識

POJ 3177 Redundant Paths(邊雙聯通圖)


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.

Source

USACO 2006 January Gold

題意:就是加入最少的邊使之成雙聯通圖

一個有橋的聯通圖要變成雙聯通圖的化,先把雙聯通子圖縮點處理,使之成為樹(無環)
找出樹的葉子節點(入度為1)加邊的數為(leaf+1)/2;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pairpil;
const int maxn=5000+10;
const int maxm=10000+10;
struct node{
    int to,next;
    bool col;//為橋
}e[maxm];
int head[maxn],cnt;
int DFN[maxn],low[maxn];
int s[maxn],instack[maxn];
int idex,top,bridge;
int belong[maxn],in[maxn];
int n,m;
void init()
{
    cnt=top=idex=bridge=0;
    CLEAR(head,-1);
    CLEAR(DFN,0);
    CLEAR(low,0);
    CLEAR(instack,0);
    CLEAR(belong,0);
    CLEAR(in,0);
}
void addedge(int u,int v)
{
    e[cnt].to=v;e[cnt].next=head[u];
    e[cnt].col=false;head[u]=cnt++;
}
void Tarjan(int u,int pre)
{
    int v;
    low[u]=DFN[u]=++idex;
    s[top++]=u;
    instack[u]=1;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        v=e[i].to;
        if(v==pre)  continue;
        if(!DFN[v])
        {
            Tarjan(v,u);
            if(low[u]>low[v]) low[u]=low[v];
            if(low[v]>DFN[u])//橋
            {
                bridge++;
                e[i].col=true;
                e[i^1].col=true;
            }
        }
        else if(instack[v]&&low[u]>DFN[v])
            low[u]=DFN[v];
    }
    if(low[u]==DFN[u])
    {
        cnt++;
        do{
            v=s[--top];
            instack[v]=0;
            belong[v]=cnt;
        }while(v!=u);
    }
}
void work()
{
    REPF(i,1,n)
      if(!DFN[i])  Tarjan(i,i);
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j!=-1;j=e[j].next)
            if(e[j].col)  in[belong[i]]++;
    }
    int ans=0;
    REPF(i,1,cnt)
      if(in[i]==1)  ans++;
    printf("%d\n",(ans+1)/2);
}
int main()
{
    int u,v;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        REPF(i,1,m)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        work();
    }
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved