zoukankan      html  css  js  c++  java
  • Network (tarjan割点)

    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

    给你一个图

    问你图中割点有几个

    就是裸的求割点题目

    直接类似tarjan的算法 求就可以了

    注意的一点就是 在比较只是跟dfn比较

    而不是跟low比较

    只有在更新时才把该点low更新所有子树中的最低

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    using namespace std;
    int nxt[20005];
    int to[20005];
    int first[205];
    int dfn[205];
    int low[205];
    int vis[205];
    int point[205];
    int pos;
    int ans=0;
    void tarjan(int x,int last)//深度优先树形图
    {
        int num=0;//记录根节点的子节点
        if(!dfn[x]) dfn[x]=low[x]=++pos;
        //cout<<x<<" "<<pos<<endl;
        for(int i=first[x];i!=-1;i=nxt[i])
        {
            num++;
            int tmp=to[i];
            if(tmp==last) continue;
            if(!vis[tmp])//未访问过
            {
                vis[tmp]=1;
                tarjan(tmp,x);
                if(x==1&&num>1&&point[x]==0)
                {
                    point[x]=1;
                    ans++;
                }
                else if(x!=1&&low[tmp]>=dfn[x]&&point[x]==0)
                {
                    ans++;
                    point[x]=1;
                }
                //cout<<x<<" "<<tmp<<" "<<low[x]<<" "<<low[tmp]<<endl;
                low[x]=(low[tmp],low[x]);
            }
            else//访问过
            {
                //cout<<"*"<<endl;
                low[x]=min(low[x],dfn[tmp]);
            }
        }
        return ;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n;
        while(~scanf("%d",&n))
        {
            if(n==0) return 0;
            memset(point,0,sizeof(point));
            memset(dfn,0,sizeof(dfn));
            memset(low,0,sizeof(low));
            memset(first,-1,sizeof(first));
            memset(nxt,-1,sizeof(nxt));
            memset(vis,0,sizeof(vis));
            memset(to,0,sizeof(to));
            int temp1,temp2;
            int cnt=0;
            while(~scanf("%d",&temp1))
            {
                //cout<<temp1<<endl;
                if(temp1==0) break;
                while(getchar()!='
    ')
                {
                    //cout<<temp1<<endl;
                    scanf("%d",&temp2);
                    cnt++;
                    nxt[cnt]=first[temp1];
                    to[cnt]=temp2;
                    first[temp1]=cnt;
                    cnt++;
                    nxt[cnt]=first[temp2];
                    to[cnt]=temp1;
                    first[temp2]=cnt;
                }
            }
            ans=0;
            pos=0;
            vis[1]=1;
            tarjan(1,0);
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    Git中使用.gitignore忽略文件的推送
    git stash详解
    Git撤销&回滚操作(git reset 和 get revert)
    git rebase和git merge的区别
    git撤销已经push到远程仓库上的代码
    Git Merge
    git cherry-pick 教程
    Failed to start LSB: Bring up/down错误解决方法
    linux centos7安装部署gitlab服务器
    CentOs7 HP找回root密码
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852285.html
Copyright © 2011-2022 走看看