-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathshortest-path-in-a-weighted-tree.cpp
137 lines (127 loc) · 3.84 KB
/
shortest-path-in-a-weighted-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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Time: O(nlogn)
// Space: O(n)
class BIT {
public:
BIT(int n) : bit_(n + 1) { // 0-indexed
}
void add(int i, int val) {
++i;
for (; i < size(bit_); i += lower_bit(i)) {
bit_[i] += val;
}
}
int query(int i) const {
++i;
int total = 0;
for (; i > 0; i -= lower_bit(i)) {
total += bit_[i];
}
return total;
}
private:
int lower_bit(int i) const {
return i & -i;
}
vector<int> bit_;
};
// iterative dfs, fenwick tree
class Solution {
public:
vector<int> treeQueries(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {
vector<vector<pair<int, int>>> adj(n);
for (const auto& e : edges) {
const int u = e[0] - 1, v = e[1] - 1, w = e[2];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
const auto& iter_dfs = [&]() {
vector<int> L(n), R(n), dist(n), lookup(n);
int cnt = 0;
vector<tuple<int, int, int, int>> stk = {{1, 0, -1, 0}};
while (!empty(stk)) {
const auto [step, u, p, d] = stk.back(); stk.pop_back();
if (step == 1) {
L[u] = cnt++;
dist[u] = d;
stk.emplace_back(2, u, -1, -1);
for (const auto& [v, w] : adj[u]) {
if (v == p) {
continue;
}
lookup[v] = w;
stk.emplace_back(1, v, u, d + w);
}
} else if (step == 2) {
R[u] = cnt;
}
}
return tuple(L, R, dist, lookup);
};
auto [L, R, dist, lookup] = iter_dfs();
BIT bit(n);
vector<int> result;
for (const auto& q : queries) {
if (q[0] == 1) {
int u = q[1] - 1, v = q[2] - 1, w = q[3];
if (L[u] > L[v]) {
swap(u, v);
}
const int diff = w - lookup[v];
bit.add(L[v], diff);
bit.add(R[v], -diff);
lookup[v] = w;
} else {
const int x = q[1] - 1;
result.emplace_back(dist[x] + bit.query(L[x]));
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
// dfs, fenwick tree
class Solution2 {
public:
vector<int> treeQueries(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {
vector<vector<pair<int, int>>> adj(n);
for (const auto& e : edges) {
const int u = e[0] - 1, v = e[1] - 1, w = e[2];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
vector<int> L(n), R(n), dist(n), lookup(n);
int cnt = 0;
const function<void (int, int, int)> dfs = [&](int u, int p, int d) {
L[u] = cnt++;
dist[u] = d;
for (const auto& [v, w] : adj[u]) {
if (v == p) {
continue;
}
lookup[v] = w;
dfs(v, u, d + w);
}
R[u] = cnt;
};
dfs(0, -1, 0);
BIT bit(n);
vector<int> result;
for (const auto& q : queries) {
if (q[0] == 1) {
int u = q[1] - 1, v = q[2] - 1, w = q[3];
if (L[u] > L[v]) {
swap(u, v);
}
const int diff = w - lookup[v];
bit.add(L[v], diff);
bit.add(R[v], -diff);
lookup[v] = w;
} else {
const int x = q[1] - 1;
result.emplace_back(dist[x] + bit.query(L[x]));
}
}
return result;
}
};