码迷,mamicode.com
首页 > Web开发 > 详细

poj 1144 Network

时间:2017-06-22 21:53:28      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:sizeof   efi   ssi   etc   csdn   接下来   sof   pen   company   

                        poj 1144——Network
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13995   Accepted: 6371

Description

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

描述

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

输入

输入文件包括若组测试数据。每一组是一个网络,每一组测试数据的第一行是地点的总数量N<100.每个接下来最多N行包括一个数字表示一个地点和与它相连接的地点的数字。这些最多N行完全描述了整个网络,比如,网络中每个直接连接的两个地点被至少一行包括。一行内的所有数字都要用空格隔开。每组数据需要用单独的一个0结束。最后的块只有一行即N=0。

输出

输出除了最后一个组其他每一个组的critical地点的数量,每个块用一行输出。

 

思路

这个题差不多就是个tarjan求割点的板子。

就是我们在输入时处理起来有点麻烦。。。

代码:

 

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10000
using namespace std;
int x,y,n,tot,ans,tim;
int head[N],low[N],dfn[N],cut_point[N];
struct Edge
{
    int to,next,from;    
}edge[N];
void add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
void begin()
{
    ans=0,tot=1,tim=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(head,0,sizeof(head));
    memset(cut_point,0,sizeof(cut_point));
}
int tarjan(int now,int pre)
{
    int sum=0; bool boo=false;
    dfn[now]=low[now]=++tim;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(i==(pre^1)) continue;
        int t=edge[i].to;
        if(!dfn[t])
        {
            sum++;
            tarjan(t,i);
            low[now]=min(low[now],low[t]);
            if(low[t]>=dfn[now]) boo=true;
            //if(dfn[t]>low[now]) cut_edge[i]=1;    
        }
        else low[now]=min(low[now],dfn[t]);
    }
    if(!pre) 
    {
      if(sum>1) cut_point[now]=1;    
    }//要有大括号!!!!(唉,为了这个错误找了3天!!!!!)
    else if(boo) cut_point[now]=1; 
}
int main()
{
    char c;
    while(scanf("%d",&n)!=EOF&&n)
    {
        begin();
        while(scanf("%d",&x)!=EOF&&x)
        {
            while((c=getchar())!=\n)
            {
                scanf("%d",&y);
                add(x,y);add(y,x);
            }
        }
        tarjan(1,0);
        for(int i=1;i<=n;i++)
         if(cut_point[i]) ans++;
        printf("%d\n",ans);
    }
    return 0;
}

 

 

 

 

poj 1144 Network

标签:sizeof   efi   ssi   etc   csdn   接下来   sof   pen   company   

原文地址:http://www.cnblogs.com/z360/p/7056622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!