POJ 1523、ZOJ 1119 SPF - from lanshui_Yang

来源:互联网 发布:热血传奇装备数据库 编辑:程序博客网 时间:2024/06/05 11:49

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 25 43 13 23 43 501 22 33 44 55 101 22 33 44 66 32 55 100

Sample Output

Network #1  SPF node 3 leaves 2 subnetsNetwork #2  No SPF nodesNetwork #3  SPF node 2 leaves 2 subnets  SPF node 3 leaves 2 subnets
    题目大意:给你一个连通图,让你判断去掉某个点及其关联的边后剩下的图是否连通,如果不连通,计算出剩下的连通分支个数。
    解题思路:这道题是典型的找无向图的关节点问题,顶点u是是关节点的充要条件如下:(1)如果顶点u是深度优先搜索生成树根,则u至少有两个子女。(2)如果u不是生成树的根,则它至少有一个子女w,从w出发,不可能通过w、w的子孙,以及一条回边(最多是一条)组成的路径到达u的祖先。
    代码如下:
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<cstdio>#include<algorithm>using namespace std ;const int MAXN = 1005 ;struct arcNode{    int adj ;    arcNode * next ;} ;arcNode *vert[MAXN] ;bool vis[MAXN] ; // 建立标记数组int low[MAXN] ;  // 记录从u或u的子孙出发(可以通过回边)可以到达的最低深度优先数int dfn[MAXN] ; // 记录深度优先数int sumfz[MAXN] ;  // 记录去掉某点及其相关联的边后,剩余的连通分量个数int son ;  // 记录树根的孩子个数int tmpdfn ;int sumnode ; //记录出现的结点的最大序号int ca = 0 ;void dfs(int u)  // 找关节点{    arcNode * p = vert[u] ;    while (p != NULL)    {        int v = p -> adj ;        if(!vis[v])        {            vis[v] = 1 ;            tmpdfn ++ ;            dfn[v] = low[v] = tmpdfn ;            dfs(v) ;            low[u] = min(low[u] , low[v]) ;            if(low[v] >= dfn[u])            {                if(u == 1)                {                    son ++ ;                }                else                {                    sumfz[u] ++ ;                }            }        }        else        {            low[u] = min(dfn[v] , low[u]) ; // 千万注意:这里是dfn(v) 不是,low(v)        }        p = p -> next ;    }}void dele(){    arcNode *p ;    int i ;    for(i = 1 ; i <= sumnode ; i ++)    {        p = vert[i] ;        while (p != NULL)        {            vert[i] = p -> next ;            delete p ;            p = vert[i] ;        }    }}void init(){    int a , b ;    while (scanf("%d" , & a) != EOF)    {        sumnode = 0 ;        tmpdfn = 0 ;        memset(vert , 0 , sizeof(vert)) ;        if(a == 0)            break ;        scanf("%d" , &b) ;        arcNode *p ;        if(a > sumnode)            sumnode = a ;        if(b > sumnode)            sumnode = b ;        p = new arcNode ;        p -> adj = b ;        p -> next = vert[a] ;        vert[a] = p ;        p = new arcNode ;        p -> adj = a ;        p -> next = vert[b] ;        vert[b] = p ;        while (scanf("%d", &a) != EOF)        {            if(a == 0)                break ;            scanf("%d" , &b) ;            if(a > sumnode)                sumnode = a ;            if(b > sumnode)                sumnode = b ;            p = new arcNode ;            p -> adj = b ;            p -> next = vert[a] ;            vert[a] = p ;            p = new arcNode ;            p -> adj = a ;            p -> next = vert[b] ;            vert[b] = p ;        }        son = 0 ;        memset(vis , 0 , sizeof(vis)) ;        memset(dfn , 0 , sizeof(dfn)) ;        memset(low , 0 , sizeof(low)) ;        memset(sumfz , 0  , sizeof(sumfz)) ;        vis[1] = 1 ;        dfn[1] = low[1] = 1 ;        tmpdfn = 1 ;        dfs(1) ;   // 这里默认点1是根节点        if(son > 1)        sumfz[1] = son ;        if(ca > 0)        puts("") ;        printf("Network #%d\n" , ++ ca) ;        int i ;        int pan = 0 ;        for(i = 1 ; i <= sumnode ; i ++)        {            if(sumfz[i] > 0)            {                pan = 1 ;                if(i != 1)                printf("  SPF node %d leaves %d subnets\n" , i , sumfz[i] + 1) ;                else                printf("  SPF node %d leaves %d subnets\n" , i , sumfz[i]) ;            }        }        if(pan == 0)        {            puts("  No SPF nodes") ;        }        dele() ; // 释放图    }}int main(){    init() ;    return 0 ;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 生完孩子后掉头发很厉害怎么办 头发又细又软又卷怎么办 后颈部没有头发掉光了想植发怎么办 头发掉的厉害怎么办怎么拯救掉头发 头发可以种植吗 如果是秃顶怎么办 染头发把手指甲染黑了怎么办 怀孕两个月下体流褐色分泌物怎么办 头发总是大把大把的得掉 怎么办 严重脱发怎么办去问南宁肤康 脱发严重怎么办去看南宁肤康 前额头发少怎么办 如何使头发增多 生完宝宝头发一把一把的掉怎么办 生完宝宝后头发掉的厉害怎么办 生完宝宝头发掉的厉害怎么办 生了小孩后头发掉很多怎么办 生了孩子头发掉的很厉害怎么办 母乳期头发掉的很厉害怎么办 宝宝吃母乳头发掉的厉害怎么办 头发油腻头皮屑多还掉头发怎么办 头发剪了中分刘海弯了怎么办 头发掉了长出来的头发很细怎么办? 头皮损伤毛囊怎么办还会长头发吗 一岁宝宝头发稀少怎么办能刮光头么 前编头发长了怎么办怎么梳理 九个月宝宝头发稀少不长怎么办 前牙吃饭咬合很深吃饭就痛怎么办 吃了甜的冷的就牙疼怎么办 吃热的凉的甜的牙疼怎么办 头发太细了想让头发变粗点怎么办 我的头发又少又很油该怎么办 头发油掉发头顶头发稀疏怎么办 我的头发天生就少又细怎么办 头发越来越少怎么办 用什么好呢 头发油掉头发怎么办吃什么药好 生完孩子三个月掉头发很厉害怎么办 电夹板夹头发现在掉头发怎么办 刚剪完的头发前面短后面长怎么办 头发太多太厚怎么办_百度经验 米诺地尔搽剂喷在头皮上痛怎么办 米诺地尔擦了头皮痒怎么办 头发又细又少一天不洗就油怎么办