Q - SPF POJ - 1523
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
考虑一下下图所示的两个网络。
假设在这些网络中数据只能在两个直接连接的节点中传输。
在左图网络中一个单独节点3的错误就会阻止一些其它可用节点的互相通信。
节点1和2可以保持通信,4和5也是如此,但是其他的配对就无法再保持通信了。
因此3节点就是这个网络中的SinglePoint of Failure(SPF)。
严格来讲,如果一个任意的不可用节点会阻止原有全通网络中至少一对可用节点保持通信,那么这就是一个SPF。
注意在右图的网络中并没有这种节点。因此这个网络就没有SPF。
至少有两台机器需要错误才能导致这个网络中有可用的节点对无法通信。
不停输入a,b,建立双向边,以0作为结束
找到割点,并输出,割点删除后变成了几个子图
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int vis[1005],dfn[1005],low[1005],ans[1005],exist[1005],index,e;
int head[1000005],to[1000005],nxt[1000005],cut;
void init()
{
index=e=0;
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
memset(exist,0,sizeof(exist));
memset(ans,0,sizeof(ans));
}
void add(int u,int v)
{
e++;
nxt[e]=head[u];
to[e]=v;
head[u]=e;
}
void tarjan(int u)
{
low[u]=dfn[u]=++index;
int son=0;
for(int i=head[u]; i!=-1; i=nxt[i])
{
int v=to[i];
if(!dfn[v])
{
tarjan(v);
son++;
low[u]=min(low[u],low[v]);
if((u==1&&son>1)||(u!=1&&low[v]>=dfn[u])) ans[u]=1;
}
else low[u]=min(low[u],dfn[v]);
}
}
void dfs(int u)
{
if(vis[u]) return;
vis[u]=1;
for(int i=head[u];i!=-1;i=nxt[i])
{
int v=to[i];
if(v!=cut) dfs(v);
}
}
int main()
{
int a,b,n,cnt,k=0;
while(~scanf("%d",&a)&&a!=0)
{
cnt=0;
k++;
init();
n=0,n=max(n,a);
do{
scanf("%d",&b);
if(a==b) continue;
n=max(n,a),n=max(n,b);
add(a,b);
add(b,a);
exist[a]=exist[b]=1;
}while(scanf("%d",&a)&&a!=0);
tarjan(1);
if(k>1) printf("\n");
printf("Network #%d\n",k);
for(cut=1;cut<=n;cut++)
{
if(exist[cut]&&ans[cut])
{
int sum=0;
memset(vis,0,sizeof(vis));
for(int i=1; i<=n; i++)
{
if(!vis[i]&&!ans[i]&&exist[i])
{
dfs(i),sum++;
}
}
printf(" SPF node %d leaves %d subnets\n",cut,sum);
cnt++;
}
}
if(cnt==0) printf(" No SPF nodes\n");
}
return 0;
}