poj3352 Road Construction

来源:互联网 发布:双色球内部数据库 编辑:程序博客网 时间:2024/05/30 20:08

Road Construction
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 11448 Accepted: 5713

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

大意:给你一个联通图,问你最少加多少条边使得它变成一个边双联通分量。

思路:先求出每个边联通分量,然后进行缩点。变成一棵树。

我们找出叶子结点,把他们也弄成一个环使得它们的度也变成2就OK了。叶子结点也就是度为1的点咯。

注意:因为是无向图,注意最后缩点计算度的时候要除以2.

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <stack>using namespace std;const int MAXN=1000+10;typedef vector<int>ve;vector<ve>G(MAXN);int n,m;int du[MAXN];int dfn[MAXN],bccno[MAXN],bridge[MAXN][MAXN],dfs_clock,bcc_cnt;int dfs(int u,int fa){    int lowu=dfn[u]=++dfs_clock;    for(int i=0,l=G[u].size(); i<l; ++i)    {        int v=G[u][i];        if(!dfn[v])        {            int lowv=dfs(v,u);            lowu=min(lowu,lowv);            if(lowv>dfn[u])            {                bridge[u][v]=bridge[v][u]=1;            }        }        else if(dfn[u]>dfn[v]&&v!=fa)        {            lowu=min(lowu,dfn[v]);        }    }    return lowu;}void find_bridge(){    for(int i=0; i<n; ++i)    {        dfn[i]=0;        bccno[i]=0;        for(int j=i+1; j<n; ++j)bridge[i][j]=bridge[j][i]=0;    }    dfs_clock=bcc_cnt=0;    for(int i=0; i<n; ++i)if(!dfn[i])dfs(i,-1);}int vis[MAXN];void dfse(int u,int x){    bccno[u]=x;    vis[u]=1;    for(int i=0,l=G[u].size(); i<l; ++i)    {        int v=G[u][i];        if(!vis[v]&&!bridge[u][v])        {            dfse(v,x);        }    }}void find_bcc(){    for(int i=0; i<n; ++i)vis[i]=0;    for(int i=0; i<n; ++i)if(!vis[i])dfse(i,++bcc_cnt);}int main(){    int i;    while(~scanf("%d%d",&n,&m))    {        for(i=0; i<n; ++i)        {            G[i].clear();            du[i]=0;        }        int u,v;        while(m--)        {            scanf("%d%d",&u,&v);            u--;            v--;            G[u].push_back(v);            G[v].push_back(u);        }        find_bridge();//第一次dfs找到桥        find_bcc();//第二次dfs找到所有的双联通分量        for(i=1; i<=bcc_cnt; ++i)du[i]=0;        //缩点        for(i=0; i<n; ++i)        {            for(int j=0,l=G[i].size(); j<l; ++j)            {                int v=G[i][j];                if(bccno[i]!=bccno[v])                {                    du[bccno[i]]++;                    du[bccno[v]]++;                }            }        }        //寻找叶子结点        int sum=0;        for(i=1; i<=bcc_cnt; ++i)        {            if(du[i]/2==1)sum++;        }        printf("%d\n",(sum+1)/2);    }    return 0;}










0 0