-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryLiftingTree.cpp
More file actions
79 lines (74 loc) · 1.69 KB
/
Copy pathBinaryLiftingTree.cpp
File metadata and controls
79 lines (74 loc) · 1.69 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
/**
* Author: Kevin Li
* Lang: C++
* Description: Tree with method of binary jumping
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
struct binaryjumptree {
int n;
vector<int> *e;
int *p;
int k;
int **jump;
binaryjumptree () {}
binaryjumptree (int _n) : n(_n) {
e = new vector<int>[n];
p = new int[n];
k = ceil(log((double)n)/log(2.0));
jump = new int*[n];
for (int i = 0; i < n; i++) {
jump[i] = new int[k+1];
}
}
void add(int u, int v) {
e[u].pb(v);
e[v].pb(u);
}
void pdfs(int node) {
for (int i = 0; i < e[node].size(); i++) {
if (e[node][i] != p[node]) {
p[e[node][i]] = node;
pdfs(e[node][i]);
}
}
}
void driver(int root) {
p[root] = root; // i think you could also set to -1 if needed
pdfs(root);
for (int i = 0; i < n; i++) {
jump[i][0] = p[i];
}
for (int i = 1; i <= k; i++) {
for (int j = 0; j < n; j++) {
jump[j][i] = jump[jump[j][i-1]][i-1];
}
}
}
void print() {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++) {
cout << jump[i][j] << " ";
}
cout << endl;
}
}
};
int n, root;
int main() {
cin >> n >> root;
binaryjumptree BJT = binaryjumptree(n);
for (int i = 0; i < n-1; i++) {
int u,v; cin >> u >> v;
BJT.add(u,v);
}
BJT.driver(root);
BJT.print();
}