zoj——1311 Network

Network

Time Limit: 2 Seconds      Memory Limit: 65536 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 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 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

 

题意:

电话公司(TLC)正在建立一个新的电话电缆网络。它们连接数个整数,从1到n,没有两个地方有相同的数字。这些线是双向的,总是连接在一起的两个地方,在每一个地方的电话线结束在交换机。每一个地方都有一个电话交换机。从每一个地方都可以通过其他地方的线,但它不需要直接连接,它可以通过几个交换。有时电源在一个地方故障,然后交换不工作。TLC的官员意识到,在这种情况下,除了失败的地方是不可到达的之外,也可能导致其他地方不能互相连接。在这种情况下,我们会说发生故障的地方是关键的。现在,官员们正试图编写一个程序来找出所有这些关键地点的数量。帮助他们。

思路:

裸题,tarjan求割点的数量

但是输入的时候特别恶心、、、

代码:

 

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10010
using namespace std;
char ch[N];
int n,m,x,y,tim,tot,ans;
int dfn[N],low[N],vis[N],head[N],cut_point[N];
struct Edge
{
    int from,to,next;
}edge[N];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
void add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
void clean()
{
    ans=0,tim=0;tot=1;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(head,0,sizeof(head));
    memset(cut_point,0,sizeof(cut_point));
}
void tarjan(int now,int pre)
{
    int sum=0; bool boo=false; vis[now]=true;
    dfn[now]=low[now]=++tim;
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if((pre^1)==i) continue;
        if(!dfn[t])
        {
            sum++;tarjan(t,i);
            low[now]=min(low[now],low[t]);
            if(low[t]>=dfn[now]) boo=true;
        }
        else low[now]=min(low[now],dfn[t]);
    }
    if(!pre) {if(sum>1) ans++;}
    else if(boo) ans++;
}
int main()
{
    while(1)
    {
        n=read();if(n==0) break;
        clean();
        while(scanf("%d",&x)&&x)
        {
            while(getchar()!='\n')
            {
                scanf("%d",&y);
                add(x,y),add(y,x);
            }
        }
        for(int i=1;i<=n;i++)
         if(!dfn[i]) tarjan(i,0);
        printf("%d\n",ans);
    }
    return 0;
}

突然发现自己好zz啊,老是把东西写反、、、

 

转载于:https://www.cnblogs.com/z360/p/7384448.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值