POJ - 1144 Network(缩点+输入技巧)

题目链接https://vjudge.net/contest/365311#problem/H
题目描述
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.
翻译
n个点,编号1~n,求割点的个数。
输入描述:第一行一个n,接下来最多N行。每一行都包含一个位置的编号,然后是与该位置有直线相连的一些位置的编号。
这些至多N行完全描述了网络。
一行中的所有数字都用一个空格隔开。每个块以仅包含0的行结束。最后一个块只有一行,且N = 0。
例如样例:
n=5,第一行的5 1 2 3 4,以5号为起点,可以到达的点有1 2 3 4 ,割点就一个:5

割点与桥(割边)的定义在无向图中才有定义

割点:无向连通图中,去掉一个顶点及和它相邻的所有边,图中的连通分量数增加,则该顶点称为割点。
桥(割边):无向联通图中,去掉一条边,图中的连通分量数增加,则这条边,称为桥或者割边。

有割边的图不一定有割点
两个割点之间的边不一定是割边。割边的两个端点不一定是割点。

  1. 判断割点(Tarjan)

(1)u为树根,且u有多于一个子树
(2)u不为树根,且满足存在(u,v)为树枝边(即u为v在搜索树中的父亲),并使得DFN(u)<=Low(v)(删除u后v以及v的子树不能到达u的祖先)

代码(邻接矩阵):

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N=1e2+10;
int low[N],dfn[N],dfstime;
bool cut[N];
vector<int>G[N];
void init()
{
    memset(low,0,sizeof(low));/*low(u)为u或u的子树能够回溯到的最早的栈中结点的DFN值*/
    memset(dfn,0,sizeof(dfn));/*dfn(u)为u结点的搜素次序编号(时间戳)*/
    dfstime=0;
    memset(cut,false,sizeof(cut));
}
void dfs(int u,int father)
{
    low[u]=dfn[u]=++dfstime;/*初始化时间戳*/
    int child=0;
    for(int i=0; i<G[u].size(); i++)/*所有的出边*/
    {
        int v=G[u][i];
        if(!dfn[v])
        {
            child++;/*子树的个数*/
            dfs(v,u);
            low[u]=min(low[v],low[u]);
            if((u==1&&child>1)||(u!=1&&low[v]>=dfn[u]))
                cut[u]=true;/*标记割点*/
        }
        else
            low[u]=min(low[u],dfn[v]);/*无向图不用判断是否在栈中*/
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1; i<=n; i++)
            G[i].clear();
        init();
        int u,v;
        while(~scanf("%d",&u)&&u)/*第一个起点*/
        {
            while(getchar()!='\n')/*输入技巧*/
            {
                scanf("%d",&v);
                G[u].push_back(v);/*双向边*/
                G[v].push_back(u);
            }
        }
        dfs(1,-1);/*1为根,虚设出1的根为-1*/
        int sum=0;
        for(int i=1; i<=n; i++)
        {
            if(cut[i])
                sum++;
        }
        printf("%d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值