您的位置:首页 > 其它

POJ 1523 SPF

2013-05-14 00:17 435 查看
[align=center]SPF[/align]

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4406 Accepted: 2025
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

Source
Greater New York 2000
  这题真的是折腾了好久,第二次提交的时候其实就对了,但我发现我写的关节点算法和书上写的有点不一样,就改成了书上那样结果再次提交却错了,于是不断的变换代码,最后我发现写的程序,并不能一此就能找出全部的关节点。很是奇怪啊。难道书上的错了?
过了好久才发现其实没有错的,我应该把一个变量初始化为0而我把他初始化为1了,这样在根节点进行特判的时候,可能无法正确进行。改过来就对了。

  这题的图是联通图,其实就是一个纯模板题
/***************************************************************
> File Name: spf.cpp
> Author: SDUT_GYX
> Mail: 2272902662@qq.com
> Created Time: 2013/5/13 19:39:59
**************************************************************/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <set>
#define LL long long
using namespace std;
vector <int >pt[1100],root,len;
set<int,less<int> > res;
int visit[1100],low[1100],cou,Sum;
int main()
{
//	freopen("data1.in","r", stdin);
void solve(int k);
void dfs(int k,int val);
int i,j,n,m,s,t,T,max;
T=1;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
max=-1;
for(i=0;i<=1000;i++)
{
pt[i].clear();
}
scanf("%d",&m);
if(n>m? n:m >max)
{
max=n>m? n:m;
}
Sum=0;
pt
.push_back(m);
pt[m].push_back(n);
while(1)
{
scanf("%d",&n);
if(n==0)
{
break;
}
scanf("%d",&m);
pt
.push_back(m);
pt[m].push_back(n);
if((n>m? n:m)>max)
{
max=n>m? n:m;
}
}
res.clear();
root.clear();
len.clear();
memset(visit,0,sizeof(visit));
for(i=1;i<=max;i++)
{
if(!visit[i]&&pt[i].size()>0)
{
Sum=0;
dfs(i,-1);
root.push_back(i);
len.push_back(Sum);
}
}
memset(visit,0,sizeof(visit));
for(i=1;i<=root.size();i++)
{
j=root[i-1];
Sum=len[i-1];
cou=0;
visit[j]=++cou;
solve(pt[j][0]);
if(cou<Sum)
{
res.insert(j);
for(int u=1;u<=pt[j].size();u++)
{
if(!visit[pt[j][u-1]])
{
solve(pt[j][u-1]);
}
}
}
}
printf("Network #%d\n",T++);
if(res.size()==0)
{
printf("  No SPF nodes\n");
printf("\n");
continue;
}
while(res.size()>0)
{
memset(visit,0,sizeof(visit));
set<int>::iterator it=res.begin();
s=0;
for(i=1;i<=max;i++)
{
if(!visit[i]&&i!=*it)
{
s+=1;
dfs(i,*it);
}
}
printf("  SPF node %d leaves %d subnets\n",*it,s);
res.erase(it);
}
printf("\n");
}
return 0;
}
void solve(int k)
{
int i,j,Min;
visit[k]=Min=++cou;
for(i=1;i<=pt[k].size();i++)
{
j=pt[k][i-1];
if(!visit[j])
{
solve(j);
Min=(Min>low[j]? low[j]:Min);
if(low[j]>=visit[k])
{
res.insert(k);
}
}else
{
Min=(Min>visit[j]? visit[j]:Min);
}
}
low[k]=Min;
}
void dfs(int k,int val)
{
int i,j;
visit[k]=1;
Sum+=1;
for(i=1;i<=pt[k].size(); i++)
{
j=pt[k][i-1];
if(!visit[j]&&j!=val)
{
dfs(j,val);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: