ZOJ 1119 SPF(割点)

来源:互联网 发布:改视频的软件 编辑:程序博客网 时间:2024/06/05 17:06

SPF

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Background

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.


Example

Input


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

Source: Greater New York 2000


题意就是判别哪个是割点,并此割点把图变为几个连通分量。


代码:

#include<stdio.h>#include<memory.h>const int Node  = 1000;int n;//结点的最大编号 bool G[Node][Node];//无向图的邻接表表示 int father[Node];//DFS遍历的前驱 int dfn[Node];//DFS遍历的顺序号 int low[Node];//DFS遍历的结点可以达到的最小顺序号 bool visited[Node];//DFS遍历的标志 void DFS(int v, int num){if (visited[v]) return;low[v] = num;dfn[v] = num;visited[v] =  true;for (int i = 0; i < n; i++) {if (G[v][i]) {if (visited[i])//子结点i已经访问过 low[v] = low[v]<dfn[i]?low[v]:dfn[i];else {father[i] = v;DFS(i, num+1);low[v] = low[v]<low[i]?low[v]:low[i];}}}}void SPF(){for (int i = 0; i < n; i++) {visited[i] = false;father[i]  = -1;low[i] = 0;dfn[i] = 0;}for (int i = 0; i < n; i++) if (!visited[i]) DFS(i, 1);bool find = false;for (int i = 0; i < n; i++) {int subnet = 0;//连通分量记数 for (int j = 0; j < n; j++)    //对i的所有儿子结点 if ((father[j] == i) && (low[j] >= dfn[i])) subnet++;if (subnet > 0) {if (father[i] == -1)//i是根节点 {if (subnet > 1) {find = true;printf("  SPF node %d leaves %d subnets\n", i+1, subnet);}} else//i是内部结点 {find = true;printf("  SPF node %d leaves %d subnets\n", i+1, subnet+1);}}}if (!find) {printf("  No SPF nodes\n");}}int main(){int i,j;int number = 0; memset(G, 0, sizeof(G)); while(scanf("%d", &i)) { if(i==0) {if(number) putchar('\n'); printf ("Network #%d\n", ++number); SPF();scanf("%d", &i);if (i==0) break;memset(G, 0, sizeof(G)); }n = -1; scanf("%d", &j);//寻找最大结点 if(i>n) n = i; if(j>n) n = j; G[i-1][j-1] = G[j-1][i-1]=1; }}






原创粉丝点击