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

题干:

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

题目大意:

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

解题报告:

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

  再就是注意几个地方,一个是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 ;
}
全部评论

相关推荐

07-17 12:07
门头沟学院 Java
勇敢牛牛不怕困难
投递OPPO等公司7个岗位
点赞 评论 收藏
分享
06-27 12:54
已编辑
门头沟学院 Java
累了,讲讲我的大学经历吧,目前在家待业。我是一个二本院校软件工程专业。最开始选专业是觉得计算机感兴趣,所以选择了他。本人学习计算机是从大二暑假结束开始的,也就是大三开始。当时每天学习,我个人认为Java以及是我生活的一部分了,就这样持续学习了一年半,来到了大四上学期末,大概是在12月中旬,我终于找的到了一家上海中厂的实习,但我发现实习生的工作很枯燥,公司分配的活也不多,大多时间也是自己在自学。就这样我秋招末才找到实习。时间来到了3月中旬,公司说我可以转正,但是转正工资只有7000,不过很稳定,不加班,双休,因为要回学校参加答辩了,同时当时也是心高气傲,认为可以找到更好的,所以放弃了转正机会,回学校准备论文。准备论文期间就也没有投递简历。然后时间来到了5月中旬,这时春招基本也结束了,然后我开始投递简历,期间只是约到了几家下场面试。工资也只有6-7k,到现在我不知道该怎么办了。已经没有当初学习的心劲了,好累呀,但是又不知道该干什么去。在家就是打游戏,boss简历投一投。每天日重一次。26秋招都说是针对26届的人,25怎么办。我好绝望。要不要参加考公、考研、央国企这些的。有没有大佬可以帮帮我。为什么感觉别人找工作都是顺其自然的事情,我感觉自己每一步都在艰难追赶。八股文背了又忘背了又忘,我每次都花很长时间去理解他,可是现在感觉八股、项目都忘完了。真的已经没有力气再去学习了。图片是我的简历,有没有大哥可以指正一下,或者说我应该走哪条路,有点不想在找工作了。
码客明:太累了就休息一下兄弟,人生不会完蛋的
如果实习可以转正,你会不...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务