zoukankan      html  css  js  c++  java
  • UVA 315 Network(无向图求割点)

    Description:

    Download as PDF
     

    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

    题意:电话线都是连通的,假如有些接点发生了故障,可能整个系统还是互通的,也可能致使有些地方不能互通,现在需要判断一个电话系统中有几个不能出故障的接点(即连通图求割点)

      求割点:
      一个顶点u是割点,当且仅当满足(1)或(2)
     (1) u为树根,且u有多于一个子树。
     (2) u不为树根,且满足存在(u,v)为树枝边(或称 父子边,即u为v在搜索树中的父亲),使得 dfn(u)<=low(v)。
     (也就是说 V 没办法绕过 u 点到达比 dfn[u] 要小的点)
      注:这里所说的树是指,DFS下的搜索树

    #include<stdio.h>
    #include<algorithm>
    #include<vector>
    #include<string.h>
    #define N 110
    using namespace std;
    int dfn[N], low[N];
    int f[N], vis[N]; //f数组表示父节点,vis数组标记该点是否为割点
    int n, Time;
    vector<vector<int> >G;
    void Init()
    {
        G.clear();
        G.resize(n+1);
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(f, 0, sizeof(f)); //将父节点都初始化为0
        memset(vis, 0, sizeof(vis));
        Time = 0;
    }
    void Tarjan(int u, int fa)
    {
        int i, len, v;
        dfn[u] = low[u] = ++Time;
        f[u] = fa;
        len = G[u].size();
        for (i = 0; i < len; i++)
        {
            v = G[u][i];
            if (!dfn[v])
            {
                Tarjan(v, u);
                low[u] = min(low[u], low[v]);
            }
            else if (v != fa) low[u] = min(low[u], dfn[v]); //这里注意不能写成low[v],如果写成low[v],意味着可以越过该点到比该点更早的点,实际上不一定
        }
    }
    int main ()
    {
        int a, b, son, ans, i, ni;
        char ch;
        while (scanf("%d", &n), n)
        {
            Init();
            son = ans = 0;
            while (scanf("%d", &a), a)
            {
                while (scanf("%d%c", &b, &ch) != EOF)
                {
                    G[a].push_back(b);
                    G[b].push_back(a);
                    if (ch == '
    ') break;
                }
            }
            Tarjan(1, 0);
            for (i = 2; i <= n; i++)
            {
                ni = f[i]; 
                if (ni == 1) son++;
                else if (dfn[ni] <= low[i]) vis[ni] = 1;
            }
            for (i = 2; i <= n; i++)
                if (vis[i]) ans++;
            if (son > 1) ans++; //如果son>1,则代表根节点有超过1个的子节点,根节点是割点
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    kvm克隆虚拟机
    vSAN添加license
    KVM虚拟机快照
    第二次作业及总结——数据类型和运算符
    第二次作业心得
    做完c语言作业的心得
    介绍自己
    awk数组 Jazz
    Java入门第一阶段总结
    区间dp入门+例题
  • 原文地址:https://www.cnblogs.com/syhandll/p/4705056.html
Copyright © 2011-2022 走看看