zoukankan      html  css  js  c++  java
  • POJ 1144 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.

    题目大意:给一个n个点的无向连通图,求删掉一个点后,可以使图不连通的点数。

    思路:裸的求割点题。

    代码(16MS):

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <cmath>
     6 using namespace std;
     7 
     8 const int MAXN = 110;
     9 const int MAXE = MAXN * MAXN;
    10 
    11 int head[MAXN], ecnt;
    12 int to[MAXE], next[MAXE];
    13 int pre[MAXN], lowlink[MAXN], dfs_clock;
    14 bool iscut[MAXN];
    15 int n, m, ans;
    16 
    17 void init() {
    18     memset(head, 0, sizeof(head));
    19     memset(pre, 0, sizeof(pre));
    20     memset(iscut, 0, sizeof(iscut));
    21     ecnt = 1;
    22     ans = dfs_clock = 0;
    23 }
    24 
    25 void add_edge(int u, int v) {
    26     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
    27     to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
    28     //printf("%d--%d
    ", u, v);
    29 }
    30 
    31 void dfs(int fa, int u) {
    32     pre[u] = lowlink[u] = ++dfs_clock;
    33     int child = 0;
    34     for(int p = head[u]; p; p = next[p]) {
    35         int &v = to[p];
    36         if(v == fa) continue;
    37         if(!pre[v]) {
    38             ++child;
    39             dfs(u, v);
    40             lowlink[u] = min(lowlink[u], lowlink[v]);
    41             if(pre[u] <= lowlink[v]) iscut[u] = true;
    42         } else lowlink[u] = min(lowlink[u], pre[v]);
    43     }
    44     if(fa < 0 && child == 1) iscut[u] = false;
    45 }
    46 
    47 char s[5 * MAXN], *p, *prev;
    48 
    49 int get_int(char *&src) {
    50     while(!isdigit(*src) && *src) ++src;
    51     int ret = 0;
    52     while(isdigit(*src)) ret = ret * 10 + *src++ - '0';
    53     return ret;
    54 }
    55 
    56 int main() {
    57     while(scanf("%d", &n) != EOF && n) {
    58         init();
    59         int u, v;
    60         while(gets(s) && *s != '0') {
    61             p = s;
    62             u = get_int(p);
    63             while((v = get_int(p)) > 0) add_edge(u, v);
    64         }
    65         dfs(-1, 1);
    66         for(int i = 1; i <= n; ++i) ans += iscut[i];
    67         printf("%d
    ", ans);
    68     }
    69 }
    View Code
  • 相关阅读:
    MongoDB简单使用
    证明Whiile比for的效率高
    Python的垃圾回收机制
    Google C++编程风格指南
    AVR GCC对端口的操作指南
    Android Audio 分析
    nesC 语言参考手册
    浅析Linux操作系统工作的基础
    【python】bytearray和string之间转换,用在需要处理二进制文件和数据流上
    Matlab 仿真实现TI Instaspin 的Foc 逆Clarke变换和SVPWM
  • 原文地址:https://www.cnblogs.com/oyking/p/3303276.html
Copyright © 2011-2022 走看看