zoukankan      html  css  js  c++  java
  • 18.11.16 POJ 1522 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. 

    输入

    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.输出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.

    样例输入

    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

    样例输出

    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

    来源

    Greater New York 2000

      1 #include <iostream>
      2 #include <string.h>
      3 #include <algorithm>
      4 #include <stack>
      5 #include <string>
      6 #include <math.h>
      7 #include <queue>
      8 #include <stdio.h>
      9 #include <string.h>
     10 #include <vector>
     11 #include <fstream>
     12 #include <set>
     13 #define  inf 999999;
     14 #define maxn 1001
     15 
     16 using namespace std;
     17 int cc;
     18 int dfn[maxn], low[maxn],parent[maxn];
     19 vector<vector<int>>all(maxn);
     20 set<int>gedian;
     21 
     22 void tarjan(int u,int father) {
     23     parent[u] = father;
     24     int size = all[u].size();
     25     dfn[u] =low[u]= ++cc;
     26     for (int i = 0; i < size; i++) {
     27         int v = all[u][i];
     28         if (!dfn[v]) {
     29             tarjan(v, u);
     30             low[u] = min(low[u], low[v]);
     31         }
     32         else if (father != v)
     33             low[u] = min(low[u], dfn[v]);
     34     }
     35 }
     36 
     37 int n;
     38 
     39 void getge() {
     40     tarjan(1, 0);
     41     int rootnum = 0;
     42     for (int i = 2; i <= n; i++) {
     43         int v = parent[i];
     44         if (v == 1)
     45             rootnum++;
     46         else if (v&&dfn[v] <= low[i])
     47             gedian.insert(v);
     48     }
     49     if (rootnum > 1)
     50         gedian.insert(1);
     51 }
     52 
     53 bool visited[maxn];
     54 
     55 void dfs(int x) {
     56     int size = all[x].size();
     57     for (int i = 0; i < size;i++) {
     58         int v = all[x][i];
     59         if (visited[v] == false) {
     60             visited[v] = true;
     61             dfs(v);
     62         }
     63     }
     64 }
     65 
     66 void init() {
     67     int p1, p2;
     68     int kase=0;
     69     while (1) {
     70         kase++;
     71         n = 0;
     72         cc = 0;
     73         gedian.clear();
     74         all = vector<vector<int>>(maxn);
     75         memset(dfn, 0, sizeof(dfn)), memset(low, 0, sizeof(low));
     76         memset(parent, 0, sizeof(parent));
     77         scanf("%d", &p1);
     78         if (!p1)break;
     79         scanf("%d", &p2);
     80         while (1) {
     81             all[p1].push_back(p2);
     82             all[p2].push_back(p1);
     83             scanf("%d", &p1);
     84             if (!p1)break;
     85             scanf("%d", &p2);
     86             n = max(n, max(p1, p2));
     87         }
     88         getge();
     89         bool flag =!gedian.empty();
     90         printf("Network #%d
    ", kase);
     91         if (!flag)
     92             printf("  No SPF nodes
    ");
     93         while (!gedian.empty()) {
     94             int now = *(gedian.begin());
     95             int count = 0;
     96             memset(visited, 0, sizeof(visited));
     97             visited[now] = true;
     98             for (int i = 1; i <= n; i++) {
     99                 if (visited[i] == false)
    100                 {
    101                     visited[i] = true;
    102                     count++;
    103                     dfs(i);
    104                 }
    105             }
    106             printf("  SPF node %d leaves %d subnets
    ", now, count);
    107             gedian.erase(gedian.begin());
    108         }
    109         printf("
    ");
    110     }
    111 }
    112 
    113 int main()
    114 {
    115     init();
    116     return 0;
    117 }
    View Code

    wa点:

    1)没有初始化数据

    2)听信群友言论认为不用考虑不连通图的情况(落泪)

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    css3新特性总结
    H5新特性总结
    小程序本地移除有一条数据
    字符串截取(某个指定字符前面和后面的值)(指定前几位后几位)
    uni-app 创建项目
    数组转数组对象及数组对象中的某个属性值拼成一个数组
    VUE 解决单页使用keep-alive页面返回不刷新的问题
    小程序弹窗真机不动
    js 数组去重方法
    VUE 列表页中实现分页(下拉到底部触发下一页 )
  • 原文地址:https://www.cnblogs.com/yalphait/p/9970939.html
Copyright © 2011-2022 走看看