首页 > 其他分享 >PAT Advanced 1021 Deepest Root(25)

PAT Advanced 1021 Deepest Root(25)

时间:2022-08-20 22:22:42浏览次数:72  
标签:25 1021 cur int max len st Deepest root

题目描述:

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 (≤104) 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

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

题目大意:

给出n个结点,和n-1条边
若是树,则输出能构成最大高度的所有首尾结点;否则输出连通块的数量

#include<iostream>
#include<cstring>
#include<vector>
#include<set>
using namespace std;

const int N = 10010;
set<int> ans;
vector<int> v[N];
vector<int> max_point;
bool st[N]; 
int max_len = 0;
// 遍历所有结点  求连通块数量
void dfs(int cur)
{
    if(st[cur])  return;
    st[cur] = 1;
    for(int i : v[cur])
        dfs(i);
}
// 找最长路径  
void _dfs(int cur, int len)
{
    if(st[cur])    return;
    st[cur] = 1;
    if(len > max_len)
    {
        max_len = len;
        max_point.clear();
        max_point.push_back(cur);
    }
    else if(len == max_len) max_point.push_back(cur);
    for(int i : v[cur])
    {
        _dfs(i, len + 1);
    }
}

int main()
{
    int n;
    cin >> n;
    for(int i = 1 ; i < n ; i ++)
    {
        int j, k;
        cin >> j >> k;
        v[j].push_back(k);
        v[k].push_back(j);
    }
    
    int cnt = 0;
    for(int i = 1 ; i <= n ; i ++)
        if(!st[i])
        {
            cnt ++;
            dfs(i);
        }
    // 连通块cnt为1时是树
    if(cnt != 1) 
    {
        printf("Error: %d components", cnt);
    }
    else
    { //先由根结点出发找到离根结点最远的结点(不一定唯一)并放入set
        memset(st, 0, sizeof st);
        _dfs(1, 0);
        for(int x : max_point)  ans.insert(x);
        // 再由离根最远的结点(任一)出发找最远的结点(不一定唯一)并放入set
        max_point.clear();
        max_len = 0;
        memset(st, 0, sizeof st);
        _dfs(*ans.begin(), 0);
        for(int x : max_point)  ans.insert(x);
        
        for(int x : ans)    cout << x << endl;
    }
    
    return 0;
}

标签:25,1021,cur,int,max,len,st,Deepest,root
From: https://www.cnblogs.com/yztozju/p/16608929.html

相关文章

  • 25. Redis---性能测试
    1.前言为了解Redis在不同配置环境下的性能表现,Redis提供了一种行性能测试工具redis-benchmark(也称压力测试工具),它通过同时执行多组命令实现对Redis的性能测试。性......
  • P2508-[HAOI2008]圆上的整点【数学】
    正题题目链接:https://www.luogu.com.cn/problem/P2508题目大意一个在\((0,0)\)的圆心,半径为\(r\),求圆有多少个整点。\(1\leqr\leq2\times10^9\)解题思路设这个......
  • 爬虫-获取豆瓣Top250信息
    importtimeimportrequestsfromlxmlimportetreei=0foriteminrange(0,275,25):url=f'https://movie.douban.com/top250?start={item}&filter='......
  • leetcode 225. Implement Stack using Queues 用队列实现栈(简单)
    一、题目大意请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop和empty)。实现MyStack类:voidpush(intx)将元素x压入栈顶。......
  • 1021 ObstacleCourse障碍训练课 优先队列+bfs+转弯
    链接:https://ac.nowcoder.com/acm/contest/26077/1021来源:牛客网题目描述考虑一个NxN(1<=N<=100)的有1个个方格组成的正方形牧场。......
  • [Oracle] LeetCode 253 Meeting Rooms II
    Givenanarrayofmeetingtimeintervalsintervalswhereintervals[i]=[starti,endi],returntheminimumnumberofconferenceroomsrequired.Solution先按照......
  • PAT Advanced 1020 Tree Traversals(25)
    题目描述:Supposethatallthekeysinabinarytreearedistinctpositiveintegers.Giventhepostorderandinordertraversalsequences,youaresupposedtoou......
  • CF1625E1 Cats on the Upgrade (easy version)
    刚刚学完whk时无聊看了下提交记录,发现这道富有启发意义的题目。首先,注意到这实际上就是个序列的《括号树》,拿来做就行,\(f_i\)为以\(i\)结尾的合法括号串数量,\(f_i=f......
  • 名校 AI 课程|斯坦福 CS25:Transformers United 专题讲座
    自2017年提出后,Transformer名声大噪,不仅颠覆了自然语言处理(NLP)领域,而且在计算机视觉(CV)、强化学习(RL)、生成对抗网络(GANs)、语音甚至是生物学等领域也大显锋芒,于是就有了近......
  • 手机网页限制用户缩放代码 (2014-03-25 18:16:52)
    网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放。    width-viewport的宽度height-viewport的高度  initi......