poj 1144 Network 【求无向图中割点总数】【点双联通入门题目】

原创 2015年07月08日 16:31:44

Network
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10441   Accepted: 4850

Description

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure
occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.

Input

The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;

Output

The output contains for each block except the last in the input file one line containing the number of critical places.

Sample Input

5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0

Sample Output

1
2

Hint

You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.

题意:给出节点数N,然后下面最多N行输入边的信息,每行包括一个地方的号码,以及有直接线路通往其他地方的号码。每个网络信息的最后一行为数字0,表示该网络数据的结束。输入文件最后一行为0,表示输入文件结束。       让 你  求出无向网络中割点总数。

tarjan算法:只需注意输入字符串时建边的处理

自己写的代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define MAXN 1000+10
#define MAXM 2000000+10
using namespace std;
struct Edge
{
	int from, to, next;
}edge[MAXM];
int head[MAXN], top;
bool iscut[MAXN];//判断是否为割点
int low[MAXN];//从当前节点或它的子孙出发通过回边可以到达的最低深度优先数 
int dfn[MAXN];//记录该点在DFS树中的深度优先数
int recdfn;//记录当前的深度优先序数 
void init()
{
	top = 0;
	memset(head, -1, sizeof(head));
} 
void addEdge(int u, int v)
{
	Edge E = {u, v, head[u]};
	edge[top] = E;
	head[u] = top++;
	Edge E1 = {v, u, head[v]};
	edge[top] = E1;
	head[v] = top++;
}
void getMap(int m)
{
	int a, b;
	while(m--)
	{
		scanf("%d%d", &a, &b);
		addEdge(a, b);
	}
}
void tarjan(int u, int fa)//u为当前节点 fa为其父节点 
{
	low[u] = dfn[u] = ++recdfn;
	int son = 0;//记录子节点数目 
	for(int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if(!dfn[v])//没有查询过 
		{
			son++;
			tarjan(v, u);//求low[v]
		    low[u] = min(low[u], low[v]);//取子节点中low最小值
		    if(u != fa && low[v] >= dfn[u])//该割点不是根节点 
			iscut[u] = true;
		}
		else low[u] = min(low[u], dfn[v]);//更新通过回边到达的最低深度优先数 
	} 
	if(u == fa && son > 1)//割点是根节点 且子节点数目大于1
	iscut[u] = true;
}
void find_cut(int l, int r)//节点最小的编号 到 节点最大的编号 
{
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	memset(iscut, 0, sizeof(iscut));
	recdfn = 0;
	for(int i = l; i <= r; i++) if(!dfn[i]) tarjan(i, i);
}
int main()
{
	int N;
	int a, b;
	int i, l;
	char str[5010];
	while(scanf("%d", &N), N)
	{
		init();
		while(gets(str), strcmp(str, "0"))
		{
			l = strlen(str);
			a = 0; i = 0;
			while(str[i] != ' ')
			{
				a = a * 10 + str[i] - '0';
				++i;
			}
			for( ; i < l; i++)
			{
				if(str[i] != ' ')//空格ascll码为32 
				{
					b = 0;
					while(str[i] != ' ' && i < l)
					{
						b = b * 10 + str[i] - '0';
						++i;
					}
					addEdge(a, b);
				}
			}
		}
		find_cut(1, N);
		int ans = 0;
		for(i = 1; i <= N; i++)
		{
			if(iscut[i])
			ans++;
		}
		printf("%d\n", ans);
	} 
	return 0;
} 


刘汝佳白书带缩点 求割点模板:


#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define MAXN 100+10
#define MAXM 10000+10
using namespace std;
struct Edge
{
	int from, to, next;
}edge[MAXM];
int head[MAXN], top;//存储指针 
int add_bcc[MAXN];//去掉该点增加的bcc数目
int dfn[MAXN];//该点的深度优先数
int low[MAXN];//从该点或它的子孙出发 通过回边可以到达的最低深度优先数 
bool iscut[MAXN];//该点是否为割点 
int belong[MAXN];//该点属于哪个bcc 
int dfs_clock, bcc_cnt;
stack<Edge> S;//存储当前bcc中的边
vector<int> bcc[MAXN];
void init()
{
	top = 0;
	memset(head, -1, sizeof(head));
} 
void addEdge(int u, int v)
{
	Edge E = {u, v, head[u]};
	edge[top] = E;
	head[u] = top++;
	Edge E1 = {v, u, head[v]};//无向图需要反向建边
	edge[top] = E1;
	head[v] = top++; 
}
int tarjan(int u, int fa)//u在DFS树中的父节点是fa 
{
	low[u] = dfn[u] = ++dfs_clock;
	int child = 0;
	for(int i = head[u]; i != -1; i = edge[i].next)
	{
		Edge E = edge[i];
		int v = E.to;
		if(!dfn[v])
		{
			S.push(E);
			child++;
			low[v] = tarjan(v, u);
			low[u] = min(low[u], low[v]);
			if(low[v] >= dfn[u])
			{
				iscut[u] = true;
				add_bcc[u]++;//增加一个bcc 
				bcc_cnt++;
				bcc[bcc_cnt].clear();
				for(;;)
				{
					Edge x = S.top(); S.pop();
					if(belong[x.from] != bcc_cnt)
					{
						bcc[bcc_cnt].push_back(x.from);
						belong[x.from] = bcc_cnt;
					}
					if(belong[x.to] != bcc_cnt)
					{
						bcc[bcc_cnt].push_back(x.to);
						belong[x.to] = bcc_cnt;
					}
					if(x.from == u && x.to == v) break;
				}
			} 
		}
		else if(dfn[v] < dfn[u] && v != fa)
		{
			S.push(E);
			low[u] = min(low[u], dfn[v]);
		}
	}
	if(fa < 0 && child == 1) iscut[u] = 0;
	return low[u];
}
void find_cut(int l, int r)//左右区间 根据数据可以进行变化 
{
	memset(add_bcc, 0, sizeof(add_bcc));
	memset(iscut, 0, sizeof(iscut));
	memset(low, 0, sizeof(low));
	memset(belong, 0, sizeof(belong)); 
	memset(dfn, 0, sizeof(dfn));
	dfs_clock = bcc_cnt = 0;
	for(int i = l; i <= r; i++)//从左到右
	if(!dfn[i]) tarjan(i, -1); 
} 
int main()
{
	int N;
	int a, b;
	int i, l;
	char str[5010];
	while(scanf("%d", &N), N)
	{
		init();
		while(gets(str), strcmp(str, "0"))
		{
			l = strlen(str);
			a = 0; i = 0;
			while(str[i] != ' ')
			{
				a = a * 10 + str[i] - '0';
				++i;
			}
			for( ; i < l; i++)
			{
				if(str[i] != ' ')//空格ascll码为32 
				{
					b = 0;
					while(str[i] != ' ' && i < l)
					{
						b = b * 10 + str[i] - '0';
						++i;
					}
					addEdge(a, b);
				}
			}
		}
		find_cut(1, N);
		int ans = 0;
		for(i = 1; i <= N; i++)
		{
			if(iscut[i])
			ans++;
		}
		printf("%d\n", ans);
	} 
	return 0;
} 



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 1144 Network 无向图求割点

来源:http://poj.org/problem?id=1144 题意:就是给你一些点,某些点之间有边。求有多少个点是割点。 思路:模板题目了,直接用无向图求个点模板就可以ac。需要注意的是输入...
  • wmn_wmn
  • wmn_wmn
  • 2012年08月25日 10:59
  • 3398

poj1144Network 无向图求割点Tarjan

n个点,组成一个无向图,求这个图中割点的数量。模板题。 只是这道题在输入数据的时候有点麻烦,如样例中,第一组数据有五个点,5 1 2 3 4 表示5这个点与1 2 3 4 点相连。其中这个图的割点...

poj 1144 Network (无向图求割点)

#include #include #include using namespace std; vector vec[110]; int dfn[110] , low[110],in[110]; i...
  • LiWen_7
  • LiWen_7
  • 2012年08月31日 15:13
  • 919

求无向图的割点 (poj 1144 Network)

割点 :去掉该点后原来的图不连通(出现好几个连通分量),该点被称为割点。 注意删除某点意味着和该点关联的边也全部删除 求割点的伪代码 DFS(v1,father): dfn[v1...

POJ 1144 Network 无向图求割点Tarjan

题目描述:Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They...

基于DFS求无向图的割点及桥(割边)算法总结 POJ_1144题解

1.割点,桥(割边)定义: 若v2(v1的后继节点)有且仅有反向边最远连接到v1,那么删除v1后不连通,v1是割点。作为一种特殊情况,如果v2及其后代通过反向边只能连回v2自己,那么只要删除edge...
  • zl_130
  • zl_130
  • 2015年08月13日 16:43
  • 153

POJ 1144 Network 求割点

Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14235 Accepted: 646...

POJ 1144 Network 图论 求割点模板

这道题目是个简单的割点模板题目。就直接贴代码了。 考虑一下输入就行,那个比较麻烦。#include #include #include #include using namespace st...

【POJ 1144】Network(Tarjan求割点)

红叶黄花秋意晚,千里念行客

POJ 1144 & Uva 315 Network 【求割点数目】

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10855   Accep...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 1144 Network 【求无向图中割点总数】【点双联通入门题目】
举报原因:
原因补充:

(最多只允许输入30个字)