poj 2117 判断删除一个点后的最大联通分支个数

标签: 割点
6人阅读 评论(0) 收藏 举报
分类:
Electricity
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6019 Accepted: 1919

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

题意:  给你n个点和m 条边 组成的一个图  那么你需要求出删除一个点后最大的联通分支。

思路:  直接用tarjan  打出 删除某一个点后的联通分支的增加量。    然后找出联通分支量最大的那个点   并且找到原本的联通分支的个数。  这里有个坑点就是  必须要删除一个点   那么如果  n为x  m 为0   就会出现删除一个点后的最大联通分支个数 小于原来的的联通分支个数。


代码: 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>

using namespace std;

const int N=10005;
const int M=200010;

struct Edge
{
	int to;
	bool cut;
	int next;
}edge[M];

int head[N],tot;
int low[N],dfn[N],Stack[N];
bool instack[N];
int index,top;
bool cut[N];
int add_block[N];
int bridge;

int n,m;

void add(int u,int v)
{
	edge[++tot].to=v;
	edge[tot].cut=0;
	edge[tot].next=head[u];
	head[u]=tot;
	return ;
}

void init()
{
	tot=0;
	memset(head,-1,sizeof(head));
	
	memset(dfn,0,sizeof(dfn));
	memset(instack,0,sizeof(instack));
	memset(add_block,0,sizeof(add_block));
	memset(cut,0,sizeof(cut));
	index=top=0;
	bridge=0;
}


void tarjan(int u,int pre)
{
	int v;
	low[u]=dfn[u]=++index;
	Stack[top++]=u;
	instack[u]=1;
	
	int son=0;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		v=edge[i].to;
		if(v==pre) continue;
		
		if(!dfn[v])
		{
			son++;
			tarjan(v,u);
			if(low[v]<low[u]) low[u]=low[v];
			
			if(low[v]>dfn[u]){
				bridge++;
				edge[i].cut=1;
				edge[i^1].cut=1;
			}
			
			if(u!=pre&&low[v]>=dfn[u]){
				cut[u]=1;
				add_block[u]++;
			}
		}
		else if(dfn[v]<low[u]){
			low[u]=dfn[v];
		}
	}
	
	if(u==pre&&son>1) cut[u]=1;
	if(u==pre) add_block[u]=son-1;
	instack[u]=0;
	top--;
	
	return ;
}

int main()
{
	int u,v;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		if(n==0&&m==0) break;
		init();
		
		for(int i=1;i<=m;i++)
		{
			scanf("%d %d",&u,&v);
			u++; v++;
			add(u,v);
			add(v,u);
		}
		
		memset(dfn,0,sizeof(dfn));
	    memset(instack,0,sizeof(instack));
	    memset(add_block,0,sizeof(add_block));
	    memset(cut,0,sizeof(cut));
	    index=top=0;
		
		int ans=0;
		for(int i=1;i<=n;i++){
			if(!dfn[i]){
				ans++;
				tarjan(i,i);
			}
		}
		int maxx=-1;
		for(int i=1;i<=n;i++)
		{
			if(add_block[i]>maxx) maxx=add_block[i];
		}
		
		ans+=maxx;
		printf("%d\n",ans);
		
	}
	
	return 0;
}

查看评论

github入门和git功能分支工作流

介绍github使用入门和git功能分支开发流程以及实操演示
  • 2018年03月05日 11:36

无向图的割点(POJ 2117)

  • 2009年08月19日 00:00
  • 2KB
  • 下载

poj 2117 Electricity (无向图割点去除后最大连通分支数)

题意:求去除一点后,形成的连通分支数的最大值。(使最多的网络不能跟原路线相连) 顶点u是割项当且仅满足 (1) 或 (2)时: (1) 若u是树根,且u的孩子数 son>1 。因为没有u的...
  • LiWen_7
  • LiWen_7
  • 2012-08-29 11:44:39
  • 2262

POJ2117.Electricity——无向图的割点

http://poj.org/problem?id=2117题目描述: 求删除一个点后,图中最多有多少个连通块#include #include #include #include #include...
  • u014141559
  • u014141559
  • 2015-04-08 14:19:37
  • 514

Kosaraju 算法求解一个有向图的强连通分支个数

基本介绍网上看了很多关于求解一个有向图的强连通分支个数的算法,其中最著名的莫过于: Kosaraju 算法看的比较晕!过程如下: 1。 创建一个空的栈 S,并做一次 DFS 遍历。在 DFS 遍...
  • starstar1992
  • starstar1992
  • 2016-11-02 21:53:45
  • 1003

POJ2117-Electricity

题目链接 题意: 求出删除一个点之后,连通块最多有多少 思路:数组记录每个点删除后的连通块有多少个,注意图不一定是连通的。 代码: #include #include...
  • u011345461
  • u011345461
  • 2014-10-13 17:03:46
  • 896

POJ 2117 Electricity(无向图割点)

POJ 2117 Electricity(无向图割点) http://poj.org/problem?id=2117 题意:给你一个无向图(不一定连通),现在问你从该图中删除任意一个顶点之后,该无向图...
  • u013480600
  • u013480600
  • 2014-06-15 11:45:10
  • 975

[ACM] POJ 2186 Popular Cows (强连通分量,Kosaraju算法知识整理)

首先是一些知识整理:来源于网络: 以下转载于:http://blog.sina.com.cn/s/blog_4dff87120100r58c.html   Kosaraju算法是求解有向图强...
  • sr19930829
  • sr19930829
  • 2014-09-25 16:10:47
  • 2010

带权最大联通子树

树中的每个结点都有权重,编号从1~N,要求选出包含编号为1的共Mge
  • flyHighflyAway
  • flyHighflyAway
  • 2014-11-01 16:33:08
  • 607

树的最大连通分支问题

  • 2013年03月04日 23:24
  • 681KB
  • 下载
    个人资料
    持之以恒
    等级:
    访问量: 8620
    积分: 1252
    排名: 3万+
    最新评论