POJ-1144 Network——Trajan+割点

【题目描述】

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

【题目分析】
题目要求求割点,首先什么是割点呢?就是一个连通图/连通分量中将这个点去掉就不再是一个连通图的点。
我们想要求割点,很显然可以直接暴力,先在一个连通图里面删去这个点所有的边,如果变得不连通了就是割点。可是这样时间复杂度显然是不允许的。
我们再来仔细观察割点有什么特点:删去这个点就有一些点无法到达。
我们结合Trajan算法中的DFS搜索树进行分析,如果一个点是割点,如果他后面的子树就无法访问到前面的点,那么删去这个点,前面的点也就无法访问后面的点了,前后两部分无法相互访问,那说明这个点就是一个割点。我们如何判断后面的子树不会访问到前面的点呢?我们可以用Trajan算法中的LOW数组 ,如果子树的LOW数组的值比这个点的DFN的值还大说明这个点就是一个割点。
可是对于根节点(开始访问的点)来讲,就算这个点是一个割点后面的点也不会访问到他前面的点,但是这个根节点不同搜索树上的点是互相没有关系的,如果他有两个或者两个以上的搜索树,那么删去根节点也可以形成割点。但是对于一般的点,就算他有好几个儿子节点也不一定是一个割点,因为这些儿子之中有可能有之前访问过的点,但是对于访问过的点我们是不会放入这个搜索树的,所以不同搜索树有可能访问到同一个之前的点,这样就算删去这个点也可能有通路。但是对于根节点就无法访问到更前面的点了,所以就可以确定是一个割点。

总结一下,割点总共就两类:

  1. 含有两个或者两个以上子树的根节点u ( u = = r o o t (u==root (u==root && s o n &gt; 1 ) son&gt;1) son>1)
  2. 子树节点v的搜索树中不含有祖先节点的点u ( u ! = r o o t (u!=root (u!=root && L O W [ v ] &gt; = D F N [ u ] ) LOW[v]&gt;=DFN[u]) LOW[v]>=DFN[u])

【AC代码】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;

typedef long long ll;
const int MAXN=105;
const int MAXM=MAXN*MAXN;
struct node
{
    int v,next;
}Edge[MAXM];
int head[MAXN],tot;
int DFN[MAXN],LOW[MAXN];
bool cnt[MAXN];
int vis[MAXN],Time;
int n,root,ans;

void init()
{
    memset(head,0,sizeof(head));
    tot=0;
    memset(vis,0,sizeof(vis));
    Time=0;
    memset(cnt,0,sizeof(cnt));
}

void AddEdge(int u,int v)
{
    tot++;
    Edge[tot].v=v; Edge[tot].next=head[u];
    head[u]=tot;
}

void read()
{
    int u,v;
    while(~scanf("%d",&u) && u)
    {
        while(getchar()!='\n')
        {
            scanf("%d",&v);
            AddEdge(u,v); AddEdge(v,u);
        }
    }
}

void Trajan(int x,int fa)
{
    int v;
    int son=0;
    DFN[x]=LOW[x]=++Time;
    vis[x]=1;
    for(int i=head[x];i;i=Edge[i].next)
    {
        v=Edge[i].v;
        if(vis[v]==0)
        {
            Trajan(v,x);
            son++;
            LOW[x]=min(LOW[x],LOW[v]);
            if(x==root &&son>1 || x!=root && LOW[v]>=DFN[x])
            {
                cnt[x]=true;
            }
        }
        else if(vis[v]==1)
        {
            LOW[x]=min(LOW[x],DFN[v]);
        }
    }
    vis[x]==2;	//说明这个搜索数完结了和其他的点就没有什么关系了,因此设为2其他点就不会访问到
}

void solve()
{
    ans=0;
    root=1;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            root=i;
            Trajan(i,i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(cnt[i]) ans++;
    }
    printf("%d\n",ans);
}

int main()
{
    while(~scanf("%d",&n) && n)
    {
        init();
        read();
        solve();
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值