码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1523 SPF

时间:2014-08-27 16:10:17      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   java   io   for   

SPF

Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1523
64-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. 
bubuko.com,布布扣
 

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

 
解题:求割点。然后dfs计算可以分成多少个块。
 
bubuko.com,布布扣
 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 long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 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 }
View Code

 

POJ 1523 SPF

标签:des   style   blog   http   color   os   java   io   for   

原文地址:http://www.cnblogs.com/crackpotisback/p/3939372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!