-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree.cpp
109 lines (96 loc) · 2.12 KB
/
tree.cpp
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
100
101
102
103
104
105
106
107
108
109
/*
In the name of ALLAH
Author : Raashid Anwar
*/
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;
mt19937 rng((uint64_t)chrono::steady_clock::now().time_since_epoch().count());
class tree {
vector<vector<int>> node;
vector<vector<int>> pi;
vector <int> l;
int n;
public :
tree(int _n) : n(_n) {
node.resize(n);
l.resize(n, 0);
pi.resize(n, vector<int> (21, 0));
}
void add(int u, int v) {
node[u].push_back(v);
node[v].push_back(u);
}
void dfs(int u) {
for (int v : node[u])
if (v != pi[u][0]) {
l[v] = l[u] + 1;
pi[v][0] = u;
dfs(v);
}
}
void dfs() {
dfs(0);
for (int i = 1; i <= 20; i++)
for (int u = 0; u < n; u++)
pi[u][i] = pi[pi[u][i - 1]][i - 1];
}
void bfs() {
queue <int> q;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : node[u])
if (v != pi[u][0]) {
l[v] = l[u] + 1;
pi[v][0] = u;
q.push(v);
}
}
for (int i = 1; i <= 20; i++)
for (int u = 0; u < n; u++)
pi[u][i] = pi[pi[u][i - 1]][i - 1];
}
int get_lca(int u, int v) {
if (l[v] > l[u])
swap(u, v);
for (int i = 20; ~i; --i)
if (l[u] - l[v] >= (1 << i))
u = pi[u][i];
if (u == v)
return u;
for (int i = 20; ~i; --i)
if (pi[u][i] != pi[v][i]) {
u = pi[u][i];
v = pi[v][i];
}
return pi[u][0];
}
int get_diameter(int u, int& d) {
int x = 0;
for (int v : node[u])
if (v != pi[u][0]) {
pi[v][0] = u;
l[v] = l[u] + 1;
int y = get_diameter(v, d);
d = max(d, x + y + 1);
x = max(x, y);
}
return x + 1;
}
int diameter() {
int d = 0;
get_diameter(0, d);
return d;
}
};
void solve() {
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}