您的位置:首页 > 其它

poj_1523 SPF(求割顶)

2016-12-08 18:50 441 查看
SPF
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8305 Accepted: 3797
DescriptionConsider 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 ofthe 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 connectednetwork. 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.InputThe 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 thesame 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.OutputFor 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 whenthat 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
求出连通图的割顶,然后求去掉割顶后有多少个子连通图。
注意输出每一例数据时要多输出一行空行,不然PE。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;vector<int> G[maxn];int pre[maxn], iscut[maxn], low[maxn], dfs_clock;int dfs(int u, int fa) //u在DFS树中的父节点是fa{int lowu = pre[u] = ++dfs_clock;int child = 0; //子结点数目for(int i = 0; i < G[u].size(); i++){int v = G[u][i];if(!pre[v]) //没有访问过v{child++;int lowv = dfs(v, u);lowu = min(lowu, lowv); //用后代的low函数更新u的low函数if(lowv >= pre[u]){iscut[u] = true;}}else if(pre[v] < pre[u] && v != fa){lowu = min(lowu, pre[v]);  //用反向边更新u的low函数}}if(fa < 0 && child == 1) iscut[u] = 0;low[u] = lowu;return lowu;}void find_cut(int n){memset(pre, 0, sizeof(pre));memset(iscut, 0, sizeof(iscut));for(int i = 1; i <= n; i++){if(!pre[i]) dfs(i, -1);}}int current_cc;bool vis[maxn];void dfs2(int u, int x){vis[u] = 1;for(int i = 0; i < G[u].size(); i++){int v = G[u][i];if(!vis[v] && v != x) dfs2(v, x);}}//x表示去掉的割顶void find_cc(int n, int x){current_cc = 0;memset(vis, 0, sizeof(vis));for(int u = 1; u <= n; u++){if(!vis[u] && u != x){current_cc++;dfs2(u, x);}}}int main(){//FOP;int u, v, n, casen = 0;while(~scanf("%d", &u)){if(u == 0) break;else scanf("%d", &v);n = max(u, v);for(int i = 0; i <= 1000; i++) G[i].clear();G[u].push_back(v), G[v].push_back(u);while(~scanf("%d", &u)){if(u == 0) break;else scanf("%d", &v);n = max(n, u<v?v:u);G[u].push_back(v), G[v].push_back(u);}find_cut(n);casen++;printf("Network #%d\n", casen);bool flag = 0;for(int i = 1; i <= n; i++){if(iscut[i]){flag = 1;find_cc(n, i);printf("  SPF node %d leaves %d subnets\n", i, current_cc);}}if(!flag) printf("  No SPF nodes\n");printf("\n");}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: