zoukankan      html  css  js  c++  java
  • POJ 1144

    A - Network
    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

    Description

    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 using namespace std;
     5 const int v = 105;
     6 int edge[v][v];
     7 int bridge[v][v], cut[v];
     8 int low[v], dfn[v], vis[v];
     9 void cut_bridge(int cur, int father, int dep, int n){
    10     vis[cur] = 1;dfn[cur] = low[cur] = dep;
    11     int children = 0;
    12     for(int i = 0;i < n;i ++) if(edge[cur][i]){
    13         if(i != father && 1 == vis[i]){
    14             if(dfn[i] < low[cur]) low[cur] = dfn[i];
    15         }
    16         if(0 == vis[i]){
    17             cut_bridge(i, cur, dep+1, n);
    18             children ++;
    19             if(low[i] < low[cur]) low[cur] = low[i];
    20             if((father == -1 && children > 1) || (father != -1 && low[i] >= dfn[cur])) cut[cur] = 1;
    21         }
    22         if(low[i] > dfn[cur]) bridge[i][cur] = bridge[cur][i] = 1;
    23     }
    24     vis[cur] = 2;
    25 }
    26 
    27 int main(){
    28     int n, temp, u;
    29     /* freopen("in.c", "r", stdin); */
    30     while(~scanf("%d", &n) && n){
    31         memset(vis, 0, sizeof(vis));
    32         memset(dfn, 0, sizeof(dfn));
    33         memset(low, 0, sizeof(low));
    34         memset(cut, 0, sizeof(cut));
    35         memset(edge, 0, sizeof(edge));
    36         scanf("%d", &temp);
    37         while(temp){
    38             while(getchar() != '
    '){
    39                 scanf("%d", &u);
    40                 edge[u-1][temp-1] = edge[temp-1][u-1] = 1;
    41             }
    42             scanf("%d", &temp);
    43         }
    44         int ans = 0;
    45         cut_bridge(1, -1, 0, n);
    46         for(int i = 0;i < n;i ++ ){
    47             if(cut[i]) ans ++;
    48         }
    49         printf("%d
    ", ans);
    50     }
    51     return 0;
    52 }

    另外附上超时代码,不懂为什么会超时。。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 typedef struct
     6 {
     7     int to, next;
     8 }Node;
     9 Node edge[105];
    10 int vis[105], dfn[105], low[105], head[105], cut[105];
    11 void Tarjan(int i, int father, int dep)
    12 {
    13     low[i] = dfn[i] = dep;
    14     vis[i] = 1;
    15     int child = 0;
    16     for(int j = head[i];j != -1;j = edge[j].next)
    17     {
    18         int v = edge[j].to;
    19         if(!dfn[v])
    20         {
    21             child ++;
    22             Tarjan(v, i, dep+1);
    23             low[i] = min(low[i], low[v]);
    24             if((father == -1 && child > 1) || (father != -1 && low[v] >= dfn[i]))
    25                cut[i] = 1;
    26         }
    27         if(vis[v])
    28             low[i] = min(low[i], dfn[v]);
    29     }
    30 }
    31 
    32 int main(int argc, char const *argv[]) 
    33 {
    34     int n, temp, u;
    35     //freopen("in.c", "r", stdin);
    36     while(~scanf("%d", &n) && n)
    37     {
    38         memset(vis, 0, sizeof(vis));
    39         memset(dfn, 0, sizeof(dfn));
    40         memset(low, 0, sizeof(low));
    41         memset(head, -1, sizeof(head));
    42         memset(cut, 0, sizeof(cut));
    43         scanf("%d", &temp);
    44         int i = 1;
    45         while(temp)
    46         {
    47             while(getchar() != '
    ')
    48             {
    49                 scanf("%d", &u);
    50                 edge[i].to = u;
    51                 edge[i].next = head[temp];
    52                 head[temp] = i ++;
    53                 edge[i].to = temp;
    54                 edge[i].next = head[u];
    55                 head[u] = i ++;
    56             }
    57             scanf("%d", &temp);
    58         }
    59         int ans = 0;
    60         Tarjan(1, -1, 1);
    61         for(int i = 1;i <= n;i ++)
    62             if(cut[i])
    63                 ans ++;
    64         printf("%d
    ", ans);
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    php字符串相加
    elementUI的input输入一个字符就失去焦点问题
    js鸡尾酒排序算法
    js快速排序算法
    js冒泡排序算法改进
    js实现队列
    EXIF.js 读取图片的方向
    new Image().src资源重复请求问题
    canvas绘制圆图输出图片格式
    去掉"You are running Vue in development mode"提示
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3603588.html
Copyright © 2011-2022 走看看