zoukankan      html  css  js  c++  java
  • POJ 1144 无向图的割点

    Network
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 10207   Accepted: 4744

    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

    Hint

    You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.

    Source

     1 //无向图的割点
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cstdlib>
     6 #include<iostream>
     7 #include<queue>
     8 #include<vector>
     9 #include<stack>
    10 using namespace std;
    11 const int maxn=107;
    12 int n;
    13 vector<int> G[maxn];
    14 int ans,low[maxn],pre[maxn],sum,root;
    15 bool iscut[maxn];
    16 void dfs(int u,int fa)
    17 {
    18     int lowu;
    19     low[u]=pre[u]=sum++;
    20     int l=G[u].size();
    21     int child=0;
    22     for(int i=0;i<l;i++)
    23     {
    24         int v=G[u][i];
    25         if(!pre[v]){
    26             child++;
    27             dfs(v,u);
    28             low[u]=min(low[u],low[v]);
    29             if(u==root&&child==2) iscut[u]=1;
    30             if(low[v]>=pre[u]&&u!=root){
    31                 iscut[u]=1;
    32             }
    33         }
    34         else if(u!=v)
    35         {
    36             low[u]=min(low[u],pre[v]);
    37         }
    38     }
    39 }
    40 int main()
    41 {
    42     char c;int m,a,b;
    43     while(1){
    44         scanf("%d",&n);
    45         if(n==0) break;
    46         for(int i=0;i<maxn;i++) G[i].clear();
    47         while(1){
    48             scanf("%d",&m);
    49             if(m==0) break;
    50              while((c=getchar())!='
    ')
    51             {
    52                 scanf("%d",&b);
    53                 G[m].push_back(b);
    54                 G[b].push_back(m);
    55             }
    56         }
    57         memset(pre,0,sizeof(pre));
    58         memset(iscut,0,sizeof(iscut));
    59         memset(low,0,sizeof(low));
    60         ans=0,sum=1,root=1;
    61         if(n==1||n==2){printf("0
    ");continue;}
    62         dfs(1,-1);
    63         for(int i=0;i<=n;i++) if(iscut[i]) ans++;
    64         printf("%d
    ",ans);
    65     }
    66 }
  • 相关阅读:
    将单向链表按某值划分为左边小、中间相等、右边大的形式
    数组中的数字按某值划分为左边小、中间相等、右边大的形式
    Kendo UI for jQuery管理数据有妙招!轻松将数据存储为JSON
    DevExpress Xamarin.Forms v21.1
    界面控件Telerik UI for WinForm初级教程
    WPF应用程序的主题颜色如何修改?DevExpress调色板工具很好用
    DevExpress WinForm模板库可快速创建Windows样式的应用程序界面
    Kendo UI for jQuery数据管理使用教程:Spreadsheet
    开发框架DevExtreme入门级教程
    跨平台.NET应用程序界面开发新亮点
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4381714.html
Copyright © 2011-2022 走看看