PAT(甲)1021 Deepest Root (25)(详解)

1021 Deepest Root (25)(25 分)

题目描述:

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.


  • 输入格式
    Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N-1 lines follow, each describes an edge by given the two adjacent nodes’ numbers.

  • 输出格式
    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.


题目大意:
题目就是要对一个树形结构,去寻找能够以某个结点以其为根,深度最大,如果有多个,则升序输出。解题思路参考自这里

解题方法:
采用dfs对图进行遍历,因为如果是连通图,dfs就能把所有结点都遍历完,而如果不是连通图,则需要至少2次以上,因此根据这个特性可以用来判断是否是树。同时,通过dfs,可以将最深的叶子结点找出来。这里需要注意一点,类似于(1-2-3)这样的树形结构,1和3都可以被称为root结点,因此,我们可以通过对第一遍dfs得到遍历结果中,随便取一个,反向dfs,最终把结果全部放入set中,把两次dfs的并集放入。


程序:

#include <stdio.h>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

int maxHight = 0;
set <int> s;    /* 保存深度最大的根结点集合(默认升序) */
vector <vector<int> > v;    /* 保存点与点之间的联系 */
vector <int> temp;  /* 保存深度最大点的向量 */
bool visit[10001];

void dfs(int node, int height)
{   /* 深度优先搜索遍历 */
    if (height > maxHight)  /* 如果当前结点的深度比最大深度还要大 */
    {
        maxHight = height;
        temp.clear();
        temp.push_back(node);
    }
    else if (height == maxHight)  /* 如果相同,将相同深度的结点放入temp */
        temp.push_back(node);
    visit[node] = true;     /* 置该点为已访问 */
    for (int i = 0; i < v[node].size(); i++)  /* 遍历v[node]的所有孩子 */
        if (visit[v[node][i]] == false)
            dfs(v[node][i], height+1);
}

int main(int argc, char const *argv[])
{
    int N, father, child, cnt = 0;
    scanf("%d", &N);
    fill(visit, visit+N+1, false);    /* 初始化visit */
    v.resize(N+1);  /* 调整vector的大小 */
    for (int i = 0; i < N-1; i++) /* 输入父节点和孩子节点之间的联系 */
    {   
        scanf("%d %d", &father, &child);
        v[father].push_back(child);
        v[child].push_back(father);
    }
    for (int i = 1; i <= N; i++)    /* 编号从1开始 */
        if (visit[i] == false)  /* 如果还未访问过 */
        {
            dfs(i, 1);
            if (i == 1)
                for (int j = 0; j < temp.size(); j++)   /* 把当前的叶子结点存入 */
                    s.insert(temp[j]);
            cnt++;  /* 每执行一次cnt++,用于判断是否是树 */
        }
    if (cnt >= 2)   /* 如果不是树 */
        printf("Error: %d components\n", cnt);
    else    /* 从叶子结点出发,去寻找最深的根结点 */
    {
        fill(visit, visit+N+1, false);    /* 重置visit */
        temp.clear();
        maxHight = 0;   
        dfs(temp[0], 1);    /* 因为树是连通的,所以从任意一个叶节点出发都能遍历到所有最深的父节点 */
        for (int j = 0; j < temp.size(); j++)
            s.insert(temp[j]);
        set <int>::iterator it;
        for (it = s.begin(); it != s.end(); it++)
            printf("%d\n", *it);
    }
    return 0;
}

如果对您有帮助,帮忙点个小拇指呗~

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值