#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
const int INF=0x3f3f3f3f;
const int LOG=17;
const int N=1e5+5;
int n, q, dtime=1;
int L[N], R[N];
int par[LOG][N], dep[N];
vector <ppi> v[N];
vector <int> ls[N];
ll dp[N], F[2*N];
ll query(int x) {
ll ret=0;
for (; x>0; x-=x&-x) ret+=F[x];
return ret;
}
void update(int x, ll val) {
for (; x<=2*n; x+=x&-x) F[x]+=val;
}
void build() {
for (int i=1; i<LOG; ++i) {
for (int j=1; j<=n; ++j) {
par[i][j]=par[i-1][par[i-1][j]];
}
}
}
int digni(int x, int d) {
for (int i=LOG-1; i>=0; --i) {
if (dep[x]-(1<<i)>=d) x=par[i][x];
}
return x;
}
int lca(int x, int y) {
if (dep[x]<dep[y]) swap(x, y);
x=digni(x, dep[y]);
if (x==y) return x;
for (int i=LOG-1; i>=0; --i) {
if (par[i][x]!=par[i][y]) x=par[i][x], y=par[i][y];
}
return par[0][x];
}
void dfs(int node, int rod) {
dep[node]=dep[rod]+1;
L[node]=dtime++;
par[0][node]=rod;
for (int sus:ls[node]) if (sus!=rod) dfs(sus, node);
R[node]=dtime++;
}
ll rek(int node) {
ll sum=0, ret=0;
for (int sus:ls[node]) {
if (sus==par[0][node]) continue;
ret=max(ret, rek(sus));
sum+=dp[sus];
}
dp[node]=sum;
for (ppi pp:v[node]) {
ll curr=sum+pp.Y;
if (pp.X.X!=node) curr+=query(L[pp.X.X]);
if (pp.X.Y!=node) curr+=query(L[pp.X.Y]);
dp[node]=max(dp[node], curr);
}
ret=max(ret, dp[node]);
update(L[node], sum-dp[node]);
update(R[node], dp[node]-sum);
return ret;
}
void load() {
scanf("%d", &n);
for (int i=0; i<n-1; ++i) {
int a, b;
scanf("%d %d", &a, &b);
ls[a].pb(b); ls[b].pb(a);
}
dfs(1, 0);
build();
scanf("%d", &q);
for (int i=0; i<q; ++i) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int lc=lca(a, b);
v[lc].pb(mp(mp(a, b), c));
}
}
int main() {
load();
printf("%lld\n", rek(1));
return 0;
}
Compilation message
election_campaign.cpp: In function 'void load()':
election_campaign.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
91 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
94 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
99 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d %d %d", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
3 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5228 KB |
Output is correct |
5 |
Correct |
98 ms |
19692 KB |
Output is correct |
6 |
Correct |
59 ms |
28908 KB |
Output is correct |
7 |
Correct |
78 ms |
25580 KB |
Output is correct |
8 |
Correct |
72 ms |
20076 KB |
Output is correct |
9 |
Correct |
86 ms |
23788 KB |
Output is correct |
10 |
Correct |
79 ms |
19948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
6 ms |
5356 KB |
Output is correct |
4 |
Correct |
145 ms |
32204 KB |
Output is correct |
5 |
Correct |
135 ms |
32236 KB |
Output is correct |
6 |
Correct |
116 ms |
32336 KB |
Output is correct |
7 |
Correct |
177 ms |
32236 KB |
Output is correct |
8 |
Correct |
139 ms |
32236 KB |
Output is correct |
9 |
Correct |
120 ms |
32236 KB |
Output is correct |
10 |
Correct |
132 ms |
32236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
6 ms |
5356 KB |
Output is correct |
4 |
Correct |
145 ms |
32204 KB |
Output is correct |
5 |
Correct |
135 ms |
32236 KB |
Output is correct |
6 |
Correct |
116 ms |
32336 KB |
Output is correct |
7 |
Correct |
177 ms |
32236 KB |
Output is correct |
8 |
Correct |
139 ms |
32236 KB |
Output is correct |
9 |
Correct |
120 ms |
32236 KB |
Output is correct |
10 |
Correct |
132 ms |
32236 KB |
Output is correct |
11 |
Correct |
14 ms |
6124 KB |
Output is correct |
12 |
Correct |
136 ms |
32492 KB |
Output is correct |
13 |
Correct |
141 ms |
32620 KB |
Output is correct |
14 |
Correct |
124 ms |
32492 KB |
Output is correct |
15 |
Correct |
137 ms |
32492 KB |
Output is correct |
16 |
Correct |
116 ms |
32492 KB |
Output is correct |
17 |
Correct |
139 ms |
32492 KB |
Output is correct |
18 |
Correct |
141 ms |
32492 KB |
Output is correct |
19 |
Correct |
127 ms |
32492 KB |
Output is correct |
20 |
Correct |
135 ms |
32492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
22576 KB |
Output is correct |
2 |
Correct |
130 ms |
32296 KB |
Output is correct |
3 |
Correct |
230 ms |
28596 KB |
Output is correct |
4 |
Correct |
125 ms |
22948 KB |
Output is correct |
5 |
Correct |
180 ms |
27884 KB |
Output is correct |
6 |
Correct |
127 ms |
22880 KB |
Output is correct |
7 |
Correct |
246 ms |
27560 KB |
Output is correct |
8 |
Correct |
178 ms |
22892 KB |
Output is correct |
9 |
Correct |
126 ms |
32236 KB |
Output is correct |
10 |
Correct |
236 ms |
26532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
3 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5228 KB |
Output is correct |
5 |
Correct |
98 ms |
19692 KB |
Output is correct |
6 |
Correct |
59 ms |
28908 KB |
Output is correct |
7 |
Correct |
78 ms |
25580 KB |
Output is correct |
8 |
Correct |
72 ms |
20076 KB |
Output is correct |
9 |
Correct |
86 ms |
23788 KB |
Output is correct |
10 |
Correct |
79 ms |
19948 KB |
Output is correct |
11 |
Correct |
4 ms |
5356 KB |
Output is correct |
12 |
Correct |
5 ms |
5356 KB |
Output is correct |
13 |
Correct |
5 ms |
5356 KB |
Output is correct |
14 |
Correct |
4 ms |
5356 KB |
Output is correct |
15 |
Correct |
5 ms |
5356 KB |
Output is correct |
16 |
Correct |
5 ms |
5356 KB |
Output is correct |
17 |
Correct |
4 ms |
5356 KB |
Output is correct |
18 |
Correct |
6 ms |
5356 KB |
Output is correct |
19 |
Correct |
4 ms |
5356 KB |
Output is correct |
20 |
Correct |
5 ms |
5356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5228 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
3 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5228 KB |
Output is correct |
5 |
Correct |
98 ms |
19692 KB |
Output is correct |
6 |
Correct |
59 ms |
28908 KB |
Output is correct |
7 |
Correct |
78 ms |
25580 KB |
Output is correct |
8 |
Correct |
72 ms |
20076 KB |
Output is correct |
9 |
Correct |
86 ms |
23788 KB |
Output is correct |
10 |
Correct |
79 ms |
19948 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
3 ms |
5100 KB |
Output is correct |
13 |
Correct |
6 ms |
5356 KB |
Output is correct |
14 |
Correct |
145 ms |
32204 KB |
Output is correct |
15 |
Correct |
135 ms |
32236 KB |
Output is correct |
16 |
Correct |
116 ms |
32336 KB |
Output is correct |
17 |
Correct |
177 ms |
32236 KB |
Output is correct |
18 |
Correct |
139 ms |
32236 KB |
Output is correct |
19 |
Correct |
120 ms |
32236 KB |
Output is correct |
20 |
Correct |
132 ms |
32236 KB |
Output is correct |
21 |
Correct |
14 ms |
6124 KB |
Output is correct |
22 |
Correct |
136 ms |
32492 KB |
Output is correct |
23 |
Correct |
141 ms |
32620 KB |
Output is correct |
24 |
Correct |
124 ms |
32492 KB |
Output is correct |
25 |
Correct |
137 ms |
32492 KB |
Output is correct |
26 |
Correct |
116 ms |
32492 KB |
Output is correct |
27 |
Correct |
139 ms |
32492 KB |
Output is correct |
28 |
Correct |
141 ms |
32492 KB |
Output is correct |
29 |
Correct |
127 ms |
32492 KB |
Output is correct |
30 |
Correct |
135 ms |
32492 KB |
Output is correct |
31 |
Correct |
183 ms |
22576 KB |
Output is correct |
32 |
Correct |
130 ms |
32296 KB |
Output is correct |
33 |
Correct |
230 ms |
28596 KB |
Output is correct |
34 |
Correct |
125 ms |
22948 KB |
Output is correct |
35 |
Correct |
180 ms |
27884 KB |
Output is correct |
36 |
Correct |
127 ms |
22880 KB |
Output is correct |
37 |
Correct |
246 ms |
27560 KB |
Output is correct |
38 |
Correct |
178 ms |
22892 KB |
Output is correct |
39 |
Correct |
126 ms |
32236 KB |
Output is correct |
40 |
Correct |
236 ms |
26532 KB |
Output is correct |
41 |
Correct |
4 ms |
5356 KB |
Output is correct |
42 |
Correct |
5 ms |
5356 KB |
Output is correct |
43 |
Correct |
5 ms |
5356 KB |
Output is correct |
44 |
Correct |
4 ms |
5356 KB |
Output is correct |
45 |
Correct |
5 ms |
5356 KB |
Output is correct |
46 |
Correct |
5 ms |
5356 KB |
Output is correct |
47 |
Correct |
4 ms |
5356 KB |
Output is correct |
48 |
Correct |
6 ms |
5356 KB |
Output is correct |
49 |
Correct |
4 ms |
5356 KB |
Output is correct |
50 |
Correct |
5 ms |
5356 KB |
Output is correct |
51 |
Correct |
179 ms |
23148 KB |
Output is correct |
52 |
Correct |
144 ms |
32492 KB |
Output is correct |
53 |
Correct |
242 ms |
26924 KB |
Output is correct |
54 |
Correct |
122 ms |
23208 KB |
Output is correct |
55 |
Correct |
188 ms |
22912 KB |
Output is correct |
56 |
Correct |
135 ms |
32492 KB |
Output is correct |
57 |
Correct |
186 ms |
27756 KB |
Output is correct |
58 |
Correct |
154 ms |
23216 KB |
Output is correct |
59 |
Correct |
175 ms |
23020 KB |
Output is correct |
60 |
Correct |
138 ms |
32492 KB |
Output is correct |
61 |
Correct |
202 ms |
27884 KB |
Output is correct |
62 |
Correct |
127 ms |
22856 KB |
Output is correct |
63 |
Correct |
184 ms |
22740 KB |
Output is correct |
64 |
Correct |
137 ms |
32620 KB |
Output is correct |
65 |
Correct |
226 ms |
27660 KB |
Output is correct |
66 |
Correct |
123 ms |
23080 KB |
Output is correct |
67 |
Correct |
194 ms |
22624 KB |
Output is correct |
68 |
Correct |
141 ms |
32644 KB |
Output is correct |
69 |
Correct |
187 ms |
26348 KB |
Output is correct |
70 |
Correct |
129 ms |
23248 KB |
Output is correct |