// IOI 2021
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ends ' '
#define endt '\t'
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
////////////////////////////////////////////////////////////////////
const int N = 1e5 + 5, LOG = 17;
int S[N], F[N], H[N], P[LOG][N], ts;
int DP[N][3], FEN[N];
vector<int> G[N];
int A[N], B[N], C[N];
vector<int> Q[N];
void Add(int p, int x) { for (p++; p < N; p += p & -p) FEN[p] += x; }
int Get(int p) {
int res = 0;
for (p++; p > 0; p -= p & -p) res += FEN[p];
return res;
}
void DFSH(int v, int p = 0) {
S[v] = ts++, P[0][v] = p;
for (int i = 1; i < LOG; i++) P[i][v] = P[i - 1][P[i - 1][v]];
for (int u : G[v]) if (u != p) H[u] = H[v] + 1, DFSH(u, v);
F[v] = ts;
}
int GetPar(int v, int h) {
for (int i = 0; i < LOG; i++) if ((h >> i) & 1) v = P[i][v];
return v;
}
int GetLCA(int v, int u) {
if (H[v] < H[u]) swap(v, u);
v = GetPar(v, H[v] - H[u]);
if (v == u) return v;
for (int i = LOG - 1; i >= 0; i--) if (P[i][v] != P[i][u])
v = P[i][v], u = P[i][u];
return P[0][v];
}
void DFS(int v, int p = 0) {
for (int u : G[v]) if (u != p) {
DFS(u, v);
DP[v][0] += DP[u][2];
}
for (int u : G[v]) if (u != p) {
Add(S[u], DP[v][0] - DP[u][2]);
Add(F[u], DP[u][2] - DP[v][0]);
}
for (int q : Q[v]) {
int a = A[q], b = B[q], c = C[q];
DP[v][1] = max(DP[v][1], c + Get(S[a]) + Get(S[b]) + DP[a][0] + DP[b][0] - DP[v][0]);
}
DP[v][2] = max(DP[v][0], DP[v][1]);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
mt19937 Rnd(time(0));
int n; cin >> n;
for (int i = 1; i < n; i++) {
int v, u; cin >> v >> u;
G[v].push_back(u);
G[u].push_back(v);
}
DFSH(1);
int m; cin >> m;
for (int i = 0; i < m; i++) {
cin >> A[i] >> B[i] >> C[i];
Q[GetLCA(A[i], B[i])].push_back(i);
}
DFS(1);
cout << DP[1][2] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5248 KB |
Output is correct |
5 |
Correct |
123 ms |
18912 KB |
Output is correct |
6 |
Correct |
62 ms |
28152 KB |
Output is correct |
7 |
Correct |
127 ms |
24900 KB |
Output is correct |
8 |
Correct |
97 ms |
19192 KB |
Output is correct |
9 |
Correct |
117 ms |
23032 KB |
Output is correct |
10 |
Correct |
92 ms |
19224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
128 ms |
31608 KB |
Output is correct |
5 |
Correct |
128 ms |
31736 KB |
Output is correct |
6 |
Correct |
124 ms |
31764 KB |
Output is correct |
7 |
Correct |
152 ms |
31736 KB |
Output is correct |
8 |
Correct |
117 ms |
31672 KB |
Output is correct |
9 |
Correct |
114 ms |
31736 KB |
Output is correct |
10 |
Correct |
132 ms |
31656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
128 ms |
31608 KB |
Output is correct |
5 |
Correct |
128 ms |
31736 KB |
Output is correct |
6 |
Correct |
124 ms |
31764 KB |
Output is correct |
7 |
Correct |
152 ms |
31736 KB |
Output is correct |
8 |
Correct |
117 ms |
31672 KB |
Output is correct |
9 |
Correct |
114 ms |
31736 KB |
Output is correct |
10 |
Correct |
132 ms |
31656 KB |
Output is correct |
11 |
Correct |
15 ms |
6144 KB |
Output is correct |
12 |
Correct |
120 ms |
32016 KB |
Output is correct |
13 |
Correct |
121 ms |
32052 KB |
Output is correct |
14 |
Correct |
115 ms |
31992 KB |
Output is correct |
15 |
Correct |
122 ms |
32016 KB |
Output is correct |
16 |
Correct |
127 ms |
32040 KB |
Output is correct |
17 |
Correct |
139 ms |
31992 KB |
Output is correct |
18 |
Correct |
118 ms |
31992 KB |
Output is correct |
19 |
Correct |
107 ms |
31992 KB |
Output is correct |
20 |
Correct |
117 ms |
31992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
173 ms |
21876 KB |
Output is correct |
2 |
Correct |
103 ms |
31736 KB |
Output is correct |
3 |
Correct |
243 ms |
28024 KB |
Output is correct |
4 |
Correct |
125 ms |
22132 KB |
Output is correct |
5 |
Correct |
203 ms |
27604 KB |
Output is correct |
6 |
Correct |
122 ms |
22392 KB |
Output is correct |
7 |
Correct |
241 ms |
27220 KB |
Output is correct |
8 |
Correct |
185 ms |
22384 KB |
Output is correct |
9 |
Correct |
109 ms |
31680 KB |
Output is correct |
10 |
Correct |
232 ms |
26088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5248 KB |
Output is correct |
5 |
Correct |
123 ms |
18912 KB |
Output is correct |
6 |
Correct |
62 ms |
28152 KB |
Output is correct |
7 |
Correct |
127 ms |
24900 KB |
Output is correct |
8 |
Correct |
97 ms |
19192 KB |
Output is correct |
9 |
Correct |
117 ms |
23032 KB |
Output is correct |
10 |
Correct |
92 ms |
19224 KB |
Output is correct |
11 |
Correct |
4 ms |
5376 KB |
Output is correct |
12 |
Correct |
4 ms |
5376 KB |
Output is correct |
13 |
Correct |
4 ms |
5376 KB |
Output is correct |
14 |
Correct |
5 ms |
5376 KB |
Output is correct |
15 |
Correct |
5 ms |
5376 KB |
Output is correct |
16 |
Correct |
5 ms |
5376 KB |
Output is correct |
17 |
Correct |
4 ms |
5376 KB |
Output is correct |
18 |
Correct |
6 ms |
5376 KB |
Output is correct |
19 |
Correct |
5 ms |
5376 KB |
Output is correct |
20 |
Correct |
5 ms |
5504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5248 KB |
Output is correct |
5 |
Correct |
123 ms |
18912 KB |
Output is correct |
6 |
Correct |
62 ms |
28152 KB |
Output is correct |
7 |
Correct |
127 ms |
24900 KB |
Output is correct |
8 |
Correct |
97 ms |
19192 KB |
Output is correct |
9 |
Correct |
117 ms |
23032 KB |
Output is correct |
10 |
Correct |
92 ms |
19224 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5376 KB |
Output is correct |
14 |
Correct |
128 ms |
31608 KB |
Output is correct |
15 |
Correct |
128 ms |
31736 KB |
Output is correct |
16 |
Correct |
124 ms |
31764 KB |
Output is correct |
17 |
Correct |
152 ms |
31736 KB |
Output is correct |
18 |
Correct |
117 ms |
31672 KB |
Output is correct |
19 |
Correct |
114 ms |
31736 KB |
Output is correct |
20 |
Correct |
132 ms |
31656 KB |
Output is correct |
21 |
Correct |
15 ms |
6144 KB |
Output is correct |
22 |
Correct |
120 ms |
32016 KB |
Output is correct |
23 |
Correct |
121 ms |
32052 KB |
Output is correct |
24 |
Correct |
115 ms |
31992 KB |
Output is correct |
25 |
Correct |
122 ms |
32016 KB |
Output is correct |
26 |
Correct |
127 ms |
32040 KB |
Output is correct |
27 |
Correct |
139 ms |
31992 KB |
Output is correct |
28 |
Correct |
118 ms |
31992 KB |
Output is correct |
29 |
Correct |
107 ms |
31992 KB |
Output is correct |
30 |
Correct |
117 ms |
31992 KB |
Output is correct |
31 |
Correct |
173 ms |
21876 KB |
Output is correct |
32 |
Correct |
103 ms |
31736 KB |
Output is correct |
33 |
Correct |
243 ms |
28024 KB |
Output is correct |
34 |
Correct |
125 ms |
22132 KB |
Output is correct |
35 |
Correct |
203 ms |
27604 KB |
Output is correct |
36 |
Correct |
122 ms |
22392 KB |
Output is correct |
37 |
Correct |
241 ms |
27220 KB |
Output is correct |
38 |
Correct |
185 ms |
22384 KB |
Output is correct |
39 |
Correct |
109 ms |
31680 KB |
Output is correct |
40 |
Correct |
232 ms |
26088 KB |
Output is correct |
41 |
Correct |
4 ms |
5376 KB |
Output is correct |
42 |
Correct |
4 ms |
5376 KB |
Output is correct |
43 |
Correct |
4 ms |
5376 KB |
Output is correct |
44 |
Correct |
5 ms |
5376 KB |
Output is correct |
45 |
Correct |
5 ms |
5376 KB |
Output is correct |
46 |
Correct |
5 ms |
5376 KB |
Output is correct |
47 |
Correct |
4 ms |
5376 KB |
Output is correct |
48 |
Correct |
6 ms |
5376 KB |
Output is correct |
49 |
Correct |
5 ms |
5376 KB |
Output is correct |
50 |
Correct |
5 ms |
5504 KB |
Output is correct |
51 |
Correct |
208 ms |
22704 KB |
Output is correct |
52 |
Correct |
166 ms |
31992 KB |
Output is correct |
53 |
Correct |
267 ms |
26612 KB |
Output is correct |
54 |
Correct |
140 ms |
22520 KB |
Output is correct |
55 |
Correct |
224 ms |
22260 KB |
Output is correct |
56 |
Correct |
160 ms |
32152 KB |
Output is correct |
57 |
Correct |
228 ms |
27256 KB |
Output is correct |
58 |
Correct |
132 ms |
22520 KB |
Output is correct |
59 |
Correct |
196 ms |
22520 KB |
Output is correct |
60 |
Correct |
116 ms |
31992 KB |
Output is correct |
61 |
Correct |
212 ms |
27408 KB |
Output is correct |
62 |
Correct |
128 ms |
22444 KB |
Output is correct |
63 |
Correct |
188 ms |
22264 KB |
Output is correct |
64 |
Correct |
118 ms |
31992 KB |
Output is correct |
65 |
Correct |
261 ms |
27252 KB |
Output is correct |
66 |
Correct |
138 ms |
22648 KB |
Output is correct |
67 |
Correct |
221 ms |
22264 KB |
Output is correct |
68 |
Correct |
135 ms |
31992 KB |
Output is correct |
69 |
Correct |
200 ms |
25976 KB |
Output is correct |
70 |
Correct |
130 ms |
22648 KB |
Output is correct |