zoukankan      html  css  js  c++  java
  • 【POJ 1523】SPF

    SPF
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 11019   Accepted: 4867

    Description

    Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible. 

    Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate. 

    Input

    The input will contain the description of several networks. A network description will consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers will range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should be ignored.

    Output

    For each network in the input, you will output its number in the file, followed by a list of any SPF nodes that exist. 

    The first network in the file should be identified as "Network #1", the second as "Network #2", etc. For each SPF node, output a line, formatted as shown in the examples below, that identifies the node and the number of fully connected subnets that remain when that node fails. If the network has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes.

    Sample Input

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

    Sample Output

    Network #1
      SPF node 3 leaves 2 subnets
    
    Network #2
      No SPF nodes
    
    Network #3
      SPF node 2 leaves 2 subnets
      SPF node 3 leaves 2 subnets

    Source

    题意:给出一个无向图,求割点以及去除这个点后图分为几部分;水过去啦
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define MAXN 1005
    #define MAXM 1005*1005
    struct Edge
    {
        int to,next;
    }edge[MAXM];
    int Low[MAXN],DFN[MAXN],first[MAXN],son,vis[MAXN];
    int n,count,cut[MAXN],tot,root;
    void addedge(int v,int w)
    {
        edge[tot].to=w;
        edge[tot].next=first[v];
        first[v]=tot++;
    }
    void Tarjan(int v)
    {
        DFN[v]=Low[v]=++count;
        for(int i=first[v];i!=-1;i=edge[i].next)
        {
            int j=edge[i].to;
            if(!DFN[j])
            {
                    Tarjan(j);
                    if(root==v)
                    {
                        son++;
                        if(son>1)
                            cut[v]=1;
                    }
                    else
                    {
                        Low[v]=min(Low[j],Low[v]);
                        if(DFN[v]<=Low[j])cut[v]=1;
                    }
            }
            else
            {
                Low[v]=min(Low[v],DFN[j]);
            }
        }
    }
    void dfs(int u)
    {
        vis[u]=1;
        for(int i=first[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(!vis[v])
            {
                dfs(v);
            }
        }
    }
    int main()
    {
        int x,y,t=0;
        while(scanf("%d",&x),x)
        {
            t++;
            memset(DFN,0,sizeof(DFN));
            memset(first,-1,sizeof(first));
            memset(Low,0,sizeof(Low));
            memset(cut,0,sizeof(cut));
            n=0;
            scanf("%d",&y);
            addedge(x,y);
            addedge(y,x);
            n=max(x,y);
            count=0;
            while(scanf("%d",&x),x)
            {
                scanf("%d",&y);
                addedge(x,y);
                addedge(y,x);
                n=max(x,y);
            }
     
     
                    son=0;
                    root=1;
                    Tarjan(1);
            printf("Network #%d
    ",t);//cout<<1111<<endl;
            int ans=0;
            int flag=0;
            for(int i=1;i<=n;i++)
            {
                if(cut[i]==1)
                {
                    flag=1;
                    memset(vis,0,sizeof(vis));
                    vis[i]=1;
                    int son1=0;
                    for(int j=first[i];j!=-1;j=edge[j].next)
                    {
                        int k=edge[j].to;
                        if(!vis[k])
                        {
                            dfs(k);
                            son1++;
                        }
                    }
                    printf("  SPF node %d leaves %d subnets
    ",i,son1);
                }
            }
     
            if(!flag)
                printf("  No SPF nodes
    ");
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    三种数据解析方式
    requests模块相关用法
    urllib模块基本用法
    django复习题
    scrapy框架编写向redis数据库中存储数据的相关代码时报错解决办法
    并发编程练习题
    网络编程 socket 开发练习题
    面向对象编程设计练习题(2)
    pytest-fixtured
    Python 删除某一目录下的所有文件或文件夹
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11154895.html
Copyright © 2011-2022 走看看