/*
Procrastination is the art of keeping up with yesterday
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define int long long
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 1;
//const int Mod = 1e9 + 7;
//const int inf =
int n, q;
struct TST{
vector <ll> st;
void resize(int _n){
st.resize(_n, 0);
}
void update(int id, int i, ll val, int l = 1, int r = n){
if (l == r){
st[id] = val;
return;
}
int mid = (l + r) >> 1;
if (i <= mid) update(id << 1, i, val, l, mid);
else update(id << 1 | 1, i, val, mid + 1, r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
int get(int id, int i, int j, int l = 1, int r = n){
if (i > r || l > j) return 0;
if (i <= l && r <= j) return st[id];
int mid = (l + r) >> 1;
return get(id << 1, i, j, l, mid) + get(id << 1 | 1, i, j, mid + 1, r);
}
};
TST tree[2];
struct TLine{
int u, v, c;
};
TLine e[maxN];
vector <int> adj[maxN];
vector <int> Q[maxN];
int child[maxN];
int id[maxN], num[maxN], timedfs, nodecnt = 1;
int head[maxN], tail[maxN];
int depth[maxN];
int par[maxN];
ll dp[maxN];
inline int lca(int u, int v){
while (id[u] != id[v]){
if (depth[head[id[u]]] < depth[head[id[v]]]) swap(u, v);
u = par[head[id[u]]];
}
if (depth[u] < depth[v]) swap(u, v);
return v;
}
inline ll Query(int u, int v, int t){
int res = 0;
while (id[u] != id[v]){
if (depth[head[id[u]]] < depth[head[id[v]]]) swap(u, v);
res += tree[t].get(1, num[head[id[u]]], num[u]);
u = par[head[id[u]]];
}
if (depth[u] < depth[v]) swap(u, v);
res += tree[t].get(1, num[v], num[u]);
return res;
}
void pre_dfs(int u, int p){
depth[u] = depth[p] + 1;
par[u] = p;
for (int i: adj[u]){
if (i == p) continue;
pre_dfs(i, u);
child[u] += child[i] + 1;
}
}
void hld(int u){
if (!head[nodecnt]) head[nodecnt] = u;
id[u] = nodecnt;
num[u] = ++timedfs;
pii maxx = {-1, -1};
for (int i: adj[u]){
if (i == par[u]) continue;
maxx = max(maxx, {child[i], i});
}
if (maxx.se != -1){
hld(maxx.se);
}
else tail[nodecnt] = u;
for (int i: adj[u]){
if (i == par[u] || i == maxx.se) continue;
++nodecnt;
hld(i);
}
}
void dfs(int u){
ll sum = 0;
for (int i: adj[u]){
if (i == par[u]) continue;
dfs(i);
sum += dp[i];
}
dp[u] = sum;
tree[0].update(1, num[u], dp[u]);
for (int i: Q[u]){
dp[u] = max(dp[u], Query(e[i].u, e[i].v, 0) - Query(e[i].u, e[i].v, 1) + e[i].c);
}
tree[1].update(1, num[u], dp[u]);
}
void Init(){
cin >> n;
for (int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
pre_dfs(1, 0);
hld(1);
tree[0].resize(maxN * 4);
tree[1].resize(maxN * 4);
cin >> q;
for (int i = 1; i <= q; ++i){
cin >> e[i].u >> e[i].v >> e[i].c;
Q[lca(e[i].u, e[i].v)].pb(i);
}
dfs(1);
cout << dp[1];
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11348 KB |
Output is correct |
2 |
Correct |
6 ms |
11316 KB |
Output is correct |
3 |
Correct |
5 ms |
11312 KB |
Output is correct |
4 |
Correct |
7 ms |
11352 KB |
Output is correct |
5 |
Correct |
104 ms |
21824 KB |
Output is correct |
6 |
Correct |
56 ms |
29588 KB |
Output is correct |
7 |
Correct |
84 ms |
27212 KB |
Output is correct |
8 |
Correct |
78 ms |
21948 KB |
Output is correct |
9 |
Correct |
97 ms |
25340 KB |
Output is correct |
10 |
Correct |
75 ms |
21964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11348 KB |
Output is correct |
2 |
Correct |
7 ms |
11348 KB |
Output is correct |
3 |
Correct |
6 ms |
11476 KB |
Output is correct |
4 |
Correct |
142 ms |
34848 KB |
Output is correct |
5 |
Correct |
134 ms |
34928 KB |
Output is correct |
6 |
Correct |
130 ms |
34848 KB |
Output is correct |
7 |
Correct |
131 ms |
34864 KB |
Output is correct |
8 |
Correct |
150 ms |
35072 KB |
Output is correct |
9 |
Correct |
131 ms |
34912 KB |
Output is correct |
10 |
Correct |
129 ms |
34776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11348 KB |
Output is correct |
2 |
Correct |
7 ms |
11348 KB |
Output is correct |
3 |
Correct |
6 ms |
11476 KB |
Output is correct |
4 |
Correct |
142 ms |
34848 KB |
Output is correct |
5 |
Correct |
134 ms |
34928 KB |
Output is correct |
6 |
Correct |
130 ms |
34848 KB |
Output is correct |
7 |
Correct |
131 ms |
34864 KB |
Output is correct |
8 |
Correct |
150 ms |
35072 KB |
Output is correct |
9 |
Correct |
131 ms |
34912 KB |
Output is correct |
10 |
Correct |
129 ms |
34776 KB |
Output is correct |
11 |
Correct |
17 ms |
12620 KB |
Output is correct |
12 |
Correct |
143 ms |
35164 KB |
Output is correct |
13 |
Correct |
149 ms |
35148 KB |
Output is correct |
14 |
Correct |
117 ms |
35124 KB |
Output is correct |
15 |
Correct |
135 ms |
35248 KB |
Output is correct |
16 |
Correct |
155 ms |
35224 KB |
Output is correct |
17 |
Correct |
133 ms |
35120 KB |
Output is correct |
18 |
Correct |
135 ms |
35124 KB |
Output is correct |
19 |
Correct |
116 ms |
35124 KB |
Output is correct |
20 |
Correct |
151 ms |
35088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
26504 KB |
Output is correct |
2 |
Correct |
149 ms |
34892 KB |
Output is correct |
3 |
Correct |
218 ms |
32020 KB |
Output is correct |
4 |
Correct |
225 ms |
26764 KB |
Output is correct |
5 |
Correct |
194 ms |
31436 KB |
Output is correct |
6 |
Correct |
217 ms |
26720 KB |
Output is correct |
7 |
Correct |
224 ms |
31212 KB |
Output is correct |
8 |
Correct |
270 ms |
26760 KB |
Output is correct |
9 |
Correct |
130 ms |
34892 KB |
Output is correct |
10 |
Correct |
214 ms |
30012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11348 KB |
Output is correct |
2 |
Correct |
6 ms |
11316 KB |
Output is correct |
3 |
Correct |
5 ms |
11312 KB |
Output is correct |
4 |
Correct |
7 ms |
11352 KB |
Output is correct |
5 |
Correct |
104 ms |
21824 KB |
Output is correct |
6 |
Correct |
56 ms |
29588 KB |
Output is correct |
7 |
Correct |
84 ms |
27212 KB |
Output is correct |
8 |
Correct |
78 ms |
21948 KB |
Output is correct |
9 |
Correct |
97 ms |
25340 KB |
Output is correct |
10 |
Correct |
75 ms |
21964 KB |
Output is correct |
11 |
Correct |
8 ms |
11452 KB |
Output is correct |
12 |
Correct |
7 ms |
11580 KB |
Output is correct |
13 |
Correct |
6 ms |
11476 KB |
Output is correct |
14 |
Correct |
7 ms |
11476 KB |
Output is correct |
15 |
Correct |
10 ms |
11476 KB |
Output is correct |
16 |
Correct |
9 ms |
11476 KB |
Output is correct |
17 |
Correct |
7 ms |
11448 KB |
Output is correct |
18 |
Correct |
7 ms |
11448 KB |
Output is correct |
19 |
Correct |
7 ms |
11476 KB |
Output is correct |
20 |
Correct |
7 ms |
11604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11348 KB |
Output is correct |
2 |
Correct |
6 ms |
11316 KB |
Output is correct |
3 |
Correct |
5 ms |
11312 KB |
Output is correct |
4 |
Correct |
7 ms |
11352 KB |
Output is correct |
5 |
Correct |
104 ms |
21824 KB |
Output is correct |
6 |
Correct |
56 ms |
29588 KB |
Output is correct |
7 |
Correct |
84 ms |
27212 KB |
Output is correct |
8 |
Correct |
78 ms |
21948 KB |
Output is correct |
9 |
Correct |
97 ms |
25340 KB |
Output is correct |
10 |
Correct |
75 ms |
21964 KB |
Output is correct |
11 |
Correct |
6 ms |
11348 KB |
Output is correct |
12 |
Correct |
7 ms |
11348 KB |
Output is correct |
13 |
Correct |
6 ms |
11476 KB |
Output is correct |
14 |
Correct |
142 ms |
34848 KB |
Output is correct |
15 |
Correct |
134 ms |
34928 KB |
Output is correct |
16 |
Correct |
130 ms |
34848 KB |
Output is correct |
17 |
Correct |
131 ms |
34864 KB |
Output is correct |
18 |
Correct |
150 ms |
35072 KB |
Output is correct |
19 |
Correct |
131 ms |
34912 KB |
Output is correct |
20 |
Correct |
129 ms |
34776 KB |
Output is correct |
21 |
Correct |
17 ms |
12620 KB |
Output is correct |
22 |
Correct |
143 ms |
35164 KB |
Output is correct |
23 |
Correct |
149 ms |
35148 KB |
Output is correct |
24 |
Correct |
117 ms |
35124 KB |
Output is correct |
25 |
Correct |
135 ms |
35248 KB |
Output is correct |
26 |
Correct |
155 ms |
35224 KB |
Output is correct |
27 |
Correct |
133 ms |
35120 KB |
Output is correct |
28 |
Correct |
135 ms |
35124 KB |
Output is correct |
29 |
Correct |
116 ms |
35124 KB |
Output is correct |
30 |
Correct |
151 ms |
35088 KB |
Output is correct |
31 |
Correct |
429 ms |
26504 KB |
Output is correct |
32 |
Correct |
149 ms |
34892 KB |
Output is correct |
33 |
Correct |
218 ms |
32020 KB |
Output is correct |
34 |
Correct |
225 ms |
26764 KB |
Output is correct |
35 |
Correct |
194 ms |
31436 KB |
Output is correct |
36 |
Correct |
217 ms |
26720 KB |
Output is correct |
37 |
Correct |
224 ms |
31212 KB |
Output is correct |
38 |
Correct |
270 ms |
26760 KB |
Output is correct |
39 |
Correct |
130 ms |
34892 KB |
Output is correct |
40 |
Correct |
214 ms |
30012 KB |
Output is correct |
41 |
Correct |
8 ms |
11452 KB |
Output is correct |
42 |
Correct |
7 ms |
11580 KB |
Output is correct |
43 |
Correct |
6 ms |
11476 KB |
Output is correct |
44 |
Correct |
7 ms |
11476 KB |
Output is correct |
45 |
Correct |
10 ms |
11476 KB |
Output is correct |
46 |
Correct |
9 ms |
11476 KB |
Output is correct |
47 |
Correct |
7 ms |
11448 KB |
Output is correct |
48 |
Correct |
7 ms |
11448 KB |
Output is correct |
49 |
Correct |
7 ms |
11476 KB |
Output is correct |
50 |
Correct |
7 ms |
11604 KB |
Output is correct |
51 |
Correct |
315 ms |
27064 KB |
Output is correct |
52 |
Correct |
135 ms |
35148 KB |
Output is correct |
53 |
Correct |
249 ms |
30404 KB |
Output is correct |
54 |
Correct |
178 ms |
26924 KB |
Output is correct |
55 |
Correct |
472 ms |
26808 KB |
Output is correct |
56 |
Correct |
135 ms |
35248 KB |
Output is correct |
57 |
Correct |
180 ms |
31100 KB |
Output is correct |
58 |
Correct |
271 ms |
27000 KB |
Output is correct |
59 |
Correct |
264 ms |
27044 KB |
Output is correct |
60 |
Correct |
148 ms |
35172 KB |
Output is correct |
61 |
Correct |
207 ms |
31352 KB |
Output is correct |
62 |
Correct |
215 ms |
26832 KB |
Output is correct |
63 |
Correct |
430 ms |
26728 KB |
Output is correct |
64 |
Correct |
135 ms |
35148 KB |
Output is correct |
65 |
Correct |
226 ms |
31152 KB |
Output is correct |
66 |
Correct |
208 ms |
27072 KB |
Output is correct |
67 |
Correct |
465 ms |
26712 KB |
Output is correct |
68 |
Correct |
144 ms |
35120 KB |
Output is correct |
69 |
Correct |
185 ms |
29808 KB |
Output is correct |
70 |
Correct |
226 ms |
27172 KB |
Output is correct |