其他分享
首页 > 其他分享> > 统计无向图中割点的个数:POJ1144 网络

统计无向图中割点的个数:POJ1144 网络

作者:互联网

题目描述:

        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.

输入:

        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;

输出:

        The output contains for each block except the last in the input file one line containing the number of critical places.   

输入样例:

5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0

输出样例:

1
2

解题思路:

        通过题目描述中的critical places,大致可以知道是找出割点的个数,割点可以通过Tarjan算法求得,用一个数组标记割点,之后统计割点的个数即可。

解题步骤:

        1、处理输入数据:由于邻接点是以每个中间有空格的形式出现,在读取源点后,必须在循环中空格和字符交替读取,来读取邻接点,当读取到‘\n'时,说明该行读取完毕;

        2、创建一个无向图,用Tarjan算法函数找到图中的割点;

        Tarjan算法应用:无向图找割点_可惜浅灰的博客-CSDN博客

        3、用bool类型数组标记割点,并统计数量输出即可。

代码实现:

#include <iostream>
using namespace std;
#define maxn 101

int n;
int head[maxn], cnt;
int dfn[maxn], low[maxn], num;
int sum;
bool is_critical[maxn];

class Edge
{
public:
    int to;
    int next;
};
Edge edge[maxn << 1];

void InsertEdge(const int& u, const int& v)
{
    edge[++cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt;
}

void Tarjan(int u, int fa)
{
    dfn[u] = low[u] = ++num;
    int flag = 0;
    for (int i = head[u]; i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (v == fa)
        {
            continue;
        }
        if (!dfn[v])
        {
            Tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if (low[v] >= dfn[u])
            {
                flag++;
                if (u != fa || flag > 1)
                {
                    is_critical[u] = true;
                }
            }
        }
        else
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
}

void Init()
{
    memset(edge, 0, sizeof(edge));
    memset(head, 0, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(is_critical, false, sizeof(is_critical));
    cnt = num = sum = 0;
}



int main()
{
    int t, u, v;
    while (cin >> t && t)
    {
        Init();
        while (cin >> u && u)
        {
            while (1)
            {
                char c = getchar();
                if (c == '\n')
                {
                    break;
                }
                cin >> v;
                InsertEdge(u, v);
                InsertEdge(v, u);
            }
        }
        for (int i = 1; i <= t; i++)
        {
            if (!dfn[i])
            {
                Tarjan(i, i);
            }
        }
        for (int i = 1; i <= t; i++)
        {
            if (is_critical[i])
            {
                sum++;
            }
        }
        cout << sum << endl;
    }
    

    return 0;
}

   

标签:places,int,POJ1144,割点,无向,place,critical,each
来源: https://blog.csdn.net/qq_48988844/article/details/120412741