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.

    Sample Input

    5
    5 1 2 3 4
    0
    6
    2 1 3
    5 4 6 2
    0
    0

    Sample Output

    1
    2
    • 代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define INF 99999999
    using namespace std;
    const int maxn=1005;
    int flag[maxn];
    int num[maxn],low[maxn];
    int e[105][105];
    int cnt,index;
    int root;
    int n;
    void dfs(int cur,int father){
        int child=0;
        index++;
        num[cur]=index;
        low[cur]=index;
        for(int i=1;i<=n;i++){
            if(e[cur][i]){
                if(!num[i]){
                    child++;
                    dfs(i,cur);
                    low[cur]=min(low[cur],low[i]);
                    if(cur!=root&&low[i]>=num[cur]){
                        flag[cur]=1;
                    }
                    if(cur==root&&child>=2){
                        flag[cur]=1;
                    }
                }else if(i!=father){
                    low[cur]=min(low[cur],num[i]);
                }
            }
        }
    }
    int main(){
        while(scanf("%d",&n)&&n){
            cnt=0;
            index=0;
            int u,v;
            memset(flag,0,sizeof flag);
            memset(num,0,sizeof num);
            memset(low,0,sizeof low);
            memset(e,0,sizeof e);
            while(scanf("%d",&u)&&u){
                while(getchar()!='
    '){
                    scanf("%d",&v);
                    e[u][v]=e[v][u]=1;
                }
            }
            root=1;
            dfs(1,root);
            for(int i=1;i<=n;i++){
                if(flag[i]) cnt++;
                    //printf("%d ",i);
            }
            printf("%d
    ",cnt);
        }
        return 0;
    }

    图的割点模板:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    using namespace std;
    const int maxn=1005;
    int e[maxn][maxn];
    int flag[maxn],num[maxn],low[maxn];
    int n,m,index;
    int root;
    void dfs(int cur,int father){//当前顶点编号和父节点编号
        int child=0;
        index++;//时间戳加1
        num[cur]=index;//当前顶点的时间戳
        low[cur]=index;//当前顶点cur能够访问到最早顶点的时间戳
        for(int i=1;i<=n;i++){
            if(e[cur][i]){
                if(!num[i]){//时间戳为,说明当前顶点还没有被访问过
                    child++;
                    dfs(i,cur);
                    low[cur]=min(low[cur],low[i]);//更新当前顶点能够访问到的最小时间戳
                    if(cur!=root&&low[i]>=num[cur]){//如果当前顶点不是根节点并且满足low[i]>=num[cur],则当前顶点为割点
                        flag[cur]=1;
                    }
                    if(cur==root&&child==2){//如果是根节点且有两个以上孩子,则当前顶点为割点。
                        flag[cur]=1;
                    }
                }else if(i!=father){
                //否则如果当前顶点i曾经被访问过,
                //并且这个顶点不是当前当前顶点cur的父亲,则需要更新当前节点cur
                //能否访问到最早顶点的时间戳
                    low[cur]=min(low[cur],num[i]);
                }
            }
        }
    }
    int main()
    {
        int a,b;
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                e[i][j]=0;
            }
        }
        for(int i=1;i<=m;i++){
            scanf("%d %d",&a,&b);
            e[a][b]=e[b][a]=1;
        }
        root=1;
        dfs(1,root);
        for(int i=1;i<=n;i++){
            if(flag[i]) printf("%d ",i);
        }
        printf("
    ");
        /*for(int i=1;i<=n;i++){
            printf("%d ",low[i]);
        }
        printf("
    ");
        for(int i=1;i<=n;i++){
            printf("%d ",num[i]);
        }
        printf("
    ");*/
        return 0;
    }
    
    /*
    6 7
    1 4
    1 3
    4 2
    3 2
    2 5
    2 6
    5 6
    */
    

  • 相关阅读:
    tensorflow学习笔记五----------逻辑回归
    tensorflow学习笔记四----------构造线性回归模型
    tensorflow学习笔记三----------基本操作
    tensorflow学习笔记二----------变量
    tensorflow学习笔记一----------tensorflow安装
    大屏某区域滚动效果循环
    echarts轨迹图,各个城市线路图轨迹如何取值
    react组件,样式添加的方法
    微信小程序中hidden属性不生效
    react组件渲染编程html不成功
  • 原文地址:https://www.cnblogs.com/kzbin/p/9205221.html
Copyright © 2011-2022 走看看