poj1523

来源:互联网 发布:淘宝寄错地址了怎么办 编辑:程序博客网 时间:2024/06/03 07:57
SPF
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6931 Accepted: 3164

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 25 43 13 23 43 501 22 33 44 55 101 22 33 44 66 32 55 100

Sample Output

Network #1  SPF node 3 leaves 2 subnetsNetwork #2  No SPF nodesNetwork #3  SPF node 2 leaves 2 subnets  SPF node 3 leaves 2 subnets
题意:给定一个连通网络,网络的结点数<=1000,求出这个网络的所有割点编号,并求出若删去其中一个割点k后,对应的,原网络会被分割为多少个连通分量?
分析:求割点除了tarjan算法,还有一种O(n^2)的算法,就是分别把每个点作为根,进行dfs,看根有几个子结点,如果大于一个则为割点否则不是割点。本题就是观察每个点为根时有几个子结点,去掉该点后的连通分支数等于其子结点数。
代码:
var  a:array [1..1100,1..1010] of integer;  low,dfn,c:array [1..1000] of longint;  f,cut,b:array [1..1000] of boolean;  i,j,m,n,x,max,y,t,sum,root,rootson,d,ans:longint;  s:string;function min(x,y:longint):longint;begin  if x<y then exit(x);  exit(y);end;procedure search(x:longint);var  i:longint;begin  b[x]:=true;  for i:=1 to c[x] do  if not b[a[x,i]] then  search(a[x,i]);end;procedure check;var  stop:boolean;  i,j:longint;begin  stop:=false;  for i:=1 to max do  begin    if cut[i] then    begin      stop:=true;      fillchar(b,sizeof(b),false);      b[i]:=true;      sum:=0;      for j:=1 to max do      if not b[j] then      begin        search(j);        inc(sum);      end;      writeln('  SPF node ',i,' leaves ',sum,' subnets');    end;  end;  if not stop then  writeln('  No SPF nodes');end;procedure dfs(x,y:longint);var  i:longint;begin  inc(d);  low[y]:=d;  dfn[y]:=d;  f[y]:=true;  for i:=1 to c[y] do  begin    if not f[a[y,i]] and(a[y,i]<>x) then    begin      dfs(y,a[y,i]);      if low[a[y,i]]>=dfn[y] then cut[y]:=true;      low[y]:=min(low[y],low[a[y,i]]);    end else    low[y]:=min(low[y],dfn[a[y,i]]);  end;end;begin    t:=0;  while not eof do  begin    fillchar(a,sizeof(a),0);    fillchar(c,sizeof(c),0);    fillchar(low,sizeof(low),0);    fillchar(dfn,sizeof(dfn),0);    ans:=0;    max:=0;    root:=0;    inc(t);    while not eof do    begin      read(m);      if m=0 then      break;      readln(n);      inc(c[m]);      inc(c[n]);      a[m,c[m]]:=n;      a[n,c[n]]:=m;      if max<n then      max:=n;      if max<m then      max:=m;    end;    if max=0 then    halt;    writeln('Network #',t);    fillchar(cut,sizeof(cut),false);    fillchar(f,sizeof(f),false);    low[1]:=1;    dfn[1]:=1;    d:=1;    f[1]:=true;    for i:=1 to c[1] do    if not f[a[1,i]] then    begin      inc(root);      dfs(1,a[1,i]);    end;    if root>1 then cut[1]:=true;    check;    readln;    writeln;  end;end.
0 0
原创粉丝点击