zoukankan      html  css  js  c++  java
  • POJ 1144 Network(Tarjan求割点)

    Network
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 12707   Accepted: 5835

    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

    题目链接:POJ 1144

    给你一个无向图求割点数目,学习Tarjan求割点的一道模版题。

    无向图在Tarjan算法中有两种情况可以成为割点:

    1、当前点是DFS树根,且当前点可以DFS出去的子树(子树不是孩子)不止一棵,即u==root且son>1;

    2、当前点不是DFS树根,但是当前点产生的子树中存在节点low[v]>=dfn[u],即这个节点v除非返回u,否则是不具有反向边返回u祖先的能力的,那此时把u拿掉,v就和u的祖先隔开了,u此时就是也是割点,代码有一些部分是可以优化的,但还是喜欢写成最原始的tarjan

    代码:

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <sstream>
    #include <cstring>
    #include <bitset>
    #include <string>
    #include <deque>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define CLR(arr,val) memset(arr,val,sizeof(arr))
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    typedef pair<int,int> pii;
    typedef long long LL;
    const double PI=acos(-1.0);
    const int N=1010;
    struct edge
    {
        int to;
        int nxt;
        int id;
    };
    edge E[N*N];
    int head[N],tot;
    int dfn[N],low[N],ts,ins[N],iscut[N],st[N*N],top;
    
    void init()
    {
        CLR(head,-1);
        tot=0;
        CLR(dfn,0);
        CLR(low,0);
        CLR(ins,0);
        CLR(iscut,0);
        ts=top=0;
    }
    inline void add(int s,int t,int id)
    {
        E[tot].to=t;
        E[tot].id=id;
        E[tot].nxt=head[s];
        head[s]=tot++;
    }
    void Tarjan(int u,int id,const int &rt)
    {
        low[u]=dfn[u]=++ts;
        ins[u]=1;
        st[top++]=u;
        int i,v;
        int son=0;
        for (i=head[u]; ~i; i=E[i].nxt)
        {
            v=E[i].to;
            if(E[i].id==id)
                continue;
            if(!dfn[v])
            {
                ++son;
                Tarjan(v,E[i].id,rt);
                low[u]=min<int>(low[u],low[v]);
                if(u==rt&&son>1)
                    iscut[u]=1;
                else if(u!=rt&&low[v]>=dfn[u])
                    iscut[u]=1;
            }
            else if(ins[v])
                low[u]=min<int>(low[u],dfn[v]);
        }
        if(low[u]==dfn[u])
        {
            do
            {
                v=st[--top];
                ins[v]=0;
            }while (u!=v);
        }
    }
    int main(void)
    {
        int n,a,b,i;
        while (~scanf("%d",&n)&&n)
        {
            init();
            int id=0;
            while (scanf("%d",&a)&&a)
            {
                while (getchar()!='
    ')
                {
                    scanf("%d",&b);
                    add(a,b,id);
                    add(b,a,id);
                    ++id;
                }
            }
            for (i=1; i<=n; ++i)
                if(!dfn[i])
                    Tarjan(i,-1,i);
            int r=0;
            for (i=1; i<=n; ++i)
                if(iscut[i])
                    ++r;
            printf("%d
    ",r);
        }
        return 0;
    }
  • 相关阅读:
    css动画特效
    http标码集合
    vue的搭建项目
    多功能
    react官方脚手架搭建项目
    深入挖掘分析Go代码
    GoLang AST简介
    GoLang中的逃逸分析简介
    使用Golang实现状态机
    GoLang中的Context
  • 原文地址:https://www.cnblogs.com/Blackops/p/6000474.html
Copyright © 2011-2022 走看看