Road Construction+求双联通分量、割点、桥+POJ

来源:互联网 发布:三维插值算法 编辑:程序博客网 时间:2024/06/08 02:18
Road Construction
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8652 Accepted: 4323

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
解决方案:可求双连通分量,把连通分量压缩成一个点,之后就成了一颗树,求出叶子节点的个数k,则要添加的边为(k+1)/2条,而桥就刚好是形成的树的边。
code:
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#define MMAX 1003using namespace std;int n,r,index,root,k;vector<int>Map[MMAX];int low[MMAX],dfn[MMAX],d[MMAX];bool jud[MMAX];void init(){    index=0,k=0;    for(int i=0; i<=n; i++)    {        Map[i].clear();        low[i]=0;        dfn[i]=0;        jud[i]=false;        d[i]=0;    }}void tarjan(int u,int fa){    low[u]=dfn[u]=++index;    int len=Map[u].size();    for(int i=0; i<len; i++)    {        int v=Map[u][i];        if(!dfn[v])        {            tarjan(v,u);            low[u]=min(low[u],low[v]);        }        else if(fa!=v)///防止搜回上一个节点            low[u]=min(low[u],dfn[v]);    }}int main(){    while(~scanf("%d%d",&n,&r))    {        init();        for(int i=0; i<r; i++)        {            int a,b;            scanf("%d%d",&a,&b);            Map[a].push_back(b);            Map[b].push_back(a);        }        tarjan(1,-1);        for(int i=1; i<=n; i++)        {            int len=Map[i].size();            for(int j=0; j<len; j++)            {                int v=Map[i][j];                if(low[v]!=low[i])                {                    d[low[v]]++,d[low[i]]++;                }            }        }        for(int i=1; i<=n; i++)        {            if(d[i]==2) k++;        }        printf("%d\n",(k+1)/2);    }    return 0;}
其实上面的方法也不太安全,在同一个边双连通分量中的low值未必是相等的,所以下面是比较谨慎的代码,先标记桥,然后进行一次dfs。
code:
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#define MMAX 5005using namespace std;int F,R;vector<int>Map[MMAX];int dfn[MMAX],low[MMAX],index,belong[MMAX],degree[MMAX],cc;bool m[MMAX][MMAX];void init(){    index=1;    memset(m,false,sizeof(m));    memset(low,0,sizeof(low));    memset(dfn,0,sizeof(dfn));    memset(degree,0,sizeof(degree));    memset(belong,0,sizeof(belong));    for(int i=0; i<=F; i++)    {        Map[i].clear();    }}void tarjan(int u,int fa){    dfn[u]=low[u]=index++;    int len=Map[u].size();    for(int i=0; i<len; i++)    {        int v=Map[u][i];        if(v!=fa)        {            if(!dfn[v])            {                tarjan(v,u);                low[u]=min(low[u],low[v]);                if(low[v]>dfn[u])                {                    m[u][v]=m[v][u]=true;                }            }            else            {                low[u]=min(low[u],dfn[v]);            }        }    }}void dfs(int u){    int len=Map[u].size();    for(int i=0; i<len; i++)    {        int v=Map[u][i];        if(!m[u][v]&&!belong[v])        {            belong[v]=cc;            dfs(v);        }    }}int main(){    while(~scanf("%d%d",&F,&R))    {        init();        for(int i=0; i<R; i++)        {            int a,b;            scanf("%d%d",&a,&b);            Map[a].push_back(b);            Map[b].push_back(a);        }        tarjan(1,-1);        cc=0;        for(int i=1; i<=F; i++)        {            if(!belong[i])            {                cc++;                belong[i]=cc;                dfs(i);            }        }        for(int i=1; i<=F; i++)        {            int len=Map[i].size();            for(int j=0; j<len; j++)            {                int v=Map[i][j];                if(belong[i]!=belong[v])                {                    degree[belong[i]]++,degree[belong[v]]++;                }            }        }        int cnt=0;        for(int i=1; i<=cc; i++)        {            if(degree[i]==2)cnt++;        }        printf("%d\n",(cnt+1)/2);    }    return 0;}

0 0
原创粉丝点击