-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_1483_TreeAncestor.cc
More file actions
99 lines (84 loc) · 1.72 KB
/
Problem_1483_TreeAncestor.cc
File metadata and controls
99 lines (84 loc) · 1.72 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <vector>
using namespace std;
// TODO: figure it out.
class TreeAncestor
{
public:
static const int MAXN = 50001;
static const int LIMIT = 16;
// 根据节点个数n,计算出2的几次方就够用了
int power;
int log2(int n)
{
int ans = 0;
while ((1 << ans) <= (n >> 1))
{
ans++;
}
return ans;
}
// 链式前向星建图
vector<int> head = vector<int>(MAXN);
vector<int> next = vector<int>(MAXN);
vector<int> to = vector<int>(MAXN);
int cnt;
// deep[i] : 节点i在第几层
vector<int> deep = vector<int>(MAXN);
// stjump[i][p] : 节点i往上跳2的p次方步,到达的节点编号
vector<vector<int>> stjump = vector<vector<int>>(MAXN, vector<int>(LIMIT));
TreeAncestor(int n, vector<int>& parent)
{
power = log2(n);
cnt = 1;
std::fill(head.begin(), head.begin() + n, 0);
for (int i = 1; i < parent.size(); i++)
{
addEdge(parent[i], i);
}
dfs(0, 0);
}
void addEdge(int u, int v)
{
next[cnt] = head[u];
to[cnt] = v;
head[u] = cnt++;
}
// 当前来到i节点,i节点父亲节点是f
void dfs(int i, int f)
{
if (i == 0)
{
deep[i] = 1;
}
else
{
deep[i] = deep[f] + 1;
}
stjump[i][0] = f;
for (int p = 1; p <= power; p++)
{
stjump[i][p] = stjump[stjump[i][p - 1]][p - 1];
}
for (int e = head[i]; e != 0; e = next[e])
{
dfs(to[e], i);
}
}
int getKthAncestor(int i, int k)
{
if (deep[i] <= k)
{
return -1;
}
// s是想要去往的层数
int s = deep[i] - k;
for (int p = power; p >= 0; p--)
{
if (deep[stjump[i][p]] >= s)
{
i = stjump[i][p];
}
}
return i;
}
};