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.
题意是说给你一个联通网路,求出这个网络所有割点的编号,以及如果删除这个割点之后所对应的联通分量数。
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.
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.
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
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
首先要明白什么是割点,什么是连通分量。离散数学的知识。
1、【割点】在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合。当割点集合的顶点个数只有1个时,该顶点就是割点。
2、【连通分量】当删除某个割点后,原图会被划分为若干个互不连通的子图,这些子图就是该割点对应的连通分量。
#include
#include
#include
using namespace std;
const int maxn=1007;
int head[maxn];
int low[maxn],dfn[maxn],visit[maxn];
int cut[maxn];
int n,cnt,num,count;
struct node{
int to,next;
}edge[maxn*6];
void init()
{
memset(dfn,0,sizeof(dfn));
memset(head,-1,sizeof(head));
memset(visit,0,sizeof(visit));
memset(low,0,sizeof(low));
memset(cut,0,sizeof(cut));
cnt=0;
num=0;
}
void add(int u,int v)
{
edge[count].to=v;
edge[count].next=head[u];
head[u]=count++;
edge[count].to=u;
edge[count].next=head[v];
head[v]=count++;
}
void tarjan(int what,int father)
{
int child=0;
visit[what]=1;
low[what]=dfn[what]=++cnt;
for(int i=head[what];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(visit[v]==1)
{
low[what]=min(low[what],dfn[v]);
}
if(visit[v]==0)
{
tarjan(v,what);
child++;
low[what]=min(low[what],low[v]);
if((father==-1&&child>1)||(father!=-1&&low[v]>=dfn[what]))
cut[what]++;
}
}
visit[what]=2;
}
int main()
{
init();
int u,v;
int cas=0,flag=0;
while(~scanf("%d",&u))
{
if(u==0&&flag==1)
{
cas++;
for(int i=1;i<=1000;i++)
{
if(visit[i]==0)
{
cnt=0;
tarjan(i,-1);
}
}
printf("Network #%d\n",cas);
int f=0;
for(int i=1;i<=1000;i++)
{
if(cut[i])
{
f=1;
printf(" SPF node %d leaves %d subnets\n",i,cut[i]+1);
}
}
if(f==0) printf(" No SPF nodes\n");
printf("\n");
init();
flag=0;
}
else
{
flag=1;
scanf("%d",&v);
add(u,v);
}
}
return 0;
}