首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

PAT甲级-1021 Deepest Root (25分)

  • 24-03-03 00:43
  • 3251
  • 10142
blog.csdn.net

点击链接PAT甲级-AC全解汇总

题目:
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 (≤10​4​​ ) 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.

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
  • 1
  • 2
  • 3
  • 4
  • 5

Sample Output 1:

3
4
5
  • 1
  • 2
  • 3

Sample Input 2:

5
1 3
1 4
2 5
3 4
  • 1
  • 2
  • 3
  • 4
  • 5

Sample Output 2:

Error: 2 components
  • 1

题意:
输入的是一棵树,找树的根使得深度最深
类似于图的最大生成树,但是因为输入的就是树,标记visited比较方便
思路:
第一次dfs,看有几个连通分量,同时找到最深的叶子,这些叶子肯定是可以做最深根的;
第二次dfs,从上一次dfs找到的最深叶子作为根,再找到的最深的叶子是刚才漏的;(因为第一次的起始点可能比较靠近它,所以才会漏,需要第二次dfs找回来);
两次最深根求并集就行了;

我的代码:

#include
using namespace std;
bool node[10010][10010]={false};
bool visited[10010]={false};
int N,deepest=0;

void DFS(int root,int root_depth,set<int>& candidate)
{
    visited[root]=true;
    if(root_depth>deepest)
    {
        deepest=root_depth;
        candidate.clear();
        candidate.insert(root);
    }
    else if(root_depth==deepest)
        candidate.insert(root);

    for(int next=1;next<=N;next++)
    {
        if(node[root][next]&&!visited[next])
            DFS(next,root_depth+1,candidate);
    }
}

int main()
{
    cin>>N;
    for(int i=0;i<N-1;i++)
    {
        int a,b;
        cin>>a>>b;
        node[a][b]=true;
        node[b][a]=true;
    }

    //第一次DFS找到最大根候选集1,同时算有几个连通分量
    int component=0;
    set<int>candidate1;
    for(int i=1;i<=N;i++)
    {
        if(visited[i])continue;
        component++;
        DFS(i,1,candidate1);
    }

    if(component>1)
    {
        printf("Error: %d components",component);
        return 0;
    }

    //第二次DFS找到最大根候选集2
    fill(visited,visited+10010,false);
    deepest=0;
    set<int>candidate2;
    DFS(*candidate1.begin(),1,candidate2);

    //两个候选集求并集
    set<int>result;
	set_union(candidate1.begin(), candidate1.end(),
              candidate2.begin(), candidate2.end(),
              inserter(result, result.begin()));

    for(set<int>::iterator it=result.begin();it!=result.end();it++)
    {
        cout<<*it<<endl;
    }
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
注:本文转载自blog.csdn.net的邂逅模拟卷的文章"https://blog.csdn.net/qq_34451909/article/details/105324261"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top