POJ 1144 & Uva 315 Network 【求割点数目】

来源:互联网 发布:淘宝上如何找真弩 编辑:程序博客网 时间:2024/04/28 14:32

Network
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10855 Accepted: 5020链接:http://poj.org/problem?id=1144

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

55 1 2 3 4062 1 35 4 6 200

Sample Output

12

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个顶点,若干条边,求这个图中的割点的数目。

分析:

附:Tarjan求割点的算法讲解:http://www.cnblogs.com/en-heng/p/4002658.html
题目意思其实很简单。直接套用模板即可了,对于单行不确定输入元素个数的处理,我习惯用stringstream来处理,虽然大多时候用起来还是比较方便,但是在竞赛的时候暴露了一个弊端,就是stringstream处理的速度是非常慢的,建议同时熟练掌握strtok的作法。

代码实现:

#include <cmath>#include <cstdio>#include <string>#include <cstring>#include <sstream>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;//#pragma comment(linker, "/STACK:1024000000,1024000000")#define FIN             freopen("input.txt","r",stdin)#define FOUT            freopen("output.txt","w",stdout)#define CASE(T)         for(scanf("%d",&T);T--;)typedef long long LL;const int maxn = 100 + 5;const int maxm = 10000 + 5;int N, ans;bool Map[maxn][maxn];struct Edge{    int to, next;} edge[maxm];int head[maxn], tot;int Low[maxn], DFN[maxn];int Index;bool cut[maxn];void AddEdge(int u, int v){    edge[tot].to = v, edge[tot].next = head[u];    head[u] = tot++;}void Tarjan(int u, int pre){    int v;    Low[u] = DFN[u] = ++Index;    int son = 0;    for(int i = head[u]; ~i; i = edge[i].next)    {        v = edge[i].to;        if(v == pre) continue;        if(!DFN[v])        {            son++;            Tarjan(v, u);            if(Low[u] > Low[v]) Low[u] = Low[v];            if(u != pre && Low[v] >= DFN[u])            {                if(!cut[u]) ans++;                cut[u] = true;            }        }        else if(Low[u] > DFN[v]) Low[u] = DFN[v];    }    if(u == pre && son > 1)    {        if(!cut[u]) ans++;        cut[u]  = true;    }}void init(){    memset(Map, false, sizeof(Map));    ans = 0;    memset(head, -1, sizeof(head));    tot = 0;}stringstream BUF;string buf;void Read_Graph(){    int a, b;    while(1)    {        scanf("%d", &a);        if(a == 0) break;        getline(cin, buf);        BUF.clear();        BUF << buf;        while(BUF >> b)        {            Map[a][b] = Map[b][a] = true;        }    }    for(int i = 1; i <= N; i++)    {        for(int j = 1; j < i; j++)        {            if(Map[i][j])            {                AddEdge(i, j), AddEdge(j, i);            }        }    }}void Solve(){    memset(DFN, 0, sizeof(DFN));    memset(cut, false, sizeof(cut));    Index = 0;    for(int i = 1; i <= N; i++) if(!DFN[i])        {            Tarjan(i, i);        }    printf("%d\n", ans);}int main(){  //  FIN;    while(~scanf("%d", &N), N)    {        init();        Read_Graph();        Solve();    }    return 0;}


3 0
原创粉丝点击