poj 1144 Network (tarjan割点模板)

本文提供了一道经典的图论题目——POJ1144 Network的详细解析,通过邻接矩阵和邻接链表两种方式实现了求解网络中关键节点的数量。该题目的核心在于使用深度优先搜索(DFS)来确定网络中哪些节点的故障会导致其他节点间失去连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:poj 1144

Network

Time Limit: 1000MS Memory Limit: 10000K

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

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.

模板题。
输入输出比较麻烦,要自行判断是否到达行末。

邻接矩阵实现:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#define MAX 105
#define INF -0x7f7f7f7f

using namespace std;

int dfn[MAX];
int low[MAX];
bool vis[MAX];
int depth, n, m, root, rt_cnt, gra[MAX][MAX];

void init()
{
    root = 1;
    depth = 0;
    rt_cnt = 0;
    memset(dfn, -1, sizeof(dfn));
    memset(gra, 0, sizeof(gra));
    memset(vis, false, sizeof(vis));
    memset(low, 0, sizeof(low));
}

void dfs(int cur, int father)
{
    dfn[cur] = low[cur] = depth++;
    for(int i = 1; i <= n; i++)
    {
        if(gra[cur][i] == 1)
        {
            if(dfn[i] == -1)
            {
                dfs(i, cur);
                low[cur] = min(low[cur], low[i]);

                if(cur == root)
                    rt_cnt++;
                else if(low[i] >= dfn[cur])
                    vis[cur] = true;
            }
            else if(i != father)
            {
                low[cur] = min(low[cur], dfn[i]);
            }
        }
    }
}

int main()
{
    while(scanf("%d", &n) && n)
    {
        init();
        int tmp;
        while(scanf("%d", &tmp) && tmp)
        {
            while(getchar() != '\n')
            {
                int t;
                scanf("%d", &t);
                gra[tmp][t] = gra[t][tmp] = 1;
            }
        }

        dfs(1, root);
        int cnt = 0;
        for(int i = 2; i <= n; i++)
        {
            if(vis[i])
                cnt++;
        }
        if(rt_cnt > 1)
            cnt++;
        printf("%d\n", cnt);
    }
    return 0;
}

运行结果:
这里写图片描述

邻接链表实现:

#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#define MAX 105
#define INF -0x7f7f7f7f

using namespace std;

int dfn[MAX], low[MAX];
vector<int> V[MAX];
bool vis[MAX];
int depth, n, m, root, rt_cnt;

void init()
{
    root = 1;
    depth = 0;
    rt_cnt = 0;
    memset(dfn, -1, sizeof(dfn));
    memset(vis, false, sizeof(vis));
    memset(low, 0, sizeof(low));
    memset(V, 0, sizeof(V));
}

void dfs(int cur, int father)
{
    dfn[cur] = low[cur] = depth++;
    for(int i = 0; i < V[cur].size(); i++)
    {
        int v = V[cur][i];
        if(dfn[v] == -1)
        {
            dfs(v, cur);
            low[cur] = min(low[cur], low[v]);

            if(cur == root)
                rt_cnt++;
            else if(low[v] >= dfn[cur])
                vis[cur] = true;
        }
        else if(v != father)
        {
            low[cur] = min(low[cur], dfn[v]);
        }
    }
}

int main()
{
    while(scanf("%d", &n) && n)
    {
        init();
        int tmp;
        while(scanf("%d", &tmp) && tmp)
        {
            while(getchar() != '\n')
            {
                int t;
                scanf("%d", &t);
                V[tmp].push_back(t);
                V[t].push_back(tmp);
            }
        }
        dfs(1, root);
        int cnt = 0;
        for(int i = 2; i <= n; i++)
        {
            if(vis[i])
                cnt++;
        }
        if(rt_cnt > 1)
            cnt++;
        printf("%d\n", cnt);
    }
    return 0;
}

运行结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值