【Tarjan求割点数量+割点模板】POJ - 1144 R - Network

用tarjan算法求割点

原理:在一棵DFS树中

根root是割顶当且仅当它至少有两个儿子

其他点v是割顶当且仅当它有一个儿子u,从u或者u的后代出发没有指向v祖先(不含v)的B边,则删除v以后u和v的父亲不连通,故为割顶

  1. 基本算法同tarjan经典算法

  2. 每遍历一个新的(颜色为白色)u的儿子v都记录个数

  3. low[u]值更新后进行以下判断(前提v未被遍历过):

  1. u为树根,且儿子个数大于1

  2. u不为树根,但low[v]>=dfn[u](说明v及其子节点都不能达到u以上的父亲节点)

满足以上任意条件u便为割点,记录在数组里,tarjan完成后再输出(中途输出会重复)

R - Network  POJ - 1144

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

Hint

You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.

 

一个电话线公司(简称TLC)正在建立一个新的电话线缆网络。
他们连接了若干个地点分别从1到N编号。没有两个地点有相同的号码。
这些线是双向的并且能使两个地点保持通讯。
有时候某个地点供电出问题时,交换机就会停止工作。
TLC的工作人员意识到,除非这个地点是不可达的,否则这种情况就会发生,
它还会导致一些其它的地点不能互相通讯。在这种情况下我们会称这个地点(错误发生的地方)为critical。
现在工作人员想要写一个程序找到所有critical地点的数量.
就是求割点的数量

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int e,index;
int head[105],nxt[10005],to[10005];
int low[105],dfn[105],ans[105];

void init()
{
    e=index=0;
    memset(head,-1,sizeof(head));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(ans,0,sizeof(ans));
}

void add(int u,int v)
{
    e++;
    to[e]=v;
    nxt[e]=head[u];
    head[u]=e;
}

void tarjan(int u)
{
    low[u]=dfn[u]=++index;
    int son=0;
    for(int i=head[u];i!=-1;i=nxt[i])
    {
        int v=to[i];
        if(!dfn[v])
        {
            tarjan(v);
            son++;
            low[u]=min(low[u],low[v]);
            if((u==1&&son>1)||(u!=1&&low[v]>=dfn[u])) ans[u]++;
            //1、u为树根,且儿子个数大于1
            //2、u不为树根,但low[v]>=dfn[u](说明v及其子节点都不能达到u以上的父亲节点)
        }
        else low[u]=min(low[u],dfn[v]);
    }
}

int main()
{
    int n,a,b;
    char c;
    while(~scanf("%d",&n)&&n!=0)
    {
        init();
        while(scanf("%d",&a)&&a!=0)
        {
            do{
                scanf("%d",&b);
                if(a==b) continue;
                add(a,b);
                add(b,a);
                c=getchar();
            }while(c!='\n');
        }
        tarjan(1);
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            if(ans[i]) sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值