zoukankan      html  css  js  c++  java
  • poj 1523 割点 tarjan

    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


    题意: 求割点 并输出 (当删除割点时) 将原图分为几个连通图;

    题解: tarjan 模板

    low[u] 是从u或u的子孙出发通过回边能够到达的最低深度优先数

    dfn[u] 时间戳

    u是关节点的 条件

    1. 若u为根节点 u至少有两个子女

    2. 若u不是根节点  他又一个子女w low[w]>=dfn[u]

     

      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdio> 
      4 #define ll __int64
      5 #define mod 1 
      6 #define PI acos(-1.0)
      7 using namespace std;
      8 int Edge[1001][1001];
      9 int visited[1001];
     10 int nodes;
     11 int tmpdfn;
     12 int dfn[1001];
     13 int low[1001];
     14 int son;
     15 int subnets[1001];
     16 void dfs(int u)
     17 {
     18     for(int v=1;v<=nodes;v++)
     19     {
     20         if(Edge[u][v])
     21         {
     22             if(!visited[v])
     23             {
     24                 visited[v]=1;
     25                 tmpdfn++ ;
     26                 dfn[v]=low[v]=tmpdfn;
     27                 dfs(v);
     28                 low[u]=min(low[u],low[v]);
     29                 if(low[v]>=dfn[u])
     30                 {
     31                     if(u!=1)
     32                     subnets[u]++;
     33                     if(u==1)
     34                     son++;
     35                 }
     36                 
     37             }
     38             else
     39             low[u]=min(low[u],dfn[v]);
     40         }
     41     }
     42 }
     43 void init()
     44 {
     45     low[1]=dfn[1]=1;
     46     tmpdfn=1;
     47     son=0;
     48     memset(visited,0,sizeof(visited));
     49     visited[1]=1;
     50     memset(subnets,0,sizeof(subnets));
     51 }
     52 int main()
     53 {
     54     int i;
     55     int u,v;
     56     int find;
     57     int number=1;
     58     while(1)
     59     {
     60         scanf("%d",&u);
     61         if(u==0)
     62         break;
     63         memset(Edge,0,sizeof(Edge));
     64         nodes=0;
     65         scanf("%d",&v);
     66         if(u>nodes) 
     67         nodes=u;
     68         if(v>nodes)
     69         nodes=v;
     70         Edge[u][v]=1;
     71         Edge[v][u]=1;
     72         while(1)
     73         {
     74             scanf("%d",&u);
     75         if(u==0)
     76         break;
     77         scanf("%d",&v);
     78         if(u>nodes) 
     79         nodes=u;
     80         if(v>nodes)
     81         nodes=v;
     82         Edge[u][v]=1;
     83         Edge[v][u]=1;
     84         }
     85         if(number>1)
     86         printf("
    ");
     87         printf("Network #%d
    ",number);
     88         number++;
     89         init();
     90         dfs(1);
     91         if(son>1)
     92         subnets[1]=son-1;
     93         find=0;
     94         for(i=1;i<=nodes;i++)
     95         {
     96             if(subnets[i])
     97             {
     98                 find=1;
     99                 printf("  SPF node %d leaves %d subnets
    ",i,subnets[i]+1);
    100             }
    101         }
    102         if(!find)
    103         printf("  No SPF nodes
    ");
    104         
    105     }
    106     return 0;
    107 }
    108 /*割点 图论书模板*/
  • 相关阅读:
    C# asp:Repeater DataSource List<T>
    MySQL DATE_FORMATE函数内置字符集的坑_转小叶子爹
    MySQL count(distinct) 逻辑的一个bug
    org.hibernate.PersistentObjectException: detached entity passed to persist:
    CGLIB Enhancement failed
    firstResult/maxResults specified on polymorphic query;
    Last packet sent to the server was 0 ms ago.
    MySql Error Code: 2006 – MySQl
    InnoDB: Error: auto-extending data file ./ibdata1 is of a different size
    mysql 大数据量分页处理
  • 原文地址:https://www.cnblogs.com/hsd-/p/5370077.html
Copyright © 2011-2022 走看看