PAT Advanced 1021. Deepest Root (25) (C语言实现)

该博客主要介绍PAT考试中的一道题目,涉及寻找一棵连通且无环图(树)中使得树高度最大的根节点,即最深根。通过DFS算法来解决此问题,如果图不构成树,即非连通,博主提供了解决连通组件数量的方法。并给出了代码实现。
摘要由CSDN通过智能技术生成

我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到Github Pages浏览最新内容(本篇文章链接)。此处文章目前已更新至与Github Pages同步。欢迎star我的repo

题目

A graph which is connected and acyclic can be considered a tree. The height of
the tree depends on the selected root. Now you are supposed to find the root
that results in a highest tree. Such a root is called the deepest root.

Input Specification:

Each input file contains one test case. For each case, the first line contains
a positive integer N N N ( ≤ 1 0 4 \le 10^4 104 ) which is the number of nodes, and hence
the nodes are numbered from 1 to N N N . Then N − 1 N-1 N1 lines follow, each describes
an edge by given the two adjacent nodes’ numbers.

Output Specification:

For each test case, print each of the deepest roots in a line. If such a root
is not unique, print them in increasing order of their numbers. In case that
the given graph is not a tree, print Error: K components where K is the
number of connected components in the graph.

Sample Input 1:

5
1 2
1 3
1 4
2 5

Sample Output 1:

3
4
5

Sample Input 2:

5
1 3
1 4
2 5
3 4

Sample Output 2:

Error: 2 components

思路

这道题主要做的事情就是找到构成的树达到最大深度的根节点,使用DFS即可。
题目还要求所给的图不是树的话,也就是说这个图不是连通的,
这时候需要知道到底有几个部分,可以对剩下DFS未遍历到的结点继续DFS知道全部遍历,
过程中记数即可。

代码

最新代码@github,欢迎交流

#include <stdio.h>

typedef struct node{
    int visited, level, depth;
    struct adj *adj;
} node;

typedef struct adj{
    struct node *node;
    struct adj *next;
} adj;

void DFS(node *n, int level)
{
    n->visited = 1;
    n->level = level + 1;

    for(adj *a = n->adj; a; a = a->next)
        if(a->node->visited == 0)
            DFS(a->node, level + 1);
}

int main()
{
    int N, n1, n2, count, depth, maxdepth = 0;
    node nodes[10000] = {0}, *pnode;
    adj edges[20000] = {0}, *padj;

    /* Read and build the adjacent linked list */
    scanf("%d", &N);
    for(int i = 0; i < N - 1; i++)
    {
        scanf("%d %d", &n1, &n2);
        /* n1 to n2 */
        pnode = &nodes[n1 - 1];
        padj = &edges[i * 2];
        padj->node = &nodes[n2 - 1];
        padj->next = pnode->adj;
        pnode->adj = padj;
        /* n2 to n1 */
        pnode = &nodes[n2 - 1];
        padj = &edges[i * 2 + 1];
        padj->node = &nodes[n1 - 1];
        padj->next = pnode->adj;
        pnode->adj = padj;
    }

    for(int i = 0; i < N; i++)
    {
        /* Reset the whole graph */
        depth = 0;
        count = 1;
        for(int i = 0; i < N; i++)
            nodes[i].visited = nodes[i].level = 0;

        /* Start from the ith node */
        DFS(nodes + i, 0);

        /* Get the depth of the tree */
        for(int i = 0; i < N; i++)
            if(nodes[i].visited == 1)
                if(nodes[i].level > depth)
                    depth = nodes[i].level;

        /* Try to find other disjoint components */
        for(int i = 0; i < N; i++)
            if(nodes[i].visited == 0)
            {
                DFS(nodes + i, 0);
                count ++;
            }

        if(count != 1)  /* If not all the nodes are visited */
        {
            printf("Error: %d components", count);
            return 0;  /* Only have to do it once */
        }
        else            /* It is one tree */
        {
            nodes[i].depth = depth;
            if(maxdepth < depth)
                maxdepth = depth;
        }
    }

    /* Find the root with the same maximum depth */
    for(int i = 0; i < N; i++)
        if(nodes[i].depth == maxdepth)
            printf("%d\n", i + 1);

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值