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
  • 3537

poj 1144 Network 无向连通图求割点

Problem: 给定一个连通图,求割点的数目。
  • cFarmerReally
  • cFarmerReally
  • 2016年10月27日 20:50
  • 249

POJ 1144 Network(求无向图中的割点)

题目链接:http://poj.org/problem?id=1144 Network Time Limit: 1000MS Memory Limit: 10000K Total Su...
  • qq_32866009
  • qq_32866009
  • 2017年08月11日 19:53
  • 82

无重边连通无向图求割点和桥

一.割点和桥  无向连通图中,如果删除某点后,图变 成不连通,则称该点为割点。    无向连通图中,如果删除某边后,图变 成不连通,则称该边为桥。 所以说啊,割点和桥这个概念的应该范围应该只是在...
  • u013555159
  • u013555159
  • 2016年09月03日 21:01
  • 484

图论——寻找无向连通图割点算法

查看原文:http://www.wyblog.cn/2016/12/20/%e5%9b%be%e8%ae%ba-%e5%af%bb%e6%89%be%e6%97%a0%e7%9b%b8%e8%bf%9...
  • wy250229163
  • wy250229163
  • 2016年12月20日 20:51
  • 1447

POJ 1144 Network 求割点

Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14235 Accepted: 646...
  • ywq12138
  • ywq12138
  • 2017年07月14日 09:44
  • 110

POJ 1144--Network 【无向图中割点总数】

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10764   Accepted: 4978 ...
  • hpuhjh
  • hpuhjh
  • 2015年08月12日 17:56
  • 487

POJ 1144 Network 求割点

点击打开链接 Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8297   Accepte...
  • Dinivity123
  • Dinivity123
  • 2013年11月10日 21:26
  • 1293

POJ -1144 Network 求割点

                                      NetworkA Telephone Line Company (TLC) is establishing a new te...
  • qq_41222001
  • qq_41222001
  • 2018年02月22日 17:46
  • 10

【POJ 1144】 Network(割点入门)

【POJ 1144】 Network(割点入门) Network Time Limit: 1000MS   Memory Limit: 10000K To...
  • ChallengerRumble
  • ChallengerRumble
  • 2016年02月20日 14:59
  • 1382
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 1144 Network 【求无向图中割点总数】【点双联通入门题目】
举报原因:
原因补充:

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