#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pill pair<int,ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define name "test"
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
struct item{
int u, v, w;
};
int n;
int m;
ll f[N];
vector<int> adj[N];
vector<item> campaign[N];
struct FenwickTree{
int n;
ll val[N];
FenwickTree(){};
void update(int idx, ll x){
assert(idx > 0);
for (; idx <= n; idx += idx & -idx)
val[idx] += x;
}
ll get(int idx){
ll res = 0;
for (; idx; idx -= idx & -idx)
res += val[idx];
return res;
}
ll get(int l, int r){
return get(r) - get(l - 1);
}
} ft;
struct HLD{
int cntDfs;
int inv[N];
int cnt[N];
int idx[N];
int par[N];
int head[N];
int high[N];
int big_child[N];
long long f[N];
long long g[N];
HLD(){};
void dfs(int u, int pre){
int C = 0;
cnt[u] = 1;
head[u] = u;
for (int v : adj[u]) if (v != pre){
dfs(v, u);
par[v] = u;
cnt[u] += cnt[v];
if (cnt[C] <= cnt[v]) C = v;
}
big_child[u] = C;
}
void indexing(int u, int pre){
idx[u] = ++cntDfs;
inv[cntDfs] = u;
if (!big_child[u]) return;
head[big_child[u]] = head[u];
high[big_child[u]] = high[u];
indexing(big_child[u], u);
for (int v : adj[u]) if (v != pre && v != big_child[u]){
high[v] = high[u] + 1;
indexing(v, u);
}
}
int LCA(int u, int v){
if (high[u] < high[v]) swap(u, v);
while (high[u] > high[v]) u = par[head[u]];
while (head[u] != head[v]){
u = par[head[u]];
v = par[head[v]];
}
return idx[u] < idx[v] ? u : v;
}
void init(){
dfs(1, 0);
indexing(1, 0);
}
ll get(int u, int v, int nxtU, int ancestor, bool debug){
if (high[u] < high[v]) swap(u, v);
ll ans = 0;
while (high[u] > high[v]){
if (head[inv[idx[u] + 1]] == head[u]) ans += f[inv[idx[u] + 1]];
ans += ft.get(idx[head[u]], idx[u]) - f[head[u]];
u = par[head[u]];
}
while (head[u] != head[v]){
if (head[inv[idx[u] + 1]] == head[u]) ans += f[inv[idx[u] + 1]];
if (head[inv[idx[v] + 1]] == head[v]) ans += f[inv[idx[v] + 1]];
ans += ft.get(idx[head[u]], idx[u]) - f[head[u]];
ans += ft.get(idx[head[v]], idx[v]) - f[head[v]];
u = par[head[u]];
v = par[head[v]];
}
if (idx[u] > idx[v]) swap(u, v);
ans += ft.get(idx[u], idx[v]);
if (head[inv[idx[v] + 1]] == head[v]) ans += f[inv[idx[v] + 1]];
return ans;
}
void dfsAns(int u, int pre){
int nxtU = -1;
if (idx[u] < n && head[inv[idx[u] + 1]] == head[u])
nxtU = inv[idx[u] + 1];
for (int v : adj[u]) if (v != pre){
dfsAns(v, u);
f[u] += f[v];
}
maxi(f[u], g[u]);
ft.update(idx[u], g[u]);
for (auto [a, b, w] : campaign[u]){
maxi(f[u], get(a, b, nxtU, u, 0) + w);
}
if (head[par[u]] != head[u]){
g[par[u]] += f[u];
}
}
void solve(){
ft.n = n;
dfsAns(1, 0);
cout << f[1];
}
} tree;
void load(){
cin.tie(0)->sync_with_stdio(0);
if (fopen(name".inp", "r")){
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
}
void solve(){
cin >> n;
for (int i = 1; i < n; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
tree.init();
cin >> m;
for (int i = 1; i <= m; i++){
int u, v, w; cin >> u >> v >> w;
campaign[tree.LCA(u, v)].push_back({u, v, w});
}
tree.solve();
}
int main(){
load();
solve();
}
Compilation message
election_campaign.cpp: In member function 'void HLD::dfsAns(int, int)':
election_campaign.cpp:143:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
143 | for (auto [a, b, w] : campaign[u]){
| ^
election_campaign.cpp: In function 'void load()':
election_campaign.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
42 ms |
15180 KB |
Output is correct |
6 |
Correct |
32 ms |
23748 KB |
Output is correct |
7 |
Correct |
37 ms |
21064 KB |
Output is correct |
8 |
Correct |
29 ms |
14160 KB |
Output is correct |
9 |
Correct |
38 ms |
19284 KB |
Output is correct |
10 |
Correct |
29 ms |
14028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5812 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5968 KB |
Output is correct |
4 |
Correct |
59 ms |
24648 KB |
Output is correct |
5 |
Correct |
58 ms |
26928 KB |
Output is correct |
6 |
Correct |
53 ms |
24648 KB |
Output is correct |
7 |
Correct |
54 ms |
24440 KB |
Output is correct |
8 |
Correct |
69 ms |
27036 KB |
Output is correct |
9 |
Correct |
57 ms |
26192 KB |
Output is correct |
10 |
Correct |
62 ms |
25924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5812 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5968 KB |
Output is correct |
4 |
Correct |
59 ms |
24648 KB |
Output is correct |
5 |
Correct |
58 ms |
26928 KB |
Output is correct |
6 |
Correct |
53 ms |
24648 KB |
Output is correct |
7 |
Correct |
54 ms |
24440 KB |
Output is correct |
8 |
Correct |
69 ms |
27036 KB |
Output is correct |
9 |
Correct |
57 ms |
26192 KB |
Output is correct |
10 |
Correct |
62 ms |
25924 KB |
Output is correct |
11 |
Correct |
7 ms |
6736 KB |
Output is correct |
12 |
Correct |
65 ms |
27208 KB |
Output is correct |
13 |
Correct |
73 ms |
24648 KB |
Output is correct |
14 |
Correct |
64 ms |
24476 KB |
Output is correct |
15 |
Correct |
59 ms |
26184 KB |
Output is correct |
16 |
Correct |
73 ms |
27076 KB |
Output is correct |
17 |
Correct |
62 ms |
24536 KB |
Output is correct |
18 |
Correct |
64 ms |
24392 KB |
Output is correct |
19 |
Correct |
60 ms |
24652 KB |
Output is correct |
20 |
Correct |
59 ms |
24472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
15616 KB |
Output is correct |
2 |
Correct |
62 ms |
27116 KB |
Output is correct |
3 |
Correct |
90 ms |
24136 KB |
Output is correct |
4 |
Correct |
58 ms |
16912 KB |
Output is correct |
5 |
Correct |
69 ms |
23480 KB |
Output is correct |
6 |
Correct |
56 ms |
16968 KB |
Output is correct |
7 |
Correct |
75 ms |
23092 KB |
Output is correct |
8 |
Correct |
84 ms |
18140 KB |
Output is correct |
9 |
Correct |
55 ms |
26952 KB |
Output is correct |
10 |
Correct |
78 ms |
22016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
42 ms |
15180 KB |
Output is correct |
6 |
Correct |
32 ms |
23748 KB |
Output is correct |
7 |
Correct |
37 ms |
21064 KB |
Output is correct |
8 |
Correct |
29 ms |
14160 KB |
Output is correct |
9 |
Correct |
38 ms |
19284 KB |
Output is correct |
10 |
Correct |
29 ms |
14028 KB |
Output is correct |
11 |
Correct |
3 ms |
5968 KB |
Output is correct |
12 |
Correct |
3 ms |
5968 KB |
Output is correct |
13 |
Correct |
3 ms |
5968 KB |
Output is correct |
14 |
Correct |
2 ms |
6756 KB |
Output is correct |
15 |
Correct |
2 ms |
8784 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
3 ms |
8784 KB |
Output is correct |
18 |
Correct |
2 ms |
8784 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
8784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
42 ms |
15180 KB |
Output is correct |
6 |
Correct |
32 ms |
23748 KB |
Output is correct |
7 |
Correct |
37 ms |
21064 KB |
Output is correct |
8 |
Correct |
29 ms |
14160 KB |
Output is correct |
9 |
Correct |
38 ms |
19284 KB |
Output is correct |
10 |
Correct |
29 ms |
14028 KB |
Output is correct |
11 |
Correct |
3 ms |
5812 KB |
Output is correct |
12 |
Correct |
2 ms |
5712 KB |
Output is correct |
13 |
Correct |
2 ms |
5968 KB |
Output is correct |
14 |
Correct |
59 ms |
24648 KB |
Output is correct |
15 |
Correct |
58 ms |
26928 KB |
Output is correct |
16 |
Correct |
53 ms |
24648 KB |
Output is correct |
17 |
Correct |
54 ms |
24440 KB |
Output is correct |
18 |
Correct |
69 ms |
27036 KB |
Output is correct |
19 |
Correct |
57 ms |
26192 KB |
Output is correct |
20 |
Correct |
62 ms |
25924 KB |
Output is correct |
21 |
Correct |
7 ms |
6736 KB |
Output is correct |
22 |
Correct |
65 ms |
27208 KB |
Output is correct |
23 |
Correct |
73 ms |
24648 KB |
Output is correct |
24 |
Correct |
64 ms |
24476 KB |
Output is correct |
25 |
Correct |
59 ms |
26184 KB |
Output is correct |
26 |
Correct |
73 ms |
27076 KB |
Output is correct |
27 |
Correct |
62 ms |
24536 KB |
Output is correct |
28 |
Correct |
64 ms |
24392 KB |
Output is correct |
29 |
Correct |
60 ms |
24652 KB |
Output is correct |
30 |
Correct |
59 ms |
24472 KB |
Output is correct |
31 |
Correct |
98 ms |
15616 KB |
Output is correct |
32 |
Correct |
62 ms |
27116 KB |
Output is correct |
33 |
Correct |
90 ms |
24136 KB |
Output is correct |
34 |
Correct |
58 ms |
16912 KB |
Output is correct |
35 |
Correct |
69 ms |
23480 KB |
Output is correct |
36 |
Correct |
56 ms |
16968 KB |
Output is correct |
37 |
Correct |
75 ms |
23092 KB |
Output is correct |
38 |
Correct |
84 ms |
18140 KB |
Output is correct |
39 |
Correct |
55 ms |
26952 KB |
Output is correct |
40 |
Correct |
78 ms |
22016 KB |
Output is correct |
41 |
Correct |
3 ms |
5968 KB |
Output is correct |
42 |
Correct |
3 ms |
5968 KB |
Output is correct |
43 |
Correct |
3 ms |
5968 KB |
Output is correct |
44 |
Correct |
2 ms |
6756 KB |
Output is correct |
45 |
Correct |
2 ms |
8784 KB |
Output is correct |
46 |
Correct |
2 ms |
6736 KB |
Output is correct |
47 |
Correct |
3 ms |
8784 KB |
Output is correct |
48 |
Correct |
2 ms |
8784 KB |
Output is correct |
49 |
Correct |
2 ms |
6736 KB |
Output is correct |
50 |
Correct |
2 ms |
8784 KB |
Output is correct |
51 |
Correct |
71 ms |
18456 KB |
Output is correct |
52 |
Correct |
58 ms |
27208 KB |
Output is correct |
53 |
Correct |
68 ms |
22540 KB |
Output is correct |
54 |
Correct |
55 ms |
17288 KB |
Output is correct |
55 |
Correct |
97 ms |
18432 KB |
Output is correct |
56 |
Correct |
59 ms |
27208 KB |
Output is correct |
57 |
Correct |
78 ms |
23180 KB |
Output is correct |
58 |
Correct |
58 ms |
17244 KB |
Output is correct |
59 |
Correct |
77 ms |
18520 KB |
Output is correct |
60 |
Correct |
60 ms |
27220 KB |
Output is correct |
61 |
Correct |
86 ms |
22600 KB |
Output is correct |
62 |
Correct |
76 ms |
16420 KB |
Output is correct |
63 |
Correct |
117 ms |
17580 KB |
Output is correct |
64 |
Correct |
62 ms |
26700 KB |
Output is correct |
65 |
Correct |
85 ms |
22528 KB |
Output is correct |
66 |
Correct |
63 ms |
16516 KB |
Output is correct |
67 |
Correct |
109 ms |
17612 KB |
Output is correct |
68 |
Correct |
61 ms |
26696 KB |
Output is correct |
69 |
Correct |
82 ms |
21124 KB |
Output is correct |
70 |
Correct |
61 ms |
16680 KB |
Output is correct |