您的位置:首页 > 其它

poj 2117 Electricity 求无向图中去掉一个点后最大的联通分支数 无向图有可能不联通 tarjan求割点模板

2011-07-07 11:42 375 查看
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).
InputThe 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.
OutputThe 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


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//求无向图中去掉一个点后最大的联通分支数
const int maxn=10000+200;
struct edge
{
int t,w;
int next;
};
int V,E;
int p[maxn];
edge G[maxn*10];
int l;
void init()
{
memset(p,-1,sizeof(p));
l=0;
}
void addedge(int u,int t,int w,int l)
{
G[l].t=t;
G[l].w=w;
G[l].next=p[u];
p[u]=l;
}
int connect,maxCut;//原图联通分支数,去掉割点后增加的最大联通分支数
int vis[maxn];//无向图有可能不联通
//tarjan 求割点 割边
int cut[maxn];//cut[i]非0表示i是割点
int color[maxn];//颜色:0表示没有访问,1表示正在访问,2表示访问结束
int lowc[maxn];//表示i及i的子孙相连的辈分最高的祖先节点所在的深度
int d[maxn];//表示i节点在树中的深度
int root;//根节点
int fath;//父节点
int pcnt;//割点个数
int egcnt;//割边个数
void dfs(int u,int fath,int deep)
{
vis[u]=1;
color[u]=1;//正在访问
lowc[u]=d[u]=deep;//深度
int tot=0;//子树个数
for(int i=p[u];i!=-1;i=G[i].next)
{
int t=G[i].t;
if(t!=fath&&color[t]==1)
{
lowc[u]=min(lowc[u],d[t]);
}
if(color[t]==0)
{
dfs(t,u,deep+1);
tot++;//子树加1
lowc[u]=min(lowc[u],lowc[t]);
//求割点
if((u==root&&tot>1)||(u!=root&&lowc[t]>=d[u]))
cut[u]++;//不能将pscnt++写到这里 cut[u]++表示去除u后增加的联通分支数
//求割边
//if(lowc[t]>d[u]) edge[u][t]=true; u->t是割边
}
}
color[u]=2;
}
void calc(int rt)
{
pcnt=egcnt=0;
// memset(cut,0,sizeof(cut));
// memset(color,0,sizeof(color));
// memset(lowc,0,sizeof(lowc));
// memset(d,0,sizeof(d));
root=rt;
dfs(root,-1,1);
//for(int i=1;i<=V;i++) if(cut[i]) pcnt++;
}
int main()
{
while(scanf("%d%d",&V,&E)==2&&V)
{
if(E==0)//如果没有边,则联通分支数-1
{
printf("%d\n",V-1);
continue;
}
init();
for(int i=0;i<E;i++)
{
int u,t,w=1;
scanf("%d%d",&u,&t);u++,t++;//从1开始
addedge(u,t,w,l++);
addedge(t,u,w,l++);
}
//无向图可能不联通
memset(vis,0,sizeof(vis));
memset(cut,0,sizeof(cut));
memset(color,0,sizeof(color));
memset(lowc,0,sizeof(lowc));
memset(d,0,sizeof(d));
connect=0,maxCut=0;
for(int i=1;i<=V;i++)
{
if(!vis[i])
{
connect++;
calc(i);
}
maxCut=max(maxCut,cut[i]);
}
printf("%d\n",connect+maxCut);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: