#include <bits/stdc++.h>
using namespace std;
#define forinc(a, b, c) for(int a=b, _c=c; a<=_c; ++a)
#define fordec(a, b, c) for(int a=b, _c=c; a>=_c; --a)
#define forv(a, b) for(auto &a : b)
#define rep(i, n) forinc(i, 1, n)
#define repv(i, n) forinc(i, 0, n - 1)
#define ii pair<int,int>
#define fi first
#define se second
#define all(a) begin(a), end(a)
#define reset(f, x) memset(f, x, sizeof(f))
#define eb emplace_back
#define pb push_back
const int N = 1e5+5;
int n, m;
struct pack {int u, v, c;};
vector<pack> que[N];
vector<int> ad[N];
int st[N], en[N], id, it[4 * N];
int pd[N][26], dep[N], f[N], g[N];
void upd(int i, int val) {
for(; i <= n; i += i & -i) it[i] += val;
}
int get(int i) {
int ans = 0;
for(; i; i -= i & -i) ans += it[i];
return ans;
}
void dfs(int u, int par) {
rep(i, 20) pd[u][i] = pd[pd[u][i - 1]][i - 1];
st[u] = ++id;
forv(v, ad[u]) if(v != par) {
dep[v] = dep[u] + 1;
pd[v][0] = u;
dfs(v, u);
}
en[u] = id;
}
int lca(int u, int v) {
if(dep[u] < dep[v]) swap(u, v);
int unx = dep[u] - dep[v];
fordec(i, 20, 0) if(unx >> i & 1) u = pd[u][i];
if(u == v) return u;
fordec(i, 20, 0) if(pd[u][i] != pd[v][i]) u = pd[u][i], v = pd[v][i];
return pd[u][0];
}
void dfs1(int u, int par) {
forv(v, ad[u]) if(v != par) {
dfs1(v, u);
g[u] += f[v];
}
forv(v, ad[u]) if(v != par) {
upd(st[v], g[u] - f[v]);
upd(en[v] + 1, f[v] - g[u]);
}
f[u] = g[u];
forv(p, que[u]) {
int x = p.u, y = p.v, c = p.c;
f[u] = max(f[u], get(st[x]) + get(st[y]) + g[x] + g[y] - g[u] + c);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
rep(i, n - 1) {
int u, v;
cin >> u >> v;
ad[u].pb(v); ad[v].pb(u);
}
dfs(1, 0);
cin >> m;
rep(i, m) {
int u, v, c;
cin >> u >> v >> c;
int p = lca(u, v);
que[p].pb({u, v, c});
}
dfs1(1, 0);
cout << f[1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
104 ms |
22008 KB |
Output is correct |
6 |
Correct |
66 ms |
29688 KB |
Output is correct |
7 |
Correct |
119 ms |
27000 KB |
Output is correct |
8 |
Correct |
77 ms |
22008 KB |
Output is correct |
9 |
Correct |
158 ms |
25464 KB |
Output is correct |
10 |
Correct |
76 ms |
22008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
140 ms |
33020 KB |
Output is correct |
5 |
Correct |
136 ms |
33016 KB |
Output is correct |
6 |
Correct |
127 ms |
33020 KB |
Output is correct |
7 |
Correct |
137 ms |
33004 KB |
Output is correct |
8 |
Correct |
137 ms |
33016 KB |
Output is correct |
9 |
Correct |
126 ms |
33016 KB |
Output is correct |
10 |
Correct |
137 ms |
33016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
140 ms |
33020 KB |
Output is correct |
5 |
Correct |
136 ms |
33016 KB |
Output is correct |
6 |
Correct |
127 ms |
33020 KB |
Output is correct |
7 |
Correct |
137 ms |
33004 KB |
Output is correct |
8 |
Correct |
137 ms |
33016 KB |
Output is correct |
9 |
Correct |
126 ms |
33016 KB |
Output is correct |
10 |
Correct |
137 ms |
33016 KB |
Output is correct |
11 |
Correct |
15 ms |
6016 KB |
Output is correct |
12 |
Correct |
144 ms |
33272 KB |
Output is correct |
13 |
Correct |
140 ms |
33272 KB |
Output is correct |
14 |
Correct |
129 ms |
33272 KB |
Output is correct |
15 |
Correct |
145 ms |
33272 KB |
Output is correct |
16 |
Correct |
132 ms |
33272 KB |
Output is correct |
17 |
Correct |
142 ms |
33272 KB |
Output is correct |
18 |
Correct |
141 ms |
33272 KB |
Output is correct |
19 |
Correct |
135 ms |
33272 KB |
Output is correct |
20 |
Correct |
142 ms |
33316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
24752 KB |
Output is correct |
2 |
Correct |
145 ms |
33112 KB |
Output is correct |
3 |
Correct |
252 ms |
29944 KB |
Output is correct |
4 |
Correct |
131 ms |
24880 KB |
Output is correct |
5 |
Correct |
206 ms |
29476 KB |
Output is correct |
6 |
Correct |
127 ms |
24808 KB |
Output is correct |
7 |
Correct |
231 ms |
29360 KB |
Output is correct |
8 |
Correct |
245 ms |
25176 KB |
Output is correct |
9 |
Correct |
149 ms |
32944 KB |
Output is correct |
10 |
Correct |
238 ms |
28208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
104 ms |
22008 KB |
Output is correct |
6 |
Correct |
66 ms |
29688 KB |
Output is correct |
7 |
Correct |
119 ms |
27000 KB |
Output is correct |
8 |
Correct |
77 ms |
22008 KB |
Output is correct |
9 |
Correct |
158 ms |
25464 KB |
Output is correct |
10 |
Correct |
76 ms |
22008 KB |
Output is correct |
11 |
Correct |
5 ms |
5376 KB |
Output is correct |
12 |
Correct |
5 ms |
5248 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
4 ms |
5248 KB |
Output is correct |
15 |
Correct |
9 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5248 KB |
Output is correct |
18 |
Correct |
7 ms |
5248 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
5 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
104 ms |
22008 KB |
Output is correct |
6 |
Correct |
66 ms |
29688 KB |
Output is correct |
7 |
Correct |
119 ms |
27000 KB |
Output is correct |
8 |
Correct |
77 ms |
22008 KB |
Output is correct |
9 |
Correct |
158 ms |
25464 KB |
Output is correct |
10 |
Correct |
76 ms |
22008 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5376 KB |
Output is correct |
14 |
Correct |
140 ms |
33020 KB |
Output is correct |
15 |
Correct |
136 ms |
33016 KB |
Output is correct |
16 |
Correct |
127 ms |
33020 KB |
Output is correct |
17 |
Correct |
137 ms |
33004 KB |
Output is correct |
18 |
Correct |
137 ms |
33016 KB |
Output is correct |
19 |
Correct |
126 ms |
33016 KB |
Output is correct |
20 |
Correct |
137 ms |
33016 KB |
Output is correct |
21 |
Correct |
15 ms |
6016 KB |
Output is correct |
22 |
Correct |
144 ms |
33272 KB |
Output is correct |
23 |
Correct |
140 ms |
33272 KB |
Output is correct |
24 |
Correct |
129 ms |
33272 KB |
Output is correct |
25 |
Correct |
145 ms |
33272 KB |
Output is correct |
26 |
Correct |
132 ms |
33272 KB |
Output is correct |
27 |
Correct |
142 ms |
33272 KB |
Output is correct |
28 |
Correct |
141 ms |
33272 KB |
Output is correct |
29 |
Correct |
135 ms |
33272 KB |
Output is correct |
30 |
Correct |
142 ms |
33316 KB |
Output is correct |
31 |
Correct |
180 ms |
24752 KB |
Output is correct |
32 |
Correct |
145 ms |
33112 KB |
Output is correct |
33 |
Correct |
252 ms |
29944 KB |
Output is correct |
34 |
Correct |
131 ms |
24880 KB |
Output is correct |
35 |
Correct |
206 ms |
29476 KB |
Output is correct |
36 |
Correct |
127 ms |
24808 KB |
Output is correct |
37 |
Correct |
231 ms |
29360 KB |
Output is correct |
38 |
Correct |
245 ms |
25176 KB |
Output is correct |
39 |
Correct |
149 ms |
32944 KB |
Output is correct |
40 |
Correct |
238 ms |
28208 KB |
Output is correct |
41 |
Correct |
5 ms |
5376 KB |
Output is correct |
42 |
Correct |
5 ms |
5248 KB |
Output is correct |
43 |
Correct |
4 ms |
5248 KB |
Output is correct |
44 |
Correct |
4 ms |
5248 KB |
Output is correct |
45 |
Correct |
9 ms |
5248 KB |
Output is correct |
46 |
Correct |
4 ms |
5248 KB |
Output is correct |
47 |
Correct |
8 ms |
5248 KB |
Output is correct |
48 |
Correct |
7 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
5 ms |
5376 KB |
Output is correct |
51 |
Correct |
201 ms |
25460 KB |
Output is correct |
52 |
Correct |
203 ms |
33272 KB |
Output is correct |
53 |
Correct |
246 ms |
28588 KB |
Output is correct |
54 |
Correct |
126 ms |
25140 KB |
Output is correct |
55 |
Correct |
209 ms |
25264 KB |
Output is correct |
56 |
Correct |
141 ms |
33272 KB |
Output is correct |
57 |
Correct |
208 ms |
29180 KB |
Output is correct |
58 |
Correct |
127 ms |
25060 KB |
Output is correct |
59 |
Correct |
178 ms |
25340 KB |
Output is correct |
60 |
Correct |
175 ms |
33400 KB |
Output is correct |
61 |
Correct |
209 ms |
29344 KB |
Output is correct |
62 |
Correct |
125 ms |
24908 KB |
Output is correct |
63 |
Correct |
167 ms |
24924 KB |
Output is correct |
64 |
Correct |
145 ms |
33276 KB |
Output is correct |
65 |
Correct |
230 ms |
29104 KB |
Output is correct |
66 |
Correct |
126 ms |
25288 KB |
Output is correct |
67 |
Correct |
182 ms |
25064 KB |
Output is correct |
68 |
Correct |
138 ms |
33400 KB |
Output is correct |
69 |
Correct |
207 ms |
28024 KB |
Output is correct |
70 |
Correct |
122 ms |
25308 KB |
Output is correct |