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

﻿﻿
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

tarjan算法：

1，该点本身的深度优先数
2，它的子女中最低深度优先数
3，该点通过回边可以到达的最低优先数

#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];
bool iscut[MAXN];//判断是否为割点
int low[MAXN];//从当前节点或它的子孙出发通过回边可以到达的最低深度优先数
int dfn[MAXN];//记录该点在DFS树中的深度优先数
int recdfn;//记录当前的深度优先序数
void init()
{
top = 0;
}
{
Edge E = {u, v, head[u]};
edge[top] = E;
Edge E1 = {v, u, head[v]};
edge[top] = E1;
}
void getMap(int m)
{
int a, b;
while(m--)
{
scanf("%d%d", &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 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);
while(scanf("%d", &a), a)
{
Min = min(a, Min);
Max = max(a, Max);
scanf("%d", &b);
Min = min(b, Min);
Max = max(b, Max);
}
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;
}

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];
bool iscut[MAXN];//判断是否为割点
int low[MAXN];//从当前节点或它的子孙出发通过回边可以到达的最低深度优先数
int dfn[MAXN];//记录该点在DFS树中的深度优先数
int dfs_clock;//记录当前的深度优先序数
int num;
void init()
{
top = 0;
}
{
Edge E = {u, v, head[u]};
edge[top] = E;
Edge E1 = {v, u, head[v]};
edge[top] = E1;
}
void getMap(int m)
{
int a, b;
while(m--)
{
scanf("%d%d", &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;
}
}
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(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);
while(scanf("%d", &a), a)
{
Min = min(a, Min);
Max = max(a, Max);
scanf("%d", &b);
Min = min(b, Min);
Max = max(b, Max);
}
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;
}

• 本文已收录于以下专栏：

## POJ 1523--SPF【无向图的所有割点 && 删去该点后bcc的数目】

SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7066   Accepted: 3218 Des...
• hpuhjh
• 2015年08月12日 17:05
• 646

## POJ 1144 Network 无向图求割点

• wmn_wmn
• 2012年08月25日 10:59
• 3433

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

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

## POJ 1523 SPF 无向图求割点和块

• wmn_wmn
• 2012年08月21日 20:59
• 1600

## 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
• 2012年08月31日 15:13
• 934

## 求无向图的割点 （poj 1144 Network）

举报原因： 您举报文章：poj 1532 【求无向图的所有割点 以及 该点分成的BCC数目】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)