O - SPF (tarjan求割点及割点对应的连通分量数)

O - SPF

 

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

思路:

tarjan求割点(关键点)及割点对应的连通分量数

概念: 
在一个无向连通图中,如果删去某个顶点和与他相关联的边可以使得图不连通,则称该顶点为关节点。

关节点的求解tarjan算法: 
在无向连通图中,以某个顶点为根节点进行深度优先搜索,则在深度优先搜索树中,每个节点都有一个深度优先数dfn[i]。如果u是v的祖先节点,那么一定有dfn[u]< dfn[v]。 
回边:在图中有些边不在深度优先搜索生成树中,则称这些属于图但不属于生成树的边为回边。 
关节点的条件: 
1:如果在生成树中,根节点有两个或以上的孩子节点,那么根节点为关节点。 
2:在生成树中,一个顶点(不是根节点)若不是关节点,那么他的所有的孩子节点一定可以通过回边连接到该顶点的祖先节点。(由于形成了回路,这样删去了该顶点后图依然是连通的)否则该顶点为关节点。

我们为每个顶点u定义一个low值,low[u]表示从u或u的孩子出发通过回边可以到达的最低深度优先数。 
所以上面的两个条件转化为了下面的一句话:u或者是具有两个或以上子女的深度优先生成树的根,或者虽然不是根,但他有一个子女w使得low[w]>= dfn[u]。(取等号的原因是即使能回到该顶点又没用,因为会被删掉。)

删去关节点后把原来的图分成了几个连通分量? 
1:如果是根节点,则有几个子女就分成几个连通分量。 
2:不是根节点,则有d个子女w,使得low[w] >= dfn[u],就分成d+1个。
--------------------- 
作者:STILLxjy 
来源:CSDN 
原文:https://blog.csdn.net/stillxjy/article/details/52049394 
版权声明:本文为博主原创文章,转载请附上博文链接!

AC历程:

要求出这些连接点的序号范围,(最小值-最大值)minn-maxx(见代码)

输出有两个空格,一个换行

代码:

#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2e3+7;
#define mem(a,b) memset(a,b,sizeof(a))
vector<int>ve[maxn];

//low[]i及i的子孙相连的辈分最高的祖先节点所在的深度
//tot树深,T_cou连通分量的个数
//belong[]属于哪个连通分量,in_stack[]是否在栈中
//subnets[u] 去掉结点u后,形成的连通分量
//in[]入度  out[]出度
int dfn[maxn],low[maxn];
int tot,n,ans;
int subnets[maxn];
int root;
void init()
{
    mem(dfn,0);
    mem(low,0);
    mem(subnets,0);
    tot=ans=0;
    for(int i=1; i<maxn; i++)
        ve[i].clear();
}
void tarjan(int x,int y)  //x为子节点,y为父节点
{
    dfn[x]=low[x]=++tot;
    int T_cou=0;
    for(int i=0; i<ve[x].size(); i++)  //i从0开始
    {
        int v=ve[x][i];

        if(!dfn[v])
        {
            tarjan(v,x);
            T_cou++;
            low[x]=min(low[x],low[v]); //若是v这个子节点能够绕过父节点x到达更小的tot;
            //相当于x这个父节点也能到子节点的这个最小的tot;
            if(x==root&&T_cou>1)
                subnets[x]++;
            if(x!=root&&low[v]>=dfn[x])
                subnets[x]++;

        }
        else if(v!=y)
        {
            low[x]=min(low[x],dfn[v]);
        }
    }
}
int main()
{
    int u,v,maxx,minn,cas=1;
    while(~scanf("%d",&u)&&u)
    {
        init();
        maxx=-1;
        minn=0x3f3f3f3f;
        int flag=0;
        scanf("%d",&v);
        ve[u].push_back(v);
        ve[v].push_back(u);
        maxx=max(maxx,max(u,v));
        minn=min(minn,min(u,v));
        while(~scanf("%d",&u)&&u)
        {
            scanf("%d",&v);
            ve[u].push_back(v);
            ve[v].push_back(u);
            maxx=max(maxx,max(u,v));
            minn=min(minn,min(u,v));
        }
        root=minn;
        for(int i=1; i<=maxx; i++)
            tarjan(i,root);
        printf("Network #%d\n",cas++);
        for(int i=1; i<=maxx; i++)
        {
            if(subnets[i])
            {
                printf("  SPF node %d leaves %d subnets\n",i,subnets[i]+1);
                flag=1;
            }
        }
        if(flag==0)
            printf("  No SPF nodes\n");
        puts("");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值