连通图关节点求解详解--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个。

理解了上面的思想的话下面例题就是全裸的了。


例题:POJ1523
SPF
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7923 Accepted: 3624
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
Greater New York 2000

分析:
此题就是给一个连通图,问有哪些关节点,并输出删去他后会分成几个连通分量。
坑点:输出时开头有两个空格,是两个不是一个。。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

const int maxn=1010;
int E[maxn][maxn];
int vis[maxn];
int dfn[maxn];
int low[maxn];
int subnets[maxn];
int nodes;
int deep;
int rson;

void init()
{
    low[1]=dfn[1]=1;
    deep=1;rson=0;
    memset(vis,0,sizeof(vis));
    vis[1]=1;
    memset(subnets,0,sizeof(subnets));
    return ;
}

void dfs(int u)
{
    for(int v=1;v<=nodes;v++)
    {
        if(E[u][v])
        {
            if(!vis[v])
            {
                vis[v]=1;
                ++deep;
                dfn[v]=low[v]=deep;
                dfs(v);
                low[u]=min(low[u],low[v]);
                if(low[v]>=dfn[u])
                {
                    if(u==1) rson++;
                    else subnets[u]++;
                }
            }
            else low[u]=min(low[u],dfn[v]);
        }
    }
    return ;
}

int main()
{
    int u,v,cas=1;
    while(scanf("%d",&u))
    {
        if(u==0) break;
        scanf("%d",&v);
        nodes=0;
        memset(E,0,sizeof(E));
        if(nodes<u) nodes=u;
        if(nodes<v) nodes=v;
        E[u][v]=E[v][u]=1;
        while(scanf("%d",&u))
        {
            if(u==0) break;
            scanf("%d",&v);
            if(nodes<u) nodes=u;
            if(nodes<v) nodes=v;
            E[u][v]=E[v][u]=1;
        }
        if(cas>1) printf("\n");
        printf("Network #%d\n",cas++);
        init();
        dfs(1);
        if(rson>=2) subnets[1]=rson-1;
        int flag=0;
        for(int i=1;i<=nodes;i++)
        {
            if(subnets[i])
            {
                flag=1;
                printf("  SPF node %d leaves %d subnets\n",i,subnets[i]+1);
            }
        }
        if(!flag)
            printf("  No SPF nodes\n");
    }
    return 0;
}

重连通分量的求解:
根据上面的分析,我们可以很快的找出无向连通图的关节点。那么我们其实可以在求关节点的时候顺便将每个重连通分量求出来,方法如下:
利用一个栈,存储当前连通分量,在DFS过程中没找到生成树的一条边或回边,就把这条边加入栈中。如果遇到某个顶点u的子女顶点v满足low[v]>=dfn[u],说明u是割点,则把边从栈中一条条取出来,直到遇到边< u,v > ,取出的边和其相关顶点组成了一个重连通分量。
算法和找关键的唯一区别在于:找关节点时满足low[v]> =dfn[u]则做相应的标记,而求重连通分量时是将边一条条出栈中输出来而已。
由于某条边可能以如(2,3),(3,2)的形式重复进入,所以要做相应的标记处理。

样例:
输入:第一行两个数n,m。表示顶点数和边数,。然后接下来m行每行一条边,顶点序号从1开始。
输出: 每行输出一个重连通分量。

Input:
7 9
1 2
1 3
1 6
1 7
2 3
2 4
2 5
4 5
6 7
Output:
5-2 4-5 2-4
3-1 2-3 1-2
7-1 6-7 1-6

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <cmath>
using namespace std;

const int maxn=1001;
const int maxm=10010;
struct edge
{
    int u,v;
    void output(){printf("%d-%d ",u,v);}
    int cmp(edge &t)
    {
        return (u==t.u && v==t.v)||(v==t.u && u==t.v);
    }
};
edge edges[maxm];
int E[maxn][maxn];
int vis[maxn];
int dfn[maxn];
int low[maxn];
int deep,n,m,se;

void init()
{
    low[1]=dfn[1]=1;
    deep=1;
    memset(vis,0,sizeof(vis));
    vis[1]=1;
    memset(edges,0,sizeof(edges));
    se=-1;

}

void dfs(int u)
{
    for(int v=1;v<=n;v++)
    {
        if(E[u][v]==1)
        {
            edge t;t.u=u;t.v=v;
            edges[++se]=t;
            E[u][v]=E[v][u]=2;//标记,防止边重复进入
            if(!vis[v])
            {
                vis[v]=1;
                dfn[v]=low[v]=++deep;
                dfs(v);
                low[u]=min(low[u],low[v]);
                if(low[v]>=dfn[u])
                {
                    edge t1;
                    while(1)
                    {
                        if(se<0) break;
                        t1=edges[se--];
                        t1.output();
                        if(t1.cmp(t)) break;
                    }
                    printf("\n");
                }
            }
            else low[u]=min(low[u],dfn[v]);
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    int cas=1,u,v;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0 && m==0) break;
        memset(E,0,sizeof(E));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            E[u][v]=E[v][u]=1;
        }
        init();
        dfs(1);
    }
    return 0;
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值