Network (POJ 1144) (无向图的割顶)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41061455/article/details/81592666

Time limit       1000 ms        Memory limit       10000 kB

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.

题意:求这个无向图的割顶数目。

割顶:对于无向图G,如果删除某个点u后,连通分量数目增加,则称u为图的关节点或割顶。对于连通图,割顶就是删除之后使图不再连通的点。

Tarjan算法( O(n+m) ):

在Tarjan算法里面,有两个时间戳非常重要,一个是dfn,意为深度优先数,即代表访问顺序;一个是low,意为通过反向边能到达的最小dfn。于是,上述定理中第二个条件(非根结点)可以简单地写成low[v]>=dfn[u]。 --转自博客

在无向连通图G中, 
1、根结点u为割顶当且仅当它有两个或者多个子结点 
2、非根结点u为割顶当且仅当u存在结点v,使得v极其所有后代都没有反向边可以连回u的祖先(u不算)

设low[u]为u及其后代所能连回的最早的祖先的pre值,则定理中的条件可以简写成结点u存在一个子结点v,使得low[v]>=pre[u]

//#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e2+10;
vector<int>G[maxn];
int pre[maxn],low[maxn],iscut[maxn],dfs_clock;
void init()
{
    memset(pre,0,sizeof(pre));
    memset(low,0,sizeof(low));
    memset(iscut,0,sizeof(iscut));
    for(int i=0;i<maxn;i++) G[i].clear();
    dfs_clock=0;
}
void dfs(int u,int fa)//u在DFS树中的父结点是fa
{
    low[u]=pre[u]=++dfs_clock;
    int child=0;//子结点数目
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(!pre[v])//没有访问过v
        {
            child++;
            dfs(v,u);
            low[u]=min(low[u],low[v]);//用后代的low函数更新当前的low函数
            if(low[v]>=pre[u])
                iscut[u]=1;
        }
        else if(v!=fa)
            low[u]=min(low[u],pre[v]);//用反向边更新u的low函数
    }
    if(fa<0&&child==1) iscut[u]=0;//就一个后代(共两个节点)
}
int main()
{
    int t;
    while(scanf("%d",&t)==1&&t)
    {
        init();
        getchar();
        string tmp;
        int n;
        while(getline(cin,tmp))
        {
            int l,r;
            stringstream ss(tmp);
            ss>>l;
            if(!l) break;
            while(ss>>r)
            {
                G[l].push_back(r);
                G[r].push_back(l);
            }
        }
        for(int i=1;i<=t;i++)
            if(!pre[i]) dfs(i,-1);
        int ans=0;
        for(int i=1;i<=t;i++)
            if(iscut[i]) ans++;
        printf("%d\n",ans);
    }
}

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页