POJ 2117 Electricity

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5573   Accepted: 1818

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

Source

CTU Open 2004

题目大意

求去除一个点后 最多有几个连通块

Tarjan求割点

屠龙宝刀点击就送

#include <cstring>
#include <ctype.h>
#include <cstdio>
#define N 20005
void read(int &x)
{
    x=0;char ch=getchar();
    for(;!isdigit(ch);ch=getchar());
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-‘0‘;
}
struct Edge
{
    int next,to;
    Edge (int next=0,int to=0) :next(next),to(to) {}
}edge[N<<1];
bool vis[N],cutpoint[N];
int sumcol,ans,head[N],cnt=-1,n,m,dfn[N],low[N],tim;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=-1;
    memset(edge,0,sizeof(edge));
    ans=0;
    sumcol=0;
    tim=0;
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
}
void insert(int u,int v)
{
    edge[++cnt]=Edge(head[u],v);
    head[u]=cnt;
}
int max(int a,int b) {return a>b?a:b;}
int min(int a,int b) {return a>b?b:a;}
void tarjan(int x,int pre)
{
    vis[x]=1;
    low[x]=dfn[x]=++tim;
    int sum=0;
    int tot=0;
    for(int u=head[x];u!=-1;u=edge[u].next)
    {
        if(!vis[edge[u].to])
        {
            sum++;
            tarjan(edge[u].to,x);
            low[x]=min(low[x],low[edge[u].to]);
            if(low[edge[u].to]>=dfn[x]) tot++;
        }
        else if(dfn[edge[u].to]<dfn[x]&&edge[u].to!=pre) low[x]=min(low[x],dfn[edge[u].to]);
    }
    if(pre==-1) {if(sum>1) ans=max(ans,sum-1);}
    else ans=max(ans,tot);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        init();
        if(m==0)
        {
            printf("%d\n",n-1);
            continue;
        }
        for(int x,y;m--;)
        {
            read(x);
            read(y);
            insert(x,y);
            insert(y,x);
        }
        for(int i=0;i<n;i++)
        {
            if(!vis[i])
            {
                sumcol++;
                tarjan(i,-1);
            }
        }
        printf("%d\n",sumcol+ans);
    }
    return 0;
}
时间: 2024-10-12 17:51:34

POJ 2117 Electricity的相关文章

poj 2117 Electricity(tarjan求割点删掉之后的连通块数)

题目链接:http://poj.org/problem?id=2117 题意:求删除一个点后,图中最多有多少个连通块. 题解:就是找一下割点,根节点的割点删掉后增加son-1(son为子树个数),非根节点删掉之后++ #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N = 1e4 + 10; const int M = 1e6 + 10; st

poj 2117 Electricity 【无向图求割点】【求去掉一个点后 图中最多的BCC数目】

Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4597   Accepted: 1515 Description Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them sup

poj 2117 Electricity【点双连通求删除点后最多的bcc数】

Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4727   Accepted: 1561 Description Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them sup

POJ 2117 (割点+连通分量)

题目链接: http://poj.org/problem?id=2117 题目大意:在一个非连通图中,求一个切除图中任意一个割点方案,使得图中连通分量数最大. 解题思路: 一个大陷阱,m可以等于0,这时候要特判,结果就是n-1. 同时出题者脑子秀逗了,也不给C的范围.我开了两倍点大小RE了,于是怒开了五倍点大小才A了. 本题不是连通图,需要先计算原始图中的连通分量.方法就是dfs染色. 然后dfs求割点. 之后枚举割点,由于是非连通图,所以连通分量数=原始分量数+block-1. -1的原因是,

【POJ】2117 Electricity

无向图求割点和连通块. 1 /* POJ2117 */ 2 #include <iostream> 3 #include <vector> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstring> 7 #include <cstdlib> 8 using namespace std; 9 10 #define MAXN 10005 11 12 vector<i

POJ——T2117 Electricity

 http://poj.org/problem?id=2117 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5459   Accepted: 1788 当m==0 时,特判输出 n-1~~~ 先求出原始的连通分量, 然后只有删割点才会增加连通分量,枚举每个割点 最后加上原始的 1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio

POJ 2117 求无向图的割点

Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4332   Accepted: 1419 Description Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them sup

无向图求割顶与桥

无向图求割顶与桥 对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或割顶.对于连通图,割顶就是删除之后使图不再连通的点.如果删除边(u,v)一条边,就可以让连通图变成不连通的,那么边(u,v)是桥. 具体的概念和定义比较多,在刘汝佳<<训练指南>>P312-314页都有详细的介绍. 下面来写求无向图割顶和桥的DFS函数.我们令pre[i]表示第一次访问i点的时间戳,令low[i]表示i节点及其后代所能连回(通过反向边)的最早祖先的pre值. 下面的dfs函数返回

ACM第一阶段学习内容

一.知识目录 字符串处理 ................................................................. 3 1.KMP 算法 ............................................................ 3 2.扩展 KMP ............................................................ 6 3.Manacher 最长回文子串 .......