• POJ1523--SPF


    SPF
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 4791   Accepted: 2213

    Description

    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

    求关节点(割点)dfs
      1 #include<cstdio>
      2 #include<cmath>
      3 #include<iostream>
      4 #include<algorithm>
      5 #include<cstring>
      6 #include<vector>
      7 #include<list>
      8 #include<map>
      9 #include<set>
     10 #include<deque>
     11 #include<cstdlib>
     12 #include<queue>
     13 #include<stack>
     14 using namespace std;
     15 #define INF 9999999
     16 #define Maxn 1001
     17 #define min(a,b) ((a)>(b)?(b):(a))
     18 #define max(a,b) ((a)>(b)?(a):(b))
     19 
     20 int vis[Maxn];
     21 int subnets[Maxn];      
     22 int head[Maxn];
     23 struct node
     24 {
     25     int v;
     26     int next;
     27 }node[Maxn * 10];
     28 int dfn[Maxn];          //深度优先数
     29 int low[Maxn];          //最低深度优先数
     30 int son;
     31 int tempdfn;
     32 int n;
     33 int index;
     34 
     35 void init()
     36 {
     37     memset(vis,0,sizeof(vis));
     38     memset(head,0xff,sizeof(head));
     39     memset(subnets,0,sizeof(subnets));
     40     son = 0; 
     41     tempdfn = 1;
     42     low[1] = 1;
     43     dfn[1] = 1;
     44     vis[1] = 1;
     45     n = -1;
     46 }
     47 
     48 void addedge(int x,int y)
     49 {
     50     node[index].v = y;
     51     node[index].next = head[x];
     52     head[x] = index++;
     53 
     54     node[index].v = x;
     55     node[index].next = head[y];
     56     head[y] = index++;
     57 }
     58 
     59 void dfs(int u)
     60 {
     61     int v;
     62     for(v = head[u]; v != -1 ; v = node[v].next )
     63     {
     64         int t = node[v].v;
     65         if(!vis[t])
     66         {
     67             vis[t] = 1;
     68             ++ tempdfn;
     69             low[t] = dfn[t] = tempdfn;
     70             dfs(t);
     71             low[u] = min(low[u],low[t]);
     72             if(low[t] >= dfn[u])
     73             {
     74                 if( u == 1)son++;
     75                 if( u != 1)subnets[u]++;
     76             }
     77         }
     78         else
     79             low[u] = min(low[u],dfn[t]);
     80     }
     81 
     82 }
     83 
     84 
     85 int main()
     86 {
     87     int s,e;
     88     int cas = 1;
     89     while(scanf("%d",&s) != EOF && s)
     90     {
     91         int i;
     92         index = 0;
     93         init();
     94         scanf("%d",&e);
     95         if( n < s) n = s;
     96         if( n < e) n = e;
     97         addedge(s,e);
     98 
     99         while(scanf("%d",&s) && s)
    100         {
    101             scanf("%d",&e);
    102             if( n < s) n = s;
    103             if( n < e) n = e;
    104             addedge(s,e);
    105         }
    106         
    107         dfs(1);
    108         if( son > 1)subnets[1] = son - 1;
    109         if(cas > 1)
    110             printf("
    ");
    111         int Find = 0;
    112         printf("Network #%d
    ",cas++);
    113         for( i = 1 ; i <= n ; i++ )
    114         {
    115             if( subnets[i])
    116             {
    117                 Find = 1;
    118                 printf("  SPF node %d leaves %d subnets
    ",i,subnets[i]+1);
    119             }
    120         }
    121         if( !Find )
    122             printf("  No SPF nodes
    ");
    123     }
    124     return 0;
    125 }
    View Code
  • 相关阅读:
    【html+table】基于jQuery,利用TableFreeze.js实现html的table冰冻效果,非常强大
    vue中使用element-ui实现excel表格导入
    正则第二次配结果与第一次不一致;正则匹配,第一次匹配结果正常,第二次匹配结果就错误,第三次又正常,第四次又错误,以此类推
    关于用vsCode格式化代码时,代码自动换行问题
    百度地图实现测量面积和测量距离功能
    sql 通过group by、sum聚合查询结果的排序
    C#小数计算的坑
    C# 反射的例子
    IE8环境下的上传图片预览
    做移动端电子签名发现canvas的 一些坑
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3237519.html
Copyright © 2020-2023  润新知