zoukankan      html  css  js  c++  java
  • POJ 1523 SPF(无向图割顶)

    SPF

     

    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

      1 #include<cstdio>
      2 #include<vector>
      3 #include<cstring>
      4 using namespace std;
      5 
      6 vector<int>G[1005];
      7 bool flag;
      8 int low[1005],dfs_clock,pre[1005];
      9 bool iscut[1005];
     10 bool vis[1005];
     11 
     12 void init()
     13 {
     14     memset(iscut,0,sizeof(iscut));
     15     memset(pre,0,sizeof(pre));
     16     memset(vis,0,sizeof(vis));
     17     for(int i=0;i<1005;i++)G[i].clear();
     18     dfs_clock=0;
     19     flag=0;
     20 }
     21 
     22 int findcut(int u,int fa)
     23 {
     24     int lowu=pre[u]=++dfs_clock;
     25     int child=0;
     26     for(int i=0;i<G[u].size();i++)
     27     {
     28         int v=G[u][i];
     29         if(!pre[v])
     30         {
     31             child++;
     32             int lowv=findcut(v,u);
     33             lowu=min(lowu,lowv);
     34             if(lowv>=pre[u])
     35             iscut[u]=true;
     36         }
     37         else if(pre[v]<pre[u]&&v!=fa)
     38         lowu=min(lowu,pre[v]);
     39     }
     40     if(fa<0&&child==1)iscut[u]=0;
     41     low[u]=lowu;
     42     return lowu;
     43 }
     44 
     45 void dfs(int u)
     46 {
     47     vis[u]=1;
     48     for(int i=0;i<G[u].size();i++)
     49     if(!vis[G[u][i]])
     50     {
     51         vis[G[u][i]]=1;
     52         dfs(G[u][i]);
     53     }
     54 }
     55 
     56 void output()
     57 {
     58     for(int i=1;i<1005;i++)
     59     {
     60         if(iscut[i])
     61         {
     62             flag=1;
     63             memset(vis,0,sizeof(vis));
     64             vis[i]=1;
     65             int son=0;
     66             for(int j=0;j<G[i].size();j++)
     67             {
     68                 if(!vis[G[i][j]])
     69                 {
     70                     dfs(G[i][j]);
     71                     son++;
     72                 }
     73             }
     74             printf("  SPF node %d leaves %d subnets
    ",i,son);
     75         }
     76     }
     77     if(!flag)
     78     printf("  No SPF nodes
    ");
     79     printf("
    ");
     80 }
     81 
     82 int main()
     83 {
     84     //freopen("in.txt","r",stdin);
     85     int u,v,t;
     86     bool key=0;
     87     t=0;
     88     while(scanf("%d",&u))
     89     {
     90         if(u==0&&key)
     91         break;
     92         else if(u==0&&!key)    
     93         {
     94             printf("Network #%d
    ",++t);
     95             findcut(1,-1);
     96             output();
     97             init();
     98             key=1;
     99             continue;
    100         }
    101         if(u!=0)key=0;else key=1;
    102         scanf("%d",&v);
    103         G[u].push_back(v);
    104         G[v].push_back(u);
    105     }
    106     return 0;
    107 }
  • 相关阅读:
    POJ_1066_Treasure Hunt_判断线段相交
    【转载】VS写汇编程序01:VS2015配置汇编语言开发环境
    【转载】汇编调试程序Debug使用
    【转载】C++ STL快速入门
    Longest Palindromic Substring
    Leetcode经典试题:Longest Substring Without Repeating Characters解析
    C++数组的初始化
    C++题目:回文数判断
    C++-int类型整数超出范围后的处理
    Memorise Me!——用数值做地址,实现快速查找
  • 原文地址:https://www.cnblogs.com/homura/p/4748996.html
Copyright © 2011-2022 走看看