#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "
//#include "shoes.h"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <ll, pii> piii;
typedef tree <pii, null_type, less <pii>, rb_tree_tag, tree_order_statistics_node_update> OST;
const ll NMAX = 100001;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 316;
const ll nr_of_bits = 20;
const long double eps = 0.0000000000000001;
vector <ll> v[NMAX];
ll dp[NMAX][nr_of_bits + 1];
ll n;
ll lvl[NMAX];
pii timpi[NMAX];
ll stamp;
ll DP[NMAX];
ll aib[NMAX * 2 + 1];
void update(ll node, ll v) {
for(ll i = node; i <= 2 * n; i += i&(-i)){
aib[i] += v;
}
}
ll query(ll node){
ll v = 0;
for(ll i = node; i > 0; i -= i&(-i)){
v += aib[i];
}
return v;
}
struct queries {
ll a, b, la, lb, val;
};
vector <queries> events[NMAX];
bool isParent(ll a, ll b) {
return (timpi[a].first <= timpi[b].first && timpi[b].second <= timpi[a].second);
}
void DFS(ll node, ll p) {
dp[node][0] = p;
timpi[node].first = ++stamp;
lvl[node] = lvl[p] + 1;
for(auto x : v[node]) {
if(x == p)
continue;
DFS(x, node);
}
timpi[node].second = ++stamp;
}
ll LCA(ll a, ll b) {
if(lvl[a] > lvl[b])
swap(a, b);
ll r = a, pas = nr_of_bits;
while(pas >= 0) {
ll nxt = dp[r][pas];
if(nxt != 0 && !isParent(nxt, b))
r = nxt;
pas--;
}
if(!isParent(r, b))
r = dp[r][0];
return r;
}
ll sol;
void Compute(ll node, ll p){
ll sum = 0;
for(auto x : v[node]){
if(x == p)
continue;
Compute(x, node);
sum += DP[x];
}
ll maxim = sum;
for(auto x : events[node]){
ll cost = sum + x.val;
if(x.la != -1){
///poate facem ceva mai clean aici
cost += query(timpi[x.a].first);
}
if(x.lb != -1){
cost += query(timpi[x.b].first);
}
maxim = max(maxim, cost);
}
DP[node] = max(DP[node], maxim);
ll val = sum - DP[node];
update(timpi[node].first, val);
update(timpi[node].second, -val);
}
int main() {
ll i, j;
cin >> n;
for(i = 1; i < n; i++) {
ll x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
DFS(1, 0);
for(j = 1; j <= nr_of_bits; j++) {
for(i = 1; i <= n; i++) {
dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
}
ll m;
cin >> m;
while(m--) {
ll a, b, la, lb, val;
cin >> a >> b >> val;
ll lca = LCA(a, b);
if(lvl[a] == lvl[lca]) {
la = -1;
} else {
ll r = a, pas = nr_of_bits;
while(pas >= 0) {
ll nxt = dp[r][pas];
if(nxt != 0 && !isParent(nxt, lca))
r = nxt;
pas--;
}
la = r;
}
if(lvl[b] == lvl[lca]) {
lb = -1;
} else {
ll r = b, pas = nr_of_bits;
while(pas >= 0) {
ll nxt = dp[r][pas];
if(nxt != 0 && !isParent(nxt, lca))
r = nxt;
pas--;
}
lb = r;
}
events[lca].push_back({a, b, la, lb, val});
}
Compute(1, 0);
cout << DP[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
187 ms |
30316 KB |
Output is correct |
6 |
Correct |
122 ms |
37100 KB |
Output is correct |
7 |
Correct |
156 ms |
34796 KB |
Output is correct |
8 |
Correct |
149 ms |
30336 KB |
Output is correct |
9 |
Correct |
164 ms |
33260 KB |
Output is correct |
10 |
Correct |
139 ms |
30188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5356 KB |
Output is correct |
4 |
Correct |
340 ms |
42780 KB |
Output is correct |
5 |
Correct |
335 ms |
42732 KB |
Output is correct |
6 |
Correct |
300 ms |
42732 KB |
Output is correct |
7 |
Correct |
343 ms |
42732 KB |
Output is correct |
8 |
Correct |
342 ms |
42860 KB |
Output is correct |
9 |
Correct |
291 ms |
42732 KB |
Output is correct |
10 |
Correct |
325 ms |
42732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5356 KB |
Output is correct |
4 |
Correct |
340 ms |
42780 KB |
Output is correct |
5 |
Correct |
335 ms |
42732 KB |
Output is correct |
6 |
Correct |
300 ms |
42732 KB |
Output is correct |
7 |
Correct |
343 ms |
42732 KB |
Output is correct |
8 |
Correct |
342 ms |
42860 KB |
Output is correct |
9 |
Correct |
291 ms |
42732 KB |
Output is correct |
10 |
Correct |
325 ms |
42732 KB |
Output is correct |
11 |
Correct |
30 ms |
6892 KB |
Output is correct |
12 |
Correct |
344 ms |
42860 KB |
Output is correct |
13 |
Correct |
346 ms |
42672 KB |
Output is correct |
14 |
Correct |
330 ms |
42732 KB |
Output is correct |
15 |
Correct |
354 ms |
42860 KB |
Output is correct |
16 |
Correct |
315 ms |
42732 KB |
Output is correct |
17 |
Correct |
335 ms |
42732 KB |
Output is correct |
18 |
Correct |
332 ms |
42732 KB |
Output is correct |
19 |
Correct |
308 ms |
42732 KB |
Output is correct |
20 |
Correct |
338 ms |
42840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
303 ms |
34956 KB |
Output is correct |
2 |
Correct |
301 ms |
42860 KB |
Output is correct |
3 |
Correct |
493 ms |
39944 KB |
Output is correct |
4 |
Correct |
263 ms |
35672 KB |
Output is correct |
5 |
Correct |
399 ms |
39300 KB |
Output is correct |
6 |
Correct |
284 ms |
35468 KB |
Output is correct |
7 |
Correct |
458 ms |
39004 KB |
Output is correct |
8 |
Correct |
328 ms |
35072 KB |
Output is correct |
9 |
Correct |
299 ms |
42732 KB |
Output is correct |
10 |
Correct |
427 ms |
37764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
187 ms |
30316 KB |
Output is correct |
6 |
Correct |
122 ms |
37100 KB |
Output is correct |
7 |
Correct |
156 ms |
34796 KB |
Output is correct |
8 |
Correct |
149 ms |
30336 KB |
Output is correct |
9 |
Correct |
164 ms |
33260 KB |
Output is correct |
10 |
Correct |
139 ms |
30188 KB |
Output is correct |
11 |
Correct |
6 ms |
5356 KB |
Output is correct |
12 |
Correct |
7 ms |
5376 KB |
Output is correct |
13 |
Correct |
7 ms |
5356 KB |
Output is correct |
14 |
Correct |
5 ms |
5356 KB |
Output is correct |
15 |
Correct |
6 ms |
5356 KB |
Output is correct |
16 |
Correct |
5 ms |
5356 KB |
Output is correct |
17 |
Correct |
6 ms |
5356 KB |
Output is correct |
18 |
Correct |
6 ms |
5356 KB |
Output is correct |
19 |
Correct |
5 ms |
5356 KB |
Output is correct |
20 |
Correct |
5 ms |
5356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
187 ms |
30316 KB |
Output is correct |
6 |
Correct |
122 ms |
37100 KB |
Output is correct |
7 |
Correct |
156 ms |
34796 KB |
Output is correct |
8 |
Correct |
149 ms |
30336 KB |
Output is correct |
9 |
Correct |
164 ms |
33260 KB |
Output is correct |
10 |
Correct |
139 ms |
30188 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
5 ms |
5356 KB |
Output is correct |
14 |
Correct |
340 ms |
42780 KB |
Output is correct |
15 |
Correct |
335 ms |
42732 KB |
Output is correct |
16 |
Correct |
300 ms |
42732 KB |
Output is correct |
17 |
Correct |
343 ms |
42732 KB |
Output is correct |
18 |
Correct |
342 ms |
42860 KB |
Output is correct |
19 |
Correct |
291 ms |
42732 KB |
Output is correct |
20 |
Correct |
325 ms |
42732 KB |
Output is correct |
21 |
Correct |
30 ms |
6892 KB |
Output is correct |
22 |
Correct |
344 ms |
42860 KB |
Output is correct |
23 |
Correct |
346 ms |
42672 KB |
Output is correct |
24 |
Correct |
330 ms |
42732 KB |
Output is correct |
25 |
Correct |
354 ms |
42860 KB |
Output is correct |
26 |
Correct |
315 ms |
42732 KB |
Output is correct |
27 |
Correct |
335 ms |
42732 KB |
Output is correct |
28 |
Correct |
332 ms |
42732 KB |
Output is correct |
29 |
Correct |
308 ms |
42732 KB |
Output is correct |
30 |
Correct |
338 ms |
42840 KB |
Output is correct |
31 |
Correct |
303 ms |
34956 KB |
Output is correct |
32 |
Correct |
301 ms |
42860 KB |
Output is correct |
33 |
Correct |
493 ms |
39944 KB |
Output is correct |
34 |
Correct |
263 ms |
35672 KB |
Output is correct |
35 |
Correct |
399 ms |
39300 KB |
Output is correct |
36 |
Correct |
284 ms |
35468 KB |
Output is correct |
37 |
Correct |
458 ms |
39004 KB |
Output is correct |
38 |
Correct |
328 ms |
35072 KB |
Output is correct |
39 |
Correct |
299 ms |
42732 KB |
Output is correct |
40 |
Correct |
427 ms |
37764 KB |
Output is correct |
41 |
Correct |
6 ms |
5356 KB |
Output is correct |
42 |
Correct |
7 ms |
5376 KB |
Output is correct |
43 |
Correct |
7 ms |
5356 KB |
Output is correct |
44 |
Correct |
5 ms |
5356 KB |
Output is correct |
45 |
Correct |
6 ms |
5356 KB |
Output is correct |
46 |
Correct |
5 ms |
5356 KB |
Output is correct |
47 |
Correct |
6 ms |
5356 KB |
Output is correct |
48 |
Correct |
6 ms |
5356 KB |
Output is correct |
49 |
Correct |
5 ms |
5356 KB |
Output is correct |
50 |
Correct |
5 ms |
5356 KB |
Output is correct |
51 |
Correct |
372 ms |
35076 KB |
Output is correct |
52 |
Correct |
352 ms |
42732 KB |
Output is correct |
53 |
Correct |
466 ms |
37884 KB |
Output is correct |
54 |
Correct |
269 ms |
35096 KB |
Output is correct |
55 |
Correct |
305 ms |
35196 KB |
Output is correct |
56 |
Correct |
338 ms |
42988 KB |
Output is correct |
57 |
Correct |
434 ms |
38660 KB |
Output is correct |
58 |
Correct |
274 ms |
35448 KB |
Output is correct |
59 |
Correct |
340 ms |
34972 KB |
Output is correct |
60 |
Correct |
339 ms |
42860 KB |
Output is correct |
61 |
Correct |
460 ms |
38788 KB |
Output is correct |
62 |
Correct |
267 ms |
34476 KB |
Output is correct |
63 |
Correct |
312 ms |
34356 KB |
Output is correct |
64 |
Correct |
331 ms |
42732 KB |
Output is correct |
65 |
Correct |
475 ms |
38524 KB |
Output is correct |
66 |
Correct |
264 ms |
35144 KB |
Output is correct |
67 |
Correct |
325 ms |
34952 KB |
Output is correct |
68 |
Correct |
342 ms |
42696 KB |
Output is correct |
69 |
Correct |
447 ms |
37644 KB |
Output is correct |
70 |
Correct |
287 ms |
34400 KB |
Output is correct |