关闭

poj 1532 【求无向图的所有割点 以及 该点分成的BCC数目】

312人阅读 评论(0) 收藏 举报
分类:

SPF

Time Limit: 1000MS

  Memory Limit: 10000K
Total Submissions: 6921   Accepted: 3160

Description

Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible.

Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate.

Input

The input will contain the description of several networks. A network description will consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers will range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should be ignored.

Output

For each network in the input, you will output its number in the file, followed by a list of any SPF nodes that exist.

The first network in the file should be identified as "Network #1", the second as "Network #2", etc. For each SPF node, output a line, formatted as shown in the examples below, that identifies the node and the number of fully connected subnets that remain when that node fails. If the network has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes.


Sample Input

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

1 2
2 3
3 4
4 5
5 1
0

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

0

Sample Output

Network #1
  SPF node 3 leaves 2 subnets

Network #2
  No SPF nodes

Network #3
  SPF node 2 leaves 2 subnets
  SPF node 3 leaves 2 subnets
题意:在无向图中,给出一些边的信息如输入a b表明a和b之间有边。当输入a的为0时,若当前输入的是第一条边那么就结束输入,反之则说明一组测试数据输入结束,你需要求出该图中所有的割点以及该点把图分成几个BCC,若不存在割点输出No SPF nodes.
tarjan算法:
用low[]表示从该点或它的子孙出发 通过回边可以到达的最低深度优先数 
更新low有三点:均在tarjan中实现
1,该点本身的深度优先数
2,它的子女中最低深度优先数
3,该点通过回边可以到达的最低优先数
用dfn[]表示该点的深度优先数。那么则有:
当割点u为根节点时,它的子节点数目必须有两个以上,而它的根节点数目就是BCC数目;
当割点u为非根节点时,若有d个子女w,使得low[w] >= dfn[u],那么去掉u则分成d+1个BCC。
知道这些就OK了
自己写的代码:16ms 
#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;//记录当前的深度优先序数 
int add_bcc[MAXN];//去掉该点增加的BCC数目
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;
				add_bcc[u]++;//增加bcc数目为其节点数 
			} 
		}
		else low[u] = min(low[u], dfn[v]);//更新通过回边到达的最低深度优先数 
	} 
	if(u == fa && son > 1)//割点是根节点 且子节点数目大于1
	{
		iscut[u] = true;
		add_bcc[u] = son - 1;//增加的bcc数目为节点数减一 
	} 
}
void find_cut(int l, int r)//节点最小的编号 到 节点最大的编号 
{
	memset(add_bcc, 0, sizeof(add_bcc));
	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 Min, Max;//最小点  最大点 
	int a, b;
	int k = 1;
	while(1)
	{
		init();
		Min = 1000+20, Max = 0;
		scanf("%d", &a);
		if(a == 0) return 0;
		Min = min(a, Min);//更新 
		Max = max(a, Max); 
		scanf("%d", &b);
		Min = min(b, Min);
		Max = max(b, Max);
		addEdge(a, b); 
		while(scanf("%d", &a), a)
		{
			Min = min(a, Min);
		    Max = max(a, Max); 
		    scanf("%d", &b);
		    Min = min(b, Min);
		    Max = max(b, Max); 
		    addEdge(a, b);
		}
		find_cut(Min, Max);//从最小点 到 最大点 
		bool exist = false;
		printf("Network #%d\n", k++);
		for(int i = Min; i <= Max; i++)
		{
			if(iscut[i])
			{
				exist = true;
				printf("  SPF node %d leaves %d subnets\n", i, 1+add_bcc[i]);
			}
		}
		if(!exist)
		printf("  No SPF nodes\n");
		printf("\n");
	}
	return 0;
} 

更新于2015.8.18
AC代码:
#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 dfs_clock;//记录当前的深度优先序数
int add_bcc[MAXN];//去掉该点增加的BCC数目
int num;
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在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])
		{
			child++;
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
			if(low[v] >= dfn[u])//割点 先不考虑根节点 最后再考虑
			{
				iscut[u] = true;
				add_bcc[u]++;//增加一个BCC
			}
		}
		else if(dfn[v] < dfn[u] && v != fa)
			low[u] = min(low[u], dfn[v]);//反向边更新
	}
	//对根节点进行再次判断
	if(fa < 0 && child < 2) iscut[u] = false, add_bcc[u] = 0;//根节点不是割点
	if(fa < 0 && child > 1) iscut[u] = true, add_bcc[u] = child - 1;//根节点是割点 更新add_bcc的值
}
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(dfn, 0, sizeof(dfn));
	dfs_clock = num = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i]) tarjan(i, -1), num++;// 计算 图分成多少块
}
int main()
{
	int Min, Max;//最小点  最大点
	int a, b;
	int k = 1;
	while(1)
	{
		init();
		Min = 1000+20, Max = 0;
		scanf("%d", &a);
		if(a == 0) return 0;
		Min = min(a, Min);//更新
		Max = max(a, Max);
		scanf("%d", &b);
		Min = min(b, Min);
		Max = max(b, Max);
		addEdge(a, b);
		while(scanf("%d", &a), a)
		{
			Min = min(a, Min);
		    Max = max(a, Max);
		    scanf("%d", &b);
		    Min = min(b, Min);
		    Max = max(b, Max);
		    addEdge(a, b);
		}
		find_cut(Min, Max);//从最小点 到 最大点
		bool exist = false;
		printf("Network #%d\n", k++);
		for(int i = Min; i <= Max; i++)
		{
			if(iscut[i])
			{
				exist = true;
				printf("  SPF node %d leaves %d subnets\n", i, num + add_bcc[i]);
			}
		}
		if(!exist)
		printf("  No SPF nodes\n");
		printf("\n");
	}
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:480226次
    • 积分:19206
    • 等级:
    • 排名:第447名
    • 原创:1484篇
    • 转载:12篇
    • 译文:0篇
    • 评论:160条
    文章分类