首页 > 代码库 > POJ 1523 SPF
POJ 1523 SPF
SPF
Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 152364-bit integer IO format: %lld Java class name: Main
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.
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.
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
Source
Greater New York 2000
解题:求割点。然后dfs计算可以分成多少个块。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 1010;18 vector<int>g[maxn];19 bool iscut[maxn];20 int dfn[maxn],low[maxn],cnt,vis[maxn];21 void tarjan(int u,int fa) {22 dfn[u] = low[u] = ++cnt;23 vis[u] = 1;24 int son = 0;25 for(int i = 0; i < g[u].size(); i++) {26 if(!vis[g[u][i]]) {27 tarjan(g[u][i],u);28 son++;29 low[u] = min(low[u],low[g[u][i]]);30 if(fa == -1 && son > 1 || fa != -1 && low[g[u][i]] >= dfn[u])31 iscut[u] = true;32 } else if(vis[g[u][i]] == 1) low[u] = min(low[u],dfn[g[u][i]]);33 }34 vis[u] = 2;35 }36 void dfs(int u) {37 vis[u] = 1;38 for(int i = 0; i < g[u].size(); i++)39 if(!vis[g[u][i]]) dfs(g[u][i]);40 }41 void init() {42 memset(vis,0,sizeof(vis));43 memset(iscut,false,sizeof(iscut));44 for(int i = 0; i < maxn; i++) g[i].clear();45 }46 int main() {47 int i,j,u,v,ks = 1,n,son;48 bool flag;49 while(scanf("%d",&u),u) {50 n = 0;51 init();52 scanf("%d",&v);53 n = max(max(u,v),n);54 g[u].push_back(v);55 g[v].push_back(u);56 while(scanf("%d",&u),u) {57 scanf("%d",&v);58 n = max(max(u,v),n);59 g[u].push_back(v);60 g[v].push_back(u);61 }62 flag = true;63 tarjan(1,-1);64 printf("Network #%d\n",ks++);65 for(i = 1; i <= n; i++) {66 if(iscut[i]) {67 flag = false;68 son = 0;69 memset(vis,0,sizeof(vis));70 vis[i] = 1;//记得把路锁死71 for(j = 0; j < g[i].size(); j++) {72 if(!vis[g[i][j]]) {73 son++;74 dfs(g[i][j]);75 }76 }77 printf(" SPF node %d leaves %d subnets\n",i,son);78 }79 }80 if(flag) puts(" No SPF nodes");81 puts("");82 }83 return 0;84 }
POJ 1523 SPF
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。