Time Limit: 1000MS |
| Memory Limit: 10000K |
Total Submissions: 8503 |
| Accepted: 3877 |
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 leastone pair of available nodes from being able to communicate on what waspreviously a fully connected network. Note that the network on the right has nosuch node; there is no SPF in the network. At least two machines must failbefore there are any pairs of available nodes which cannot communicate.
Input
The input will contain the description of several networks. A networkdescription will consist of pairs of integers, one pair per line, that identifyconnected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify thesame connection. All node numbers will range from 1 to 1000. A line containinga single zero ends the list of connected nodes. An empty network descriptionflags 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 numberof fully connected subnets that remain when that node fails. If the network hasno SPF nodes, simply output the text "No SPF nodes" instead of a listof 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
SPFnode 3 leaves 2 subnets
Network #2
NoSPF nodes
Network #3
SPFnode 2 leaves 2 subnets
SPFnode 3 leaves 2 subnets
Source
#include <iostream>
#include <cstdio>
#include <vector>
#include <string.h>
using namespace std;
#define CLR(arr,val) memset(arr,val,sizeof(arr))
const int N = 1010;
int low[N],dfn[N],head[N],numblock[N],vis[N];
vector<int> vv[N];
int visx,numpoint = 0,numson;
void init(){
CLR(low,0); CLR(dfn,0); CLR(head,-1); CLR(numblock,0);
CLR(vv,0); CLR(vis,0);
visx = 0; numson = 0;
}
void dfs(int x){
low[x] = dfn[x] = ++visx;
vis[x] = 1;
for(int i = 0; i < vv[x].size(); ++i){
int y = vv[x][i];
if(!vis[y]){
dfs(y);
low[x] = min(low[x],low[y]);
if(low[y] >= dfn[x] && x != 1)
numblock[x]++;
else if( x == 1 )
numson++;
}
else
low[x] = min(low[x],dfn[y]);
}
}
int main(){
//freopen("1.txt","r",stdin);
int lp1,numcase = 0,ca = 0;
while(scanf("%d",&lp1) && lp1){
init();
numpoint = max(numpoint,lp1);
int rp1;
scanf("%d",&rp1);
numpoint = max(numpoint,rp1);
vv[lp1].push_back(rp1);
vv[rp1].push_back(lp1);
int lp,rp;
while(1){
scanf("%d",&lp);
if(lp == 0)
break;
numpoint = max(numpoint,lp);
scanf("%d",&rp);
numpoint = max(numpoint,rp);
vv[lp].push_back(rp);
vv[rp].push_back(lp);
}
if(ca) printf("\n");
ca++;
dfs(1);
printf("Network #%d\n",++numcase);
bool flag = false;
if(numson > 1){ // 根节点特判
flag = true;
printf(" SPF node 1 leaves %d subnets\n",numson);
}
for(int i = 1; i <= numpoint; ++i){
if(numblock[i]){
flag = true;
printf(" SPF node %d leaves %d subnets\n",i,numblock[i]+1);
}
}
if(!flag)
printf(" No SPF nodes\n");
}
return 0;
}