poj 2117

来源:互联网 发布:java中sleep函数 编辑:程序博客网 时间:2024/06/06 12:53

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 30 10 22 14 20 12 33 11 00 0

Sample Output

122题目大意:给你一个无向图(不一定连通),然后问你删去一个点后,有几个连通块。分析:这里点不多,所以直接选择暴力枚举,套个Tarjan算法就A了。#include<cstring>#include<cstdio>using namespace std;#define inf 123457890#define N 100005int n,m,clock,id,sum;int pre[N],cnt[N],head[N],low[N];struct Node{    int next,v;}e[N];void add(int u,int v){    e[id].v=v;e[id].next=head[u];head[u]=id++;}int read(){    int x=0,f=1;char ch=getchar();    for (;ch>'9'||ch<'0';ch=getchar()) if (ch=='-') f=-1;    for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';    return x*f; }int max(int A,int B){    return A>B? A:B;}int min(int A,int B){    return A>B? B:A;}#define to e[i].vint tarjan(int u,int fa){    int lowu=pre[u]=++clock;    int child=0;    for (int i=head[u];i!=-1;i=e[i].next)    {        if (!pre[to])         {            child++;            int lowv=tarjan(to,u);            lowu=min(lowu,lowv);            if (lowv>=pre[u]) cnt[u]++;        }        else if (pre[to]<pre[u]&&to!=fa) lowu=min(lowu,pre[to]);    }    if (fa<0&&child==1) cnt[u]=0;    return low[u]=lowu;}int main(){    for (;;)    {        n=read();m=read();        if (n==0&&m==0) break;        id=1;sum=clock=0;    //    memset(e,0,sizeof(e));        memset(cnt,0,sizeof(cnt));        memset(pre,0,sizeof(pre));        memset(low,0,sizeof(low));        memset(head,-1,sizeof(head));        for (int i=1;i<=m;i++)         {            int u,v;u=read();v=read();            add(u,v);add(v,u);        }        for (int i=0;i<n;i++)        if (!pre[i])        {            sum++;            tarjan(i,-1);            cnt[i]--;        }        int Mx_cnt=-inf;        for (int i=0;i<n;i++) Mx_cnt=max(Mx_cnt,cnt[i]);        printf("%d\n",sum+Mx_cnt);    }     return 0;} 
0 0
原创粉丝点击