Network POJ 1144

该程序解决了一个图论问题,即在一个电话线路网络中,当某个节点断电时,可能导致其他部分无法通信的节点被称为关键节点(割点)。程序通过DFS遍历所有节点,确定哪些节点的失效能导致网络分割。输入包含多个网络实例,每个实例的节点和连接关系,输出每个实例中割点的数量。示例输入和输出分别展示了如何处理网络并找出割点。

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

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

题意:输入一个图,求割点数目。
判断割点的板子题。num数组存dfs搜索时的序列,low存当前节点的子节点能返回的最小根节点。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EnewPosstr 1e-9
#define newPosI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int maxn = 10005;
struct Edge {
    int to, next;
}edge[maxn];
int dfn,ans;
int head[maxn];
int p[maxn];
int cnt;
int vis[maxn];
int low[maxn], num[maxn];
int child ;
void init() {
    cnt = 0;
    child = 0;
    for (int i = 0; i < maxn; i++)
    {
        p[i] = 0;
        head[i] = -1;
        edge[i].next = -1;
        vis[i] = 0;
        low[i] = 0;
     //   num[i]=0;
    }
    dfn = 0;
    ans = 0;
}
void add(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
void dfs(int u,int fa)
{
    vis[u] = 1;
    num[u] = low[u] = ++dfn;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int f = edge[i].to;
        if (!vis[f])
        {
            if(u==1)                 //判断根有几个子节点。
            child++;
            dfs(f, u);
            low[u] = min(low[f], low[u]);     
            //更新当前节点的子节点所能回到的最小根,当搜完一条路径返回时更新(逆序更新)。
            if (low[f] >= num[u] && u != 1)   
                //如果搜索的这条路径没有回边,侧u为割点。
                p[u] = 1;
        }
        else if (num[f] < num[u] && f != fa)   //如果发现回边,更新low值。
        {
            low[u] = min(low[u], num[f]);
        }
        if (u == 1 && child >= 2)
            p[1] = 1;
    }
    return;
}
int main() 
{
    int n;
    while (cin >> n&&n)
    {
        init();
        int a;
        int b;
        while (scanf_s("%d",&a)&&a)
        {
            while (getchar() != '\n')
            {
                scanf_s("%d", &b);
                add(a, b);
                add(b, a);
            }
        }
        dfs(1, -1);
        for (int i = 1; i <= n; i++)
            if (p[i])ans++;
        cout << ans << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值