POJ-3352-无向图的割顶和桥-求边-双连通分量

来源:互联网 发布:辅助软件如何制作 编辑:程序博客网 时间:2024/05/16 01:45

Road Construction

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 ton. 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 labelledv 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,如果删除某个点u后,连通分量数目增加,则称u为图的关节点或割顶

点-双连通:对于一个连通图,如果任意两点至少存在两条“点不重复”的路径,则说这个图是点-双连通的(简称双连通),这个要求等价于任意两条边都在同一个简单环中,即内部无割顶。

边-双连通:如果任意两点至少存在两条“边不重复”的路径,我们说这个图是边-双连通的

思路:将各个双连通分量看做缩点,最后将叶子的数目加一除以2就是答案(至于为什么不知道难过,当时看了别人的代码)


#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>#define MAX 1005using namespace std;vector<int> G[MAX];int pre[MAX];   //时间戳int low[MAX];   // 结点u及其后代所能连回的最早的祖先的pre值int iscut[MAX];  //记录结点是否为割点int dfs_clock;int dfs(int u,int fa)   //u在dfs树中的父结点是fa{    int lowu=pre[u]=++dfs_clock;    int child=0;        //子结点数目    for(int i=0; i<G[u].size(); i++)    {        int v=G[u][i];        if(!pre[v])     //没有访问过v        {            child++;            int lowv=dfs(v,u);      //lowv指v及其后代能连回的最早的祖先的pre值            lowu=min(lowu,lowv);     //用后代的low函数更新u的low函数            if(lowv>=pre[u])      //说明u是一个割顶            {                iscut[u]=1;            }        }        else if(pre[v]<pre[u]&&v!=fa)       //说明不是树边        {            lowu=min(lowu,pre[v]);          //用反向边更新u的low函数        }    }    if(fa<0&&child==1)        //判断树根是不是割顶        iscut[u]=0;    low[u]=lowu;    return lowu;}void start(int n){    int bcc_cnt=0;    dfs_clock=0;    memset(pre,0,sizeof(pre));    memset(low,0,sizeof(low));    memset(iscut,0,sizeof(iscut));}int main(){    int n,r,i,a,b,ans,j,deg[MAX];    while(~scanf("%d%d",&n,&r))    {        for(i=1;i<=n;i++)      //注意要清空            G[i].clear();        ans=0;        memset(deg,0,sizeof(deg));        for(i=1; i<=r; i++)        {            scanf("%d%d",&a,&b);            G[a].push_back(b);            G[b].push_back(a);        }        start(n);        dfs(1,-1);      //初始化树根的fa为-1        for(i=1; i<=n; i++)        {            for(j=0; j<G[i].size(); j++)            {                if(low[i]!=low[G[i][j]])                    deg[low[i]]++;       //记录缩点的出度            }        }        for(i=1; i<=n; i++)            if(deg[i]==1)       //出度为一说明是叶子结点                ans++;        printf("%d\n",(ans+1)/2);    }    return 0;}



0 0
原创粉丝点击