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

    Network
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 17016   Accepted: 7635

    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.

    Source

     
    分析:
    好惭愧啊,现在才会求割点,学习了一个新的算法,tarjan算法,但现在只会用他来求割点和割边....
    tarjan学习是参考的这位大佬的博客,写得真的简单易懂
     
    code:
    邻接矩阵实现:
    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<string.h>
    #include<set>
    #include<map>
    #include<list>
    #include<queue>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
    
    int getval()
    {
        int ret(0);
        char c;
        while((c=getchar())==' '||c=='
    '||c=='
    ');
        ret=c-'0';
        while((c=getchar())!=' '&&c!='
    '&&c!='
    ')
            ret=ret*10+c-'0';
        return ret;
    }
    
    #define max_v 1005
    int dfn[max_v];
    int low[max_v];
    int vis[max_v];
    int G[max_v][max_v];
    
    int depth,n,m,root,rt_cnt;
    
    void init()
    {
        root=1;
        depth=1;
        rt_cnt=0;
        memset(dfn,-1,sizeof(dfn));
        memset(G,0,sizeof(G));
        memset(vis,0,sizeof(vis));
        memset(low,0,sizeof(low));
    }
    
    void tarjan(int cur,int pa)
    {
        dfn[cur]=low[cur]=depth++;
        for(int i=1;i<=n;i++)
        {
            if(G[cur][i])
            {
                if(dfn[i]==-1)//i没有访问过
                {
                    tarjan(i,cur);
                    low[cur]=min(low[cur],low[i]);//逐步回溯更新访问过的父节点的low
    
                    if(cur==root)
                        rt_cnt++;//统计和根结点直接相连的点的个数,来确定根结点是不是割点
                    else if(low[i]>=dfn[cur])
                        vis[cur]=1;//标记当前cur点为割点
    
                }else if(i!=pa)//访问过,但不是父节点,更新low
                {
                    low[cur]=min(low[cur],dfn[i]);
                }
            }
        }
    }
    int main()
    {
        while(~scanf("%d",&n)&&n)
        {
            init();
            int temp;
            while(~scanf("%d",&temp)&&temp)
            {
                while(getchar()!='
    ')
                {
                    int t;
                    scanf("%d",&t);
                    G[temp][t]=G[t][temp]=1;
                }
            }
            tarjan(1,root);
            int cnt=0;
            for(int i=2;i<=n;i++)
                if(vis[i])
                cnt++;
            if(rt_cnt>1)
                cnt++;
            printf("%d
    ",cnt);
        }
        return 0;
    }
    邻接链表实现:
    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<string.h>
    #include<set>
    #include<map>
    #include<list>
    #include<queue>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
    
    int getval()
    {
        int ret(0);
        char c;
        while((c=getchar())==' '||c=='
    '||c=='
    ');
        ret=c-'0';
        while((c=getchar())!=' '&&c!='
    '&&c!='
    ')
            ret=ret*10+c-'0';
        return ret;
    }
    
    #define max_v 105
    int dfn[max_v];
    int low[max_v];
    int vis[max_v];
    vector<int> vv[max_v];
    int depth,n,m,root,rt_cnt;
    
    void init()
    {
        root=1;
        depth=1;
        rt_cnt=0;
        memset(dfn,-1,sizeof(dfn));
        memset(vis,0,sizeof(vis));
        memset(low,0,sizeof(low));
        //for(int i=1;i<=n;i++)
           // vv[i].clear();
        memset(vv,0,sizeof(vv));
    }
    
    void tarjan(int cur,int pa)
    {
        dfn[cur]=low[cur]=depth++;
        for(int i=0;i<vv[cur].size();i++)
        {
            int temp=vv[cur][i];
            if(dfn[temp]==-1)
            {
                tarjan(temp,cur);
                low[cur]=min(low[cur],low[temp]);
    
                if(cur==root)
                    rt_cnt++;
                else if(low[temp]>=dfn[cur])
                    vis[cur]=1;
    
            }else if(temp!=pa)
            {
                low[cur]=min(low[cur],dfn[temp]);
            }
        }
    }
    
    int main()
    {
        while(~scanf("%d",&n)&&n)
        {
            init();
            int temp;
            while(~scanf("%d",&temp)&&temp)
            {
                while(getchar()!='
    ')
                {
                    int t;
                    scanf("%d",&t);
                    vv[temp].push_back(t);
                    vv[t].push_back(temp);
                }
            }
            tarjan(1,root);
            int cnt=0;
            for(int i=2;i<=n;i++)
                if(vis[i])
                cnt++;
            if(rt_cnt>1)
                cnt++;
            printf("%d
    ",cnt);
        }
        return 0;
    }
  • 相关阅读:
    《游戏改变世界》笔记
    2017第6周日
    换个角度看执行力
    怎样拥有执行力和高效能
    提高个人执行力的五个关键
    Apache服务器部署多个进程
    IE的Cookie目录和临时缓存目录的关系
    IE/Firefox/Chrome等浏览器保存Cookie的位置
    在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程)
    如何设置win7系统的文件夹为系统文件,从而隐藏文件夹
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9854431.html
Copyright © 2011-2022 走看看