无向连通图求割点(tarjan算法去掉改割点剩下的联通分量数目)

poj2117

Electricity
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3603   Accepted: 1213

Description

Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them supplying a small area that surrounds it. This organization brings a lot of problems - it often happens that there is not enough power in one area, while there is a large surplus in the rest of the country. 

ACM++ has therefore decided to connect the networks of some of the plants together. At least in the first stage, there is no need to connect all plants to a single network, but on the other hand it may pay up to create redundant connections on critical places - i.e. the network may contain cycles. Various plans for the connections were proposed, and the complicated phase of evaluation of them has begun. 

One of the criteria that has to be taken into account is the reliability of the created network. To evaluate it, we assume that the worst event that can happen is a malfunction in one of the joining points at the power plants, which might cause the network to split into several parts. While each of these parts could still work, each of them would have to cope with the problems, so it is essential to minimize the number of parts into which the network will split due to removal of one of the joining points. 

Your task is to write a software that would help evaluating this risk. Your program is given a description of the network, and it should determine the maximum number of non-connected parts from that the network may consist after removal of one of the joining points (not counting the removed joining point itself). 

Input

The input consists of several instances. 

The first line of each instance contains two integers 1 <= P <= 10 000 and C >= 0 separated by a single space. P is the number of power plants. The power plants have assigned integers between 0 and P - 1. C is the number of connections. The following C lines of the instance describe the connections. Each of the lines contains two integers 0 <= p1, p2 < P separated by a single space, meaning that plants with numbers p1 and p2 are connected. Each connection is described exactly once and there is at most one connection between every two plants. 

The instances follow each other immediately, without any separator. The input is terminated by a line containing two zeros. 

Output

The output consists of several lines. The i-th line of the output corresponds to the i-th input instance. Each line of the output consists of a single integer C. C is the maximum number of the connected parts of the network that can be obtained by removing one of the joining points at power plants in the instance.

Sample Input

3 3
0 1
0 2
2 1
4 2
0 1
2 3
3 1
1 0
0 0

Sample Output

1
2
2
题目大题:

求去掉某个点以及与其相连的边,最多可以形成多少个连通分量:

tarjan算法求割点

程序:

#include"string.h"
#include"stdio.h"
#include"iostream"
#include"queue"
#include"stack"
#define M 10009
#include"stdlib.h"
#include"math.h"
#define inf 99999999
using namespace std;
struct node
{
    int u,v,next;
}edge[M*20];
int t,head[M],low[M],dfn[M],index,cut[M],sum,root,s,num[M];
//cut[]可以记录去掉该节点后导致形成多少个连通分量
//num[]可以记录以改点为根的连通图有多少个元素
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].next=head[u];
    head[u]=t++;
}
void tarjan(int u,int fa)
{
    s++;
    dfn[u]=low[u]=++index;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u])
                cut[u]++;
        }
        else
            low[u]=min(low[u],dfn[v]);
    }
    if(fa<0)
        cut[u]--;
}
void solve(int n)
{
    index=sum=0;
    memset(dfn,0,sizeof(dfn));
    memset(cut,0,sizeof(dfn));
    for(int i=1;i<=n;i++)
        if(!dfn[i])
        {
            sum++;
            s=0;
            root=i;
            tarjan(i,-1);
            num[i]=s;
        }

}
int main()
{
    int a,b,n,m,i;
    while(scanf("%d%d",&n,&m),n||m)
    {
        init();
        while(m--)
        {
            scanf("%d%d",&a,&b);
            a++;
            b++;
            add(a,b);
            add(b,a);
        }
        solve(n);
        int ans=0;
        int tep=-1;
        for(i=1;i<=n;i++)
        {
            if(cut[i])
            {
                if(ans<cut[i]+1)
                {
                    ans=cut[i]+1;
                    tep=i;
                }
            }
        }
        //注意:当所有联通分量的元素个数都是1的时候去掉一个元素则联通分量减小
        if(tep==-1)
        {
            int flag=0;
            for(i=1;i<=n;i++)
            {
                if(num[i]>1)
                    flag++;
            }
            if(flag)
                printf("%d\n",sum);
            else
                printf("%d\n",sum-1);
        }
        else
        printf("%d\n",ans+sum-1);
    }
}


posted @ 2014-07-09 21:22  一样菜  阅读(356)  评论(0编辑  收藏  举报