SPF
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
Source
题目类型:割点+求分割后连通分量个数
算法分析:使用Tarjan算法求解即可,注意数据之间需要有额外的空行,最后一组数据之后没有
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits>() #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define i64 long long const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1000 + 66; int edge[maxn][maxn], vis[maxn], dfsn[maxn], low[maxn], subnets[maxn]; int n, id; void Init (int rt) { for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) { if (i == j) edge[i][j] = 0; else edge[i][j] = INF; } dfsn[rt] = low[rt] = id = 1; memset (vis, 0, sizeof (vis)); vis[rt] = 1; memset (subnets, 0, sizeof (subnets)); } void dfs (int u) { for (int v = 1; v <= n; v++) { if (edge[u][v] < INF && u != v && !vis[v]) { vis[v] = 1; dfsn[v] = low[v] = ++id; dfs (v); low[u] = min (low[u], low[v]); if (low[v] >= dfsn[u]) subnets[u]++; } else if (edge[u][v] < INF && u != v && vis[v]) low[u] = min (low[u], dfsn[v]); } } int main() { // CPPFF; int flag = 1, u, v; while (cin >> u) { if (u == 0) break; Init(1); cin >> v; n = -INF; n = max (n, u); n = max (n, v); edge[u][v] = edge[v][u] = 1; while (cin >> u) { if (u == 0) break; cin >> v; n = max (n, u); n = max (n, v); edge[u][v] = edge[v][u] = 1; } if (flag > 1) cout << endl; cout << "Network #" << flag++ << endl; dfs (1); bool is_find = false; for (int i = 1; i <= n; i++) { if (subnets[i]) { if (i == 1 && subnets[i] > 1) { cout << " SPF node " << i << " leaves " << subnets[i] << " subnets" << endl; is_find = true; } else if (i != 1) { cout << " SPF node " << i << " leaves " << subnets[i] + 1 << " subnets" << endl; is_find = true; } } } if (!is_find) cout << " No SPF nodes" << endl; } return 0; } |
- « 上一篇:poj1511
- poj1528:下一篇 »