zoukankan      html  css  js  c++  java
  • UVA315 Network —— 割点

    题目链接:https://vjudge.net/problem/UVA-315

    A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.

    Input

    The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line containing just ‘0’. The last block has only one line with N = 0.

    Output

    The output contains for each block except the last in the input file one line containing the number of critical places. Sample Input 5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0 Sample Output 1 2

    题解:

    模板题,求割点的个数。

    注意:由于根节点没有父亲结点,所以在求割点的时候需要分开处理。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 1e2+10;
    19 
    20 struct Edge
    21 {
    22     int to, next;
    23 }edge[MAXN*MAXN*2];
    24 int tot, head[MAXN];
    25 
    26 int Index, DFN[MAXN], Low[MAXN];
    27 bool cut[MAXN];
    28 
    29 void addedge(int u, int v)
    30 {
    31     edge[tot].to = v;
    32     edge[tot].next = head[u];
    33     head[u] = tot++;
    34 }
    35 
    36 void Tarjan(int u, int pre)
    37 {
    38     DFN[u] = Low[u] = ++Index;
    39     int son = 0;
    40     for(int i = head[u]; i!=-1; i = edge[i].next)
    41     {
    42         int v = edge[i].to;
    43         if(v==pre) continue;
    44         if(!DFN[v])
    45         {
    46             son++;
    47             Tarjan(v, u);
    48             Low[u] = min(Low[u], Low[v]);
    49             if(u!=pre && Low[v]>=DFN[u])
    50                 cut[u] = true;
    51         }
    52         else
    53             Low[u] = min(Low[u], DFN[v]);
    54     }
    55 
    56     if(u==pre && son>1) cut[u] = true;
    57 }
    58 
    59 void init()
    60 {
    61     tot = 0;
    62     memset(head, -1, sizeof(head));
    63 
    64     Index = 0;
    65     memset(DFN, 0, sizeof(DFN));
    66     memset(Low, 0, sizeof(Low));
    67     memset(cut, false, sizeof(cut));
    68 }
    69 
    70 int main()
    71 {
    72     int n;
    73     while(scanf("%d", &n) &&n)
    74     {
    75         init();
    76         int u, v;
    77         while(scanf("%d", &u) && u)
    78         {
    79             while(getchar()!='
    ')
    80             {
    81                 scanf("%d", &v);
    82                 addedge(u, v);
    83                 addedge(v, u);
    84             }
    85         }
    86 
    87         Tarjan(1, 1);
    88         int ans = 0;
    89         for(int i = 1; i<=n; i++)
    90             if(cut[i]) ans++;
    91 
    92         printf("%d
    ", ans);
    93     }
    94 }
    View Code
  • 相关阅读:
    创建目录(单个目录和多级子目录)方法
    C++中创建目录
    C/C++中判断某一文件或目录是否存在
    Eclipse快捷键大全(转载)
    关于注册模型失败的分析
    框架Model注册失败
    nop中导航属性的写法
    CodeFirst中导航属性的代码实现 理解
    MVC下验证码
    Androidi学习笔记 1
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7679809.html
Copyright © 2011-2022 走看看