题干:

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. 

【POJ - 1523】SPF(Tarjan求割点,求分割成的连通块数,模板题,tricks)_#include

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

题目大意:

给你一个联通网路(应该是无自环无回路的),求出这个网络所有割点的编号,以及如果删除这个割点之后所对应的联通分量数。

解题报告:

  就是个板子题,,就是输出格式比较坑。。

  再就是注意几个地方,一个是if(dfn[x]==0)的时候的low[x]别忘更新,第二是son++要在if(dfn[x]==0)这个判断内。(不然的话就算下面的if(x == rt && son > 1)改成son>2也不行。)

再就是一开始写的时候把init函数放到n=max(a,b)后面了,这样肯定不对啊你都add了两条边了然后又给人家清空了,,还好样例比较良心直接就能发现,不然这个小错又得找半天错。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
struct Edge {
int u,v;
int ne;
} e[MAX];
int dfn[MAX],low[MAX],clk;
int head[MAX],tot;
int gd[MAX];
int n;
void init() {
for(int i = 1; i<=1000; i++) {
dfn[i]=low[i]=gd[i]=0;
head[i] = -1;
}
tot = 0;
clk = 0;
}
void add(int u,int v) {
e[++tot].u = u;e[tot].v = v;
e[tot].ne = head[u];head[u] = tot;
}
void tarjan(int x,int fa,int rt) {//注意这里fa和rt是不一样的含义!!
dfn[x] = low[x] = ++clk;
int son = 0;
for(int i = head[x]; ~i; i = e[i].ne) {
int v = e[i].v;
if(v == fa) continue;
if(dfn[v] == 0) {
son++;//放到dfn[v]==0这个if外面也可以??这两种都可以?? 答:不可以
tarjan(v,x,rt);
low[x] = min(low[x],low[v]);//别忘这一步啊傻不傻、、
if(x != rt && low[v] >= dfn[x]) {
gd[x]++;
}
}
else low[x] = min(low[x],dfn[v]);
}
if(x == rt && son > 1) {
gd[x] = son-1;
}
}
int main()
{
int a,b,iCase=0;

while(~scanf("%d",&a) && a) {
scanf("%d",&b);
init();
add(a,b);add(b,a);
n=max(a,b);
while(~scanf("%d",&a) && a) {
scanf("%d",&b);
add(a,b);add(b,a);
n=max(n,a);n=max(n,b);
}
tarjan(1,-1,1);
printf("Network #%d\n",++iCase);
int flag = 0;
for(int i = 1; i<=n; i++) {
if(gd[i] != 0) printf(" SPF node %d leaves %d subnets\n",i,gd[i]+1),flag = 1;
}
if(flag == 0) printf(" No SPF nodes\n");
printf("\n");
}


return 0 ;
}