problemscpp
A collection of my answers to algorithm problems in c++.
Public 成员函数 | Private 属性 | 所有成员列表
acwing::acwing4310::acwing4310类 参考

#include <acwing.h>

Public 成员函数

int dfs (vector< int > *vec, TreeNode *node)
 
int main (istream &cin, ostream &cout)
 

Private 属性

unordered_map< int, int > position
 
unordered_map< int, int > size_tree
 
unordered_map< int, TreeNode * > um
 

详细描述

在文件 acwing.h1576 行定义.

成员函数说明

◆ dfs()

int acwing::acwing4310::acwing4310::dfs ( vector< int > *  vec,
TreeNode node 
)

在文件 acwing.cpp5001 行定义.

5001 {
5002 int sum = 1;
5003 vec->push_back(node->val);
5004 for(auto [i, next]: node->nexts) {
5005 if(next != nullptr) {
5006 sum += dfs(vec, next);
5007 }
5008 }
5009 size_tree[node->val] = sum;
5010 return sum;
5011 }
int vec[100010]
Definition: pat.cpp:5095
int dfs(vector< int > *vec, TreeNode *node)
Definition: acwing.cpp:5001
unordered_map< int, int > size_tree
Definition: acwing.h:1578

◆ main()

int acwing::acwing4310::acwing4310::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing.cpp4967 行定义.

4967 {
4968 int n;
4969 int q;
4970 cin >> n >> q;
4971 vector<int> vec;
4972 for(int i = 1; i <= n; i++) {
4973 um[i] = new TreeNode(i);
4974 }
4975 for(int i = 2; i <= n; i++) {
4976 int parent;
4977 cin >> parent;
4978 um[parent]->nexts[i] = um[i];
4979 }
4980 dfs(&vec, um[1]);
4981 for(int i = 0; i < vec.size(); i++) {
4982 position[vec[i]] = i;
4983 }
4984 for(int i = 0; i < q; i++) {
4985 int u;
4986 int k;
4987 cin >> u >> k;
4988 if(k > size_tree[u]) {
4989 cout << -1;
4990 } else {
4991 cout << vec[position[u] + k - 1];
4992 }
4993 cout << endl;
4994 }
4995 for(auto [i, v]: um) {
4996 delete v;
4997 }
4998 return 0;
4999 }
unordered_map< int, int > position
Definition: acwing.h:1579
unordered_map< int, TreeNode * > um
Definition: acwing.h:1577

类成员变量说明

◆ position

unordered_map<int, int> acwing::acwing4310::acwing4310::position
private

在文件 acwing.h1579 行定义.

◆ size_tree

unordered_map<int, int> acwing::acwing4310::acwing4310::size_tree
private

在文件 acwing.h1578 行定义.

◆ um

unordered_map<int, TreeNode *> acwing::acwing4310::acwing4310::um
private

在文件 acwing.h1577 行定义.


该类的文档由以下文件生成: