4310. 树的DFS 邻接链表深搜

news/2025/1/13 2:42:38/

文章目录

          • [4310. 树的DFS](https://www.acwing.com/problem/content/4313/)

4310. 树的DFS

给定一棵 n 个节点的树。

节点的编号为 1∼n,其中 1 号节点为根节点,每个节点的编号都大于其父节点的编号。

现在,你需要回答 q 个询问。

每个询问给定两个整数 ui,ki

我们希望你用 DFS(深度优先搜索)算法来遍历根节点为 ui 的子树。

我们规定,当遍历(或回溯)到某一节点时,下一个遍历的目标应该是它的未经遍历的子节点中编号最小的那一个子节点。

wew

例如,上图实例中:

  • 如果遍历根节点为 1 号节点的子树,则子树内各节点的遍历顺序为 1,2,3,5,6,8,7,9,4。
  • 如果遍历根节点为 3 号节点的子树,则子树内各节点的遍历顺序为 3,5,6,8,7,9。
  • 如果遍历根节点为 7 号节点的子树,则子树内各节点的遍历顺序为 7,9。
  • 如果遍历根节点为 9 号节点的子树,则子树内各节点的遍历顺序为 9。

每个询问就是让你计算采用规定的 DFS 算法来遍历根节点为 ui 的子树时,第 ki 个被遍历到的节点的编号。

输入格式

第一行包含两个整数 n,q。

第二行包含 n−1 个整数 p2,p3,…,pn,其中 pi 表示第 i 号节点的父节点的编号。

接下来 q 行,每行包含两个整数 ui,ki,表示一组询问。

输出格式

共 q 行,每组询问输出一行一个整数表示第 ki 个被遍历到的节点的编号。

如果第 ki 个被遍历到的节点不存在,则输出 −1。

数据范围

前三个测试点满足 2≤n≤20,1≤q≤20。
所有测试点满足 2≤n≤2×105,1≤q≤2×105,1≤pi<i,1≤ui,ki≤n。

输入样例

9 6
1 1 1 3 5 3 5 7
3 1
1 5
3 4
7 3
1 8
1 9

输出样例

3
6
8
-1
9
4

思路

要用vector<int> h[N]写邻接链表,不要用h[N], e[N], ne[N]数组写,因为需要从小到大遍历点,所以用vector写可以方便排序

代码

#include<iostream>
#include<vector>
#include <algorithm>using namespace std;const int N = 2e5 + 10;
int m, n, k, t;
int res;
vector<int> h[N];
vector<int> p;
bool st[N];
int a[N], cnt[N];int dfs(int x) {p.push_back(x);cnt[x] = 1;for (int i: h[x])cnt[x] += dfs(i);return cnt[x];
}int main() {cin >> n >> m;for (int i = 2; i <= n; ++i) {cin >> t;h[t].push_back(i);}for (int i = 1; i <= n; ++i) sort(h[i].begin(), h[i].end());dfs(1);for (int i = 0; i < p.size(); ++i) a[p[i]] = i;while (m--) {cin >> t >> k;if (cnt[t] < k) cout << -1 << endl;else cout << p[a[t] + k - 1] << endl;}return 0;
}

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ssssssssss99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ssssssssss99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ssssssssss99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ssssssssss99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ssssssssss99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ssssssssss99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999


http://www.ppmy.cn/news/488804.html

相关文章

TPLink4310刷机

目录说明&#xff1a; MW4530R (水星4530R原厂固件、Firmware&#xff0c;uboot) TPLink4300 (TP 4310原厂固件、Firmware&#xff0c;uboot) TPLink4310 (TP 4310原厂固件、Firmware&#xff0c;uboot) OpenWRT(OP固件&#xff0c;均不含uboot&#xff0c;供4530,4310,4300原厂…

ubuntu设置开机启动命令

文章目录 概述系统版本设置开机启动命令1. 查看rc-local服务状态2. 设置rc-local服务开机启动3. 手动创建系统自启动服务4. 创建rc.local文件5. 设置rc.local文件权限6.添加开机启动命令7.启用rc-local服务8.查看rc-local服务状态9.重启系统 概述 本文档主要记录Ubuntu系统使用…

【带你刷《剑指Offer》系列】【每天40分钟,跟我一起用50天刷完 (剑指Offer)】第一天

专注 效率 记忆 预习 笔记 复习 做题 欢迎观看我的博客&#xff0c;如有问题交流&#xff0c;欢迎评论区留言&#xff0c;一定尽快回复&#xff01;&#xff08;大家可以去看我的专栏&#xff0c;是所有文章的目录&#xff09;   文章字体风格&#xff1a; 红色文字表示&#…

Matthew Ball:十多年后AR/VR为何依然发展缓慢?

2010年&#xff0c;Magic Leap和微软就开始研发AR技术&#xff0c;直到2012年Oculus才成立&#xff0c;AR/VR经过了13年左右的时间&#xff0c;虽然受到越来越多人关注&#xff0c;但发展依然缓慢。VR的主要应用场景还是游戏&#xff0c;但VR游戏只是游戏市场的一个分支&#x…

java、jvm与.net

现在sun已经被oracle收购了&#xff01;也从侧面验证了作者的一些论断&#xff01; 当然从技术上看&#xff0c;这篇文章也是分析很精辟。反正我自己感觉c很好用&#xff0c;java以及所谓的OO更多是一些概念。 Java在自取灭亡 一个比较Java语言发展的讨论贴&#xff0c;非常…

五个字的英语单词

abide v.(by)坚持,遵守 about ad. 在周围,附近,到处;大约,差不多 prep. 关于,对于;在……周围,在……附近 a.准备 above prep. 在……上面,超过,高于a. 上面的,上述的ad. 在上面,以上 abuse v./n. 滥用;虐待;谩骂 actor n. 男演员 acute a. 敏锐的,尖锐的;(疾病)急性的 adapt v…

如果编程语言是妹纸

试想一下&#xff0c;当Java、C、Python、Ruby、PHP、C#、JS等编程语言变成了动漫人物会是怎样的一幅场景呢&#xff1f;下面就一起看看在日本作家渡辺将人的笔下&#xff0c;各种编程语言都是哪类“美女”的吧&#xff01; Java 犹如宫泽贤治的《不畏风雨》中出现的、性格木讷…

Allegro如何设置默认器件的高度信息操作指导

Allegro如何设置默认器件的高度信息操作指导 在给PCB设置限高的时候,一般会添加一个package keepout的铜皮,如下图 如果器件有高度信息,且没有超过限高要求,是不会有DRC报错的,如果器件没有高度信息,软件会默认给匹配一个高度信息,从而导致误报,如下图 可以看到默认的高…