zoukankan      html  css  js  c++  java
  • ACM TJU 1556

    Background

    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
    -----------------------------------------------------------------------------------------------------------手动分割线-------------------------------------------------------------------------------------------------------------------------
    题目大意:
      给定一个有N点的无向连通图,问图中有哪些点去掉后图讲不在联通?同时输出去掉这个点后,图被分为几个联通块。
      输入包含多组数据,每组以0结尾,每行描述图中边的两个顶点编号。每组数据中间有一个空行。
      

    思路:就是一边找割点,一边求联通分量啦......
    代码送上:
     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<string.h>
     4 using namespace std;
     5 #define N 1010
     6 int low[N],dfn[N],sub[N],n,m,cnt,e[N][N],vis[N],son;
     7 void init()
     8 {
     9     memset(vis,0,sizeof(vis));
    10     low[1]=dfn[1]=1;
    11     cnt=1;
    12     son=0;
    13     vis[1]=1;
    14     memset(sub,0,sizeof(sub));
    15 }
    16 void dfs(int u)
    17 {
    18     for(int v=1;v<=n;v++)
    19     {
    20         if(!e[u][v])continue;
    21         e[v][u]=e[u][v]=0;
    22         if(!vis[v])
    23         {
    24             vis[v]=1;
    25             cnt++;
    26             dfn[v]=low[v]=cnt;
    27             dfs(v);
    28             if(low[u]>low[v]) low[u]=low[v];
    29             if(low[v]>=dfn[u])
    30             {
    31                 if(u!=1) sub[u]++;
    32                 else son++;
    33             }
    34         }
    35         else if(low[u]>dfn[v])low[u]=dfn[v];
    36     }
    37 }
    38 int main()
    39 {
    40     int u,v;
    41     int num=1;
    42     while(scanf("%d",&u),u)
    43     {
    44         memset(e,0,sizeof(e));
    45         n=0;
    46         scanf("%d",&v);
    47         n=max(n,u);
    48         n=max(n,v);
    49         e[u][v]=1;
    50         e[v][u]=1;
    51         while(scanf("%d",&u),u)
    52         {
    53             scanf("%d",&v);
    54             n=max(n,u);
    55             n=max(n,v);
    56             e[u][v]=1;
    57             e[v][u]=1;
    58         }
    59         init();
    60         dfs(1);
    61 
    62         if(son>1)sub[1]=son-1;
    63         int flag=0;
    64         if(num>1)puts("");
    65         printf("Network #%d
    ",num++);
    66         for(int i=1;i<=n;i++)
    67         {
    68             if(sub[i])
    69             {
    70                 flag=1;
    71                 printf("  SPF node %d leaves %d subnets
    ",i,sub[i]+1);
    72             }
    73         }
    74         if(!flag)printf("  No SPF nodes
    ");
    75     }
    76     return 0;
    77 }


  • 相关阅读:
    插件开发取路径
    使用SWT模拟鼠标键盘事件
    简单RCP框架源码分析
    dom4j中使用xpath解析带命名空间的xml文件,取不到节点的解决办法
    log4j不能输出配置文件问题的解决。
    SWT中定时器的一种特殊实现方式/SWT中线程互访时display.asyncExec/display.syncExec...程序死掉无响应的解决办法
    Eclipse插件开发中对于外部Jar包和类文件引用的处理(彻底解决插件开发中的NoClassDefFoundError问题)
    zk 3.6数据绑定
    PythonExcel 模块对比
    去除数组中重复元素
  • 原文地址:https://www.cnblogs.com/xxjnoi/p/7189991.html
Copyright © 2011-2022 走看看