POJ3352Road Construction(邊的雙連通+強連通縮點)

來源:互聯網
上載者:User

標籤:邊的雙連通

Road Construction
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8673   Accepted: 4330

Description

It‘s almost summer time, and that means that it‘s almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 110 121 21 31 42 52 65 63 73 87 84 94 109 10Sample Input 23 31 22 31 3

Sample Output

Output for Sample Input 12Output for Sample Input 20

Source

CCC 2007邊的雙連通定義:在無向圖G中,任意兩點之間至少有兩條路可走,且每條路不經過同一個邊。也就是說每個邊只屬於一條路徑。解題:首先求強連通分量,後縮點,縮點後的圖中度為1的點只要度變成2就形成了邊的雙連通。那麼在這些度為1的點中取兩個點連起來,重複動作,就能使這些點的度數變成2,所以添加最少的邊數=(度為1的點數+1)/2;
#include<stdio.h>#include<iostream>#include<string.h>#include<vector>using namespace std;vector<int>map[1005];int stack[1005],sn,vist[1005],low[1005],dfn[1005],deep,node[1005],k;int Min(int a,int b){    return a>b?b:a;}void dfs(int i,int fath){    vist[i]=1; stack[++sn]=i;    deep++; low[i]=dfn[i]=deep;    int len=map[i].size();    for(int j=0;j<len;j++)    {        int tj=map[i][j];        if(tj==fath)continue;        if(vist[tj]==0)        {            dfs(tj,i);            low[i]=Min(low[i],low[tj]);        }        else if(vist[tj]==1)            low[i]=Min(low[i],dfn[tj]);    }    if(low[i]==dfn[i])    {        k++;        while(stack[sn]!=i)        {            node[stack[sn]]=k; vist[stack[sn--]]=2;        }         node[stack[sn]]=k; vist[stack[sn--]]=2;    }}int answer(int n){    k=0; deep=0; sn=0;    for(int i=1;i<=n;i++)        vist[i]=0;    int ans=0,in[1005]={0};        dfs(1,-1);    for(int i=1;i<=n;i++)//縮點    for(int j=0;j<map[i].size();j++)    {        int tj=map[i][j];        if(node[i]!=node[tj])            in[node[i]]++;    }    for(int i=1;i<=k;i++)    if(in[i]==1)     ans++;    if(ans%2==1) ans++;    return ans/2;//雙連通,度為1的點兩兩相連添加邊}int main(){    int n,m,a,b;    while(scanf("%d%d",&n,&m)>0)    {        for(int i=1;i<=n;i++)        map[i].clear();        while(m--)        {            scanf("%d%d",&a,&b);            map[a].push_back(b);            map[b].push_back(a);        }        printf("%d\n",answer(n));    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.