Codeforces1437 D. Minimal Height Tree(BFS序)

2024-04-15 23:38

本文主要是介绍Codeforces1437 D. Minimal Height Tree(BFS序),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题意:
给出一颗树的BFS遍历结果,从结点1开始,每个结点叶子按编号从小到大遍历。求出可能树的最小高度。

思路:
为了让这棵树高度尽可能小,我们要使得每个结点的儿子尽可能多。所以直接遍历a数组,如果当前结点编号大于上一个,那就将此结点作为当前父亲节点儿子,否则就作为一个新的根节点。用队列维护待选父亲节点。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include <cstring>
#include<queue>
using namespace std;typedef long long ll;
const int maxn = 2e5 + 7;int a[maxn];
vector<int>G[maxn];int dfs(int x) {if(G[x].size() == 0) return 1;int mx = 0;for(int i = 0;i < G[x].size();i++) {int v = G[x][i];mx = max(dfs(v) + 1,mx);}return mx;
}int main() {int T;scanf("%d",&T);while(T--) {int n;scanf("%d",&n);for(int i = 1;i <= n;i++) {scanf("%d",&a[i]);G[i].clear();}queue<int>q; //当前根节点int pre = 1,rt = 1;for(int i = 2;i <= n;i++) {if(a[i] > pre) {q.push(a[i]);G[rt].push_back(a[i]);pre = a[i];} else {rt = q.front();q.pop();q.push(a[i]);G[rt].push_back(a[i]);pre = a[i];}}printf("%d\n",dfs(1) - 1);}return 0;
}

这篇关于Codeforces1437 D. Minimal Height Tree(BFS序)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907241

相关文章

【服务器运维】CentOS6 minimal 离线安装MySQL5.7

1.准备安装包(版本因人而异,所以下面的命令中版本省略,实际操作中用Tab自动补全就好了) cloog-ppl-0.15.7-1.2.el6.x86_64.rpmcpp-4.4.7-23.el6.x86_64.rpmgcc-4.4.7-23.el6.x86_64.rpmgcc-c++-4.4.7-23.el6.x86_64.rpmglibc-2.12-1.212.el6.x86_64.r

【服务器运维】CentOS7 minimal 离线安装 gcc perl vmware-tools

0. 本机在有网的情况下,下载CentOS镜像 https://www.centos.org/download/ 1. 取出rpm 有的情况可能不需要net-tools,但是如果出现跟ifconfig相关的错误,就把它安装上。另外如果不想升级内核版本的话,就找对应内核版本的rpm版本安装 perl-Time-Local-1.2300-2.el7.noarch.rpmperl-Tim

多源 BFS

例题一 解法(bfs)(多个源头的最短路问题) 算法思路: 对于求的最终结果,我们有两种⽅式: • 第⼀种⽅式:从每⼀个 1 开始,然后通过层序遍历找到离它最近的 0 。 这⼀种⽅式,我们会以所有的 1 起点,来⼀次层序遍历,势必会遍历到很多重复的点。并且如果 矩阵中只有⼀个 0 的话,每⼀次层序遍历都要遍历很多层,时间复

玩转Web之easyui(二)-----easy ui 异步加载生成树节点(Tree),点击树生成tab(选项卡)

关于easy ui 异步加载生成树及点击树生成选项卡,这里直接给出代码,重点部分代码中均有注释 前台: $('#tree').tree({ url: '../servlet/School_Tree?id=-1', //向后台传送id,获取根节点lines:true,onBeforeExpand:function(node,param){ $('#tree').tree('options'

BFS:解决多源最短路问题

文章目录 什么是多源最短路问题?1.矩阵2.飞地的数量3.地图的最高点4.地图分析总结 什么是多源最短路问题? 多源最短路问题(Multi-Source Shortest Path Problem,MSSP)是图论中的一个经典问题,它的目标是在给定图中找到从多个源点到所有其他顶点的最短路径。这个问题可以视为单源最短路问题(Single-Source Shortest Pa

【C++PCL】点云处理Kd-tree原理

作者:迅卓科技 简介:本人从事过多项点云项目,并且负责的项目均已得到好评! 公众号:迅卓科技,一个可以让您可以学习点云的好地方 重点:每个模块都有参数如何调试的讲解,即调试某个参数对结果的影响是什么,大家有问题可以评论哈,如果文章有错误的地方,欢迎来指出错误的地方。 目录         1.原理介绍 1.原理介绍         kd-tree是散乱点云的一种储存结构,它是一种

[leetcode] 107. Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II 描述 Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example

[leetcode] 102. Binary Tree Level Order Traversal

Binary Tree Level Order Traversal 描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20

[leetcode] 515. Find Largest Value in Each Tree Row

Find Largest Value in Each Tree Row 描述 You need to find the largest value in each row of a binary tree. Example: Input: 1/ \3 2/ \ \ 5 3 9 Output: [1, 3, 9] 我的代码 简单的dfs。 要使

[leetcode] 257. Binary Tree Paths

* Binary Tree Paths* 描述 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: [“1->2->5”, “1->3”] 我的代码