Network UVA - 315(Tarjan+连通性问题:求割点)

题意:

存在n个电话公司的网络连接站点,每个站点之间相互连通,是一个连通图,问,如果去掉一个站点,能够将整个网络体系变得不再连通,那么这样的点有几个,即求它的割点个数。

题目:

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

分析:

无向图中,如果有一个顶点,删除这个顶点以及这个顶点相关联的边以后,图的连通分量增多,就称这个点集为割点。
判断一个顶点是不是割点除了从定义,还可以从DFS(深度优先遍历)的角度出发。我们先通过DFS定义两个概念。
假设DFS中我们从顶点U访问到了顶点V(此时顶点V还未被访问过),那么我们称顶点U为顶点V的父顶点,V为U的孩子顶点。
在顶点U之前被访问过的顶点,我们就称之为U的祖先顶点。
显然如果顶点U的所有孩子顶点可以不通过父顶点U而访问到U的祖先顶点,那么说明此时去掉顶点U不影响图的连通性,U就不是割点。
相反,如果顶点U至少存在一个孩子顶点,必须通过父顶点U才能访问到U的祖先顶点,那么去掉顶点U后,顶点U的祖先顶点和孩子顶点就不连通了,说明U是一个割点。
情况1:1.u为当前搜索树的根结点且u的子树数量大于1 即 (x==f&&son>=2)
情况2:2.u不为根且存在树边(u,v)使dfn(u)<=low(v) 对应(low[v] >= dfn[x] && x != f)

AC代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1e3+10;
const int M = 1e6+10;
struct node
{
    int v,next;
    int u;
} s[M];
int ans ;
int n,a,b;
int dp[N];
int top,num;
int dfn[N],low[N];
int cut[N];//标记是否为割点
int id;
void tarjan(int x,int f) //当前结点以及父节点
{
    low[x] = dfn[x] = ++id; //(1)结点u初值
    int son = 0; //定义根结点孩子个数
    for(int i = dp[x]; ~i; i= s[i].next)//(2)枚举每一条边
    {
        int v = s[i].v;//(3)入过结点u为强连通分量的根节点时
        if(!dfn[v]) //(4)没有被访问时候递归搜索
        {
            tarjan(v,x);
            low[x] = min(low[x],low[v]);
            if(low[v] >= dfn[x] && x != f) //(5)如果该子节点不能到达祖先节点 (标记)
                cut[x] = 1;
            if(x == f)//每从根结点往孩子走就加1
                son++;
        }
        low[x] = min(low[x],dfn[v]);
    }
    if(x == f && son >= 2)  //(6)根节点且孩子个数大于2即为割点
        cut[x] = 1;
}
void add(int u,int v)
{
    s[top].v = v;
    s[top].next = dp[u];
    dp[u] = top++;
}
void init()
{
    memset(dp,-1,sizeof(dp));
    memset(cut,0,sizeof(cut));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    id = ans = top = 0;
}
int main()
{

    while(~scanf("%d",&n) &&n)
    {
        init();
        while(scanf("%d",&a) &&a)
        {
            char ch;
            while(scanf("%d%c",&b,&ch))
            {
                add(a,b),add(b,a);
                if(ch == '\n')
                    break;
            }
        }
        for(int i = 1; i<=n; i++)
        {
            if(!dfn[i])
                tarjan(i,i);
        }
        for(int i = 1; i<=n; i++)
        {
            if(cut[i])
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值