zoukankan      html  css  js  c++  java
  • 图论(无向图的割顶):POJ 1144 Network

    Network
     

    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.
     
      这道题就是求无向图的割顶,一般用tarjan算法。
      刘汝佳老师的书上有写,还注明要注意一些地方,并称之为"写错",可我觉得有些地方没注意只是不严谨,并不影响正确性……
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int maxn=110;
     6 const int maxm=10010;
     7 int low[maxn],vis[maxn],tim;
     8 int cnt,fir[maxn],to[maxm<<1],nxt[maxm<<1];
     9 bool iscut[maxn];
    10 void addedge(int a,int b){
    11     nxt[++cnt]=fir[a];fir[a]=cnt;to[cnt]=b;
    12 }
    13 void DFS(int node,int fa){
    14     low[node]=vis[node]=++tim;
    15     int child=0;
    16     for(int i=fir[node];i;i=nxt[i])
    17         if(vis[to[i]])
    18             low[node]=min(low[node],vis[to[i]]);
    19         else{
    20             child++;
    21             DFS(to[i],node);
    22             low[node]=min(low[node],low[to[i]]);
    23             if(low[to[i]]>=vis[node])
    24                 iscut[node]=true;
    25         }
    26     if(fa==-1&&child<=1)
    27         iscut[node]=false;    
    28 }
    29 int main(){
    30     int a,b,n;
    31     while(~scanf("%d",&n)&&n){
    32         tim=0;cnt=0;
    33         memset(vis,0,sizeof(vis));
    34         memset(fir,0,sizeof(fir));
    35         memset(iscut,0,sizeof(iscut));
    36         while(~scanf("%d",&a)&&a){
    37             char c;
    38             while((c=getchar())!='
    '){
    39                 scanf("%d",&b);
    40                 addedge(a,b);
    41                 addedge(b,a);
    42             }
    43         }
    44         DFS(1,-1);
    45         int ans=0;
    46         for(int i=1;i<=n;i++)
    47             if(iscut[i])
    48                 ans++;
    49         printf("%d
    ",ans);        
    50     }
    51     return 0;
    52 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    445port入侵具体解释
    重构摘要12_大型重构
    Matlab画图-非常具体,非常全面
    期望DP
    自己实现一个SQL解析引擎
    信息熵(Entropy)究竟是用来衡量什么的?
    速算123,公布
    OCP-1Z0-051-题目解析-第28题
    选择排序
    Android入门第八篇之GridView(九宫图)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5353059.html
Copyright © 2011-2022 走看看