poj 1523 SPF

                        poj   1523SPF
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8920 Accepted: 4047

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

 
 

描述
考虑一下下图所示的两个网络。假设在这些网络中数据只能在两个直接连接的节点中传输。在左图网络中一个单独节点3的错误就会阻止一些其它可用节点的互相通信。节点1和2可以保持通信,4和5也是如此,但是其他的配对就无法再保持通信了。

因此3节点就是这个网络中的SinglePoint of Failure(SPF)。严格来讲,如果一个任意的不可用节点会阻止原有全通网络中至少一对可用节点保持通信,那么这就是一个SPF。注意在右图的网络中并没有这种节点。因此这个网络就没有SPF。至少有两台机器需要错误才能导致这个网络中有可用的节点对无法通信.

输入

输入会包括若干网络的描述。一个网络描述会包括若干对的整数,每行一对整数来描述节点的连接情况,先后顺序是无关的,如:1 2和2 1描述了相同的连接。所有的节点编码会从1到1000.一个单独的0行来结束连接节点的列表。一个空的网络描述结束输入。输入文档中空白的行要被忽略。

输出

对于每个输入的网络中,你需要输出存在的SPF节点的列表在文档中。

文档中第一个网络需要用“Network #1”来定义,第二个则是” Network #2”等等(如样例)。每个SPF节点,输出一行,方式如下方例子所示,列出节点的编号和当这个节点失效时完全连通的子网络的个数。如果网络中没有SPF节点,输出“NO SPF nodes“即可。

 

思路:

其实这个题挺简单的。。。(差不多就算是一个板子。。。)

然而蒟蒻表示这道题我做了很长时间。。。。

再输入的时候需要注意一下:说实话,确实很麻烦。。。。(我们需要判断两次是否为0,第一次为边是否为一组里面的,第二次为是否该结束输入。。就这样输入就行)

其实这个题在让求割点的时候还是挺简单的,就是一个tarjan求割点的板子。

但是让在求删除这个割点后有几个强连通子图的时候蒙了好长时间。。。。(ORZ)

后经大佬指点后恍然大悟:原来求这个的时候可以来判断与该割点相连的点的low值,我们在求强连通分量的时候又知道当两个点的low值相等的时候这两个点在一个强连通分量里。所以我们用判断low值个数的方法来求删掉这割点后有几个强连通子图。

代码:

 

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10000
using namespace std;
bool c,vis[N];
int n,x,y,to,tot,tim,ans,step;
int head[N],dfn[N],low[N],cut_point[N];
struct Edge
{
    int to,next,from;
}edge[N*200];
void add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
void begin()
{
    tim=0,tot=1;
    memset(head,0,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(cut_point,0,sizeof(cut_point));
}
int tarjan(int now,int pre)
{
    int sum=0; bool boo=false;
    dfn[now]=low[now]=++tim;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(i==(pre^1)) continue;
        int t=edge[i].to;
        if(!dfn[t])
        {
            sum++;
            tarjan(t,i);
            low[now]=min(low[now],low[t]);
            if(low[t]>=dfn[now]) boo=true;
        }
        else low[now]=min(low[now],low[t]);
    }
    if(!pre) {if(sum>1) cut_point[now]=1;}
    else if(boo) cut_point[now]=1;
}
int main()
{
    while(scanf("%d",&x)!=EOF&&x)
    {
        step++;n=max(max(x,y),n);
        begin();
        scanf("%d",&y); add(x,y),add(y,x);
        while(scanf("%d",&x)!=EOF&&x)
        {
            scanf("%d",&y);
            n=max(max(x,y),n);
            add(x,y),add(y,x);
        }
        printf("Network #%d\n",step);
        tarjan(1,0);c=false;
        for(int i=1;i<=n;i++)
         if(cut_point[i])  
         {
             ans=0;c=true;
             memset(vis,0,sizeof(vis));
             for(int j=head[i];j;j=edge[j].next)
             {
                 to=edge[j].to;
                 if(!vis[low[to]])
                 vis[low[to]]=true,ans++;
              } 
             printf("  SPF node %d leaves %d subnets\n",i,ans);
         }
         if(!c) printf("  No SPF nodes\n");
         printf("\n");
    }
    return  0;
}

 

 

 

 

转载于:https://www.cnblogs.com/z360/p/7067241.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值