#include <bits/stdc++.h>
using namespace std;
#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, l, r) for(int i=l; i<r; i++)
#define frange(i, l) forr(i, 0, l)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef long long lli;
typedef vector<vi> vvi;
typedef vector<lli> vll;
typedef vector<bool> vb;
typedef set<int> seti;
typedef multiset<int> mseti;
void usaco()
{
freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
// freopen("problem.out", "w", stdout);
}
vvi liftv;
vvi graph;
vi depth;
void dfs(int x, int p) {
depth[x] = depth[p] + 1;
liftv[0][x] = p;
for(auto e : graph[x]) {
if(e != p) {
dfs(e, x);
}
}
}
int lift(int x, int c) {
frange(i, 20) {
if(c & (1<<i)) {
x = liftv[i][x];
}
}
return x;
}
int lca(int x, int y) {
if(depth[y] > depth[x]) swap(x, y);
x = lift(x, depth[x] - depth[y]);
if(x == y) return x;
for(int i=19; i>=0; i--) {
int xt = liftv[i][x];
int yt = liftv[i][y];
if(xt != yt) {
x = xt;
y = yt;
}
}
return liftv[0][x];
}
vvi st;
vvi lc;
vll cost;
lli tot = 0;
vii pos;
vector<pair<map<int, lli>, lli>> val;
void dfs2(int x, int p) {
val[x].f[x] = 0;
for(auto e : graph[x]) {
if(e != p) {
dfs2(e, x);
if(val[x].f.size() < val[e].f.size()) swap(val[x], val[e]);
for(auto pi : val[e].f) {
val[x].f[pi.f] += pi.s + val[e].s - val[x].s;
}
}
}
lli ma = -1e18;
// printf("%d\n", x);
// for(auto e : mv) {
// printf("%d %lld\n", e.f, e.s);
// }
for(auto e : lc[x]) {
ma = max(ma, val[x].f[pos[e].f] + val[x].f[pos[e].s] + 2*val[x].s + cost[e]);
}
if(ma > 0) {
tot += ma;
val[x].s -= ma;
}
}
int main() {
// usaco();
int n;
scd(n);
graph = vvi(n+1);
frange(i, n-1) {
int a, b;
scd(a);
scd(b);
graph[a].pb(b);
graph[b].pb(a);
}
depth = vi(n+1);
liftv = vvi(20, vi(n+1));
dfs(1, 0);
forr(i, 1, 20) {
forr(j, 1, n+1) {
liftv[i][j] = liftv[i-1][liftv[i-1][j]];
}
}
st = lc = vvi(n+1);
int m;
scd(m);
cost = vll(m);
pos = vii(m);
forr(i, 0, m) {
int a, b;
scd(a);
scd(b);
sclld(cost[i]);
lc[lca(a, b)].pb(i);
st[a].pb(i);
st[b].pb(i);
pos[i] = mp(a, b);
}
val = vector<pair<map<int, lli>, lli>>(n+1);
dfs2(1, 0);
printf("%lld", tot);
}
Compilation message
election_campaign.cpp: In function 'void usaco()':
election_campaign.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
election_campaign.cpp:107:5: note: in expansion of macro 'scd'
107 | scd(n);
| ^~~
election_campaign.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
election_campaign.cpp:111:9: note: in expansion of macro 'scd'
111 | scd(a);
| ^~~
election_campaign.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
election_campaign.cpp:112:9: note: in expansion of macro 'scd'
112 | scd(b);
| ^~~
election_campaign.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
election_campaign.cpp:129:5: note: in expansion of macro 'scd'
129 | scd(m);
| ^~~
election_campaign.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
election_campaign.cpp:134:9: note: in expansion of macro 'scd'
134 | scd(a);
| ^~~
election_campaign.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define scd(t) scanf("%d", &t)
| ~~~~~^~~~~~~~~~
election_campaign.cpp:135:9: note: in expansion of macro 'scd'
135 | scd(b);
| ^~~
election_campaign.cpp:6:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
6 | #define sclld(t) scanf("%lld", &t)
| ~~~~~^~~~~~~~~~~~
election_campaign.cpp:136:9: note: in expansion of macro 'sclld'
136 | sclld(cost[i]);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
170 ms |
67648 KB |
Output is correct |
6 |
Correct |
68 ms |
51916 KB |
Output is correct |
7 |
Correct |
118 ms |
47696 KB |
Output is correct |
8 |
Correct |
128 ms |
41036 KB |
Output is correct |
9 |
Correct |
112 ms |
47656 KB |
Output is correct |
10 |
Correct |
117 ms |
41656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
149 ms |
57668 KB |
Output is correct |
5 |
Correct |
149 ms |
57796 KB |
Output is correct |
6 |
Correct |
148 ms |
57780 KB |
Output is correct |
7 |
Correct |
147 ms |
57884 KB |
Output is correct |
8 |
Correct |
153 ms |
57804 KB |
Output is correct |
9 |
Correct |
133 ms |
57800 KB |
Output is correct |
10 |
Correct |
153 ms |
57816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
149 ms |
57668 KB |
Output is correct |
5 |
Correct |
149 ms |
57796 KB |
Output is correct |
6 |
Correct |
148 ms |
57780 KB |
Output is correct |
7 |
Correct |
147 ms |
57884 KB |
Output is correct |
8 |
Correct |
153 ms |
57804 KB |
Output is correct |
9 |
Correct |
133 ms |
57800 KB |
Output is correct |
10 |
Correct |
153 ms |
57816 KB |
Output is correct |
11 |
Correct |
11 ms |
1884 KB |
Output is correct |
12 |
Correct |
159 ms |
58056 KB |
Output is correct |
13 |
Correct |
150 ms |
58056 KB |
Output is correct |
14 |
Correct |
138 ms |
58140 KB |
Output is correct |
15 |
Correct |
150 ms |
58060 KB |
Output is correct |
16 |
Correct |
148 ms |
58092 KB |
Output is correct |
17 |
Correct |
162 ms |
58108 KB |
Output is correct |
18 |
Correct |
161 ms |
58060 KB |
Output is correct |
19 |
Correct |
174 ms |
58024 KB |
Output is correct |
20 |
Correct |
159 ms |
57904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
74032 KB |
Output is correct |
2 |
Correct |
143 ms |
57704 KB |
Output is correct |
3 |
Correct |
259 ms |
54320 KB |
Output is correct |
4 |
Correct |
212 ms |
46464 KB |
Output is correct |
5 |
Correct |
255 ms |
54212 KB |
Output is correct |
6 |
Correct |
212 ms |
45988 KB |
Output is correct |
7 |
Correct |
252 ms |
53708 KB |
Output is correct |
8 |
Correct |
267 ms |
74184 KB |
Output is correct |
9 |
Correct |
173 ms |
57896 KB |
Output is correct |
10 |
Correct |
254 ms |
52680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
170 ms |
67648 KB |
Output is correct |
6 |
Correct |
68 ms |
51916 KB |
Output is correct |
7 |
Correct |
118 ms |
47696 KB |
Output is correct |
8 |
Correct |
128 ms |
41036 KB |
Output is correct |
9 |
Correct |
112 ms |
47656 KB |
Output is correct |
10 |
Correct |
117 ms |
41656 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
940 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
956 KB |
Output is correct |
19 |
Correct |
2 ms |
856 KB |
Output is correct |
20 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
170 ms |
67648 KB |
Output is correct |
6 |
Correct |
68 ms |
51916 KB |
Output is correct |
7 |
Correct |
118 ms |
47696 KB |
Output is correct |
8 |
Correct |
128 ms |
41036 KB |
Output is correct |
9 |
Correct |
112 ms |
47656 KB |
Output is correct |
10 |
Correct |
117 ms |
41656 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
149 ms |
57668 KB |
Output is correct |
15 |
Correct |
149 ms |
57796 KB |
Output is correct |
16 |
Correct |
148 ms |
57780 KB |
Output is correct |
17 |
Correct |
147 ms |
57884 KB |
Output is correct |
18 |
Correct |
153 ms |
57804 KB |
Output is correct |
19 |
Correct |
133 ms |
57800 KB |
Output is correct |
20 |
Correct |
153 ms |
57816 KB |
Output is correct |
21 |
Correct |
11 ms |
1884 KB |
Output is correct |
22 |
Correct |
159 ms |
58056 KB |
Output is correct |
23 |
Correct |
150 ms |
58056 KB |
Output is correct |
24 |
Correct |
138 ms |
58140 KB |
Output is correct |
25 |
Correct |
150 ms |
58060 KB |
Output is correct |
26 |
Correct |
148 ms |
58092 KB |
Output is correct |
27 |
Correct |
162 ms |
58108 KB |
Output is correct |
28 |
Correct |
161 ms |
58060 KB |
Output is correct |
29 |
Correct |
174 ms |
58024 KB |
Output is correct |
30 |
Correct |
159 ms |
57904 KB |
Output is correct |
31 |
Correct |
286 ms |
74032 KB |
Output is correct |
32 |
Correct |
143 ms |
57704 KB |
Output is correct |
33 |
Correct |
259 ms |
54320 KB |
Output is correct |
34 |
Correct |
212 ms |
46464 KB |
Output is correct |
35 |
Correct |
255 ms |
54212 KB |
Output is correct |
36 |
Correct |
212 ms |
45988 KB |
Output is correct |
37 |
Correct |
252 ms |
53708 KB |
Output is correct |
38 |
Correct |
267 ms |
74184 KB |
Output is correct |
39 |
Correct |
173 ms |
57896 KB |
Output is correct |
40 |
Correct |
254 ms |
52680 KB |
Output is correct |
41 |
Correct |
3 ms |
860 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
2 ms |
860 KB |
Output is correct |
44 |
Correct |
1 ms |
860 KB |
Output is correct |
45 |
Correct |
2 ms |
940 KB |
Output is correct |
46 |
Correct |
2 ms |
860 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
956 KB |
Output is correct |
49 |
Correct |
2 ms |
856 KB |
Output is correct |
50 |
Correct |
3 ms |
860 KB |
Output is correct |
51 |
Correct |
272 ms |
73420 KB |
Output is correct |
52 |
Correct |
222 ms |
58056 KB |
Output is correct |
53 |
Correct |
292 ms |
53196 KB |
Output is correct |
54 |
Correct |
185 ms |
47800 KB |
Output is correct |
55 |
Correct |
305 ms |
73656 KB |
Output is correct |
56 |
Correct |
192 ms |
57936 KB |
Output is correct |
57 |
Correct |
257 ms |
53704 KB |
Output is correct |
58 |
Correct |
225 ms |
46896 KB |
Output is correct |
59 |
Correct |
306 ms |
74840 KB |
Output is correct |
60 |
Correct |
171 ms |
58016 KB |
Output is correct |
61 |
Correct |
233 ms |
53656 KB |
Output is correct |
62 |
Correct |
213 ms |
47452 KB |
Output is correct |
63 |
Correct |
306 ms |
73212 KB |
Output is correct |
64 |
Correct |
148 ms |
58064 KB |
Output is correct |
65 |
Correct |
248 ms |
53156 KB |
Output is correct |
66 |
Correct |
213 ms |
46584 KB |
Output is correct |
67 |
Correct |
292 ms |
73804 KB |
Output is correct |
68 |
Correct |
149 ms |
58060 KB |
Output is correct |
69 |
Correct |
237 ms |
53424 KB |
Output is correct |
70 |
Correct |
190 ms |
44848 KB |
Output is correct |