#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define ss(x) (int) x.size()
#define pb push_back
#define ll long long
#define cat(x) cerr << #x << " = " << x << endl
#define FOR(i, n) for(int i = 0; i < n; ++i)
using namespace std;
const int nax = 2e5 + 111;
int n, nn;
int a, b, c, d;
vector <pair<int,int>> v[nax];
ll all;
ll ans[nax];
int died[nax];
int siz[nax];
void dfs(int u, int p) {
siz[u] = 1;
nn++;
for(auto it : v[u])
if(it.fi != p && !died[it.fi]) {
dfs(it.fi, u);
siz[u] += siz[it.fi];
}
}
int daj(int u, int p) {
for(auto it : v[u])
if(it.fi != p && !died[it.fi] && nn <= 2 * siz[it.fi])
return daj(it.fi, u);
return u;
}
ll oblicz(int u, int p) {
ll sum = 0;
for(auto it : v[u]) {
if(it.fi == p) sum += it.se;
else if(!died[it.fi])
sum += oblicz(it.fi, u);
}
return sum;
}
ll dp[nax];
void makedp(int u, int p) {
dp[u] = 0;
for(auto it : v[u])
if(it.fi != p && !died[it.fi]) {
makedp(it.fi, u);
dp[u] = max(dp[u], dp[it.fi] + it.se);
}
}
vector <pair<ll, int>> vec;
void go(int u, int p, ll cost = 0, int anc = -1) {
tuple <ll, int, int> best = make_tuple(-1, 0, 0);
for(auto it : v[u])
if(it.fi != p && !died[it.fi])
best = max(best, make_tuple(dp[it.fi] + it.se, it.fi, it.se));
if(get<0> (best) != -1)
go(get<1> (best), u, cost + get<2> (best), (anc == -1 ? get<1> (best) : anc));
else
vec.pb(mp(cost, anc));
for(auto it : v[u])
if(it.fi != p && !died[it.fi] && it.fi != get<1> (best))
go(it.fi, u, it.se, (anc == -1 ? it.fi : anc));
}
ll o[nax];
void solve(int u) {
makedp(u, 0);
go(u, 0);
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
ll pref = 0;
for(int i = 0; i < ss(vec); ++i) {
pref += vec[i].fi;
ans[i + 2] = max(ans[i + 2], o[u] + pref);
}
int h = 0;
while(h < ss(vec) && vec[h].se == vec[0].se)
h++;
if(h < ss(vec)) {
ll cnt = vec[h].fi + vec[0].fi;
ans[2] = max(ans[2], cnt + o[u]);
int ile = 2;
for(int i = 1; i < ss(vec); ++i) {
if(i == h) continue;
ile++;
cnt += vec[i].fi;
ans[ile] = max(ans[ile], cnt + o[u]);
}
}
vec.clear();
}
void pro(int u, int p, ll cost) {
ll k = 0;
for(auto it : v[u]) {
if(it.fi != p)
;
else
k = it.se;
}
cost -= k;
o[u] = cost;
ans[1] = max(ans[1], o[u]);
for(auto it : v[u])
if(it.fi != p)
pro(it.fi, u, cost + it.se);
}
void decomp(int u) {
nn = 0;
dfs(u, 0);
int c = daj(u, 0);
solve(c);
died[c] = 1;
for(auto it : v[c])
if(!died[it.fi])
decomp(it.fi);
}
int main() {
scanf("%d", &n);
for(int i = 1; i < n; ++i) {
scanf("%d %d %d %d", &a, &b, &c, &d);
v[a].pb(mp(b, c));
v[b].pb(mp(a, d));
all += c + d;
}
o[1] = oblicz(1, 0);
pro(1, 0, o[1]);
decomp(1);
for(int i = 1; i <= n; ++i)
ans[i] = max(ans[i], ans[i - 1]);
int q;
scanf("%d", &q);
while(q--) {
scanf("%d", &a);
printf("%lld\n", all - ans[a]);
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:138:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
designated_cities.cpp:140:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d", &a, &b, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:155:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
designated_cities.cpp:157:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
4988 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5116 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
974 ms |
21032 KB |
Output is correct |
3 |
Correct |
1205 ms |
31224 KB |
Output is correct |
4 |
Correct |
846 ms |
21860 KB |
Output is correct |
5 |
Correct |
374 ms |
21196 KB |
Output is correct |
6 |
Correct |
941 ms |
22572 KB |
Output is correct |
7 |
Correct |
290 ms |
21824 KB |
Output is correct |
8 |
Correct |
1170 ms |
31076 KB |
Output is correct |
9 |
Correct |
249 ms |
23388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4988 KB |
Output is correct |
2 |
Correct |
778 ms |
21156 KB |
Output is correct |
3 |
Correct |
1188 ms |
31196 KB |
Output is correct |
4 |
Correct |
809 ms |
21860 KB |
Output is correct |
5 |
Correct |
379 ms |
21220 KB |
Output is correct |
6 |
Correct |
1002 ms |
23044 KB |
Output is correct |
7 |
Correct |
267 ms |
22944 KB |
Output is correct |
8 |
Correct |
1116 ms |
28276 KB |
Output is correct |
9 |
Correct |
252 ms |
23388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
4988 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5116 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
10 ms |
5240 KB |
Output is correct |
14 |
Correct |
10 ms |
5368 KB |
Output is correct |
15 |
Correct |
10 ms |
5240 KB |
Output is correct |
16 |
Correct |
10 ms |
5368 KB |
Output is correct |
17 |
Correct |
9 ms |
5368 KB |
Output is correct |
18 |
Correct |
10 ms |
5368 KB |
Output is correct |
19 |
Correct |
10 ms |
5240 KB |
Output is correct |
20 |
Correct |
9 ms |
5368 KB |
Output is correct |
21 |
Correct |
10 ms |
5368 KB |
Output is correct |
22 |
Correct |
10 ms |
5368 KB |
Output is correct |
23 |
Correct |
10 ms |
5368 KB |
Output is correct |
24 |
Correct |
8 ms |
5368 KB |
Output is correct |
25 |
Correct |
10 ms |
5368 KB |
Output is correct |
26 |
Correct |
8 ms |
5368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
974 ms |
21032 KB |
Output is correct |
3 |
Correct |
1205 ms |
31224 KB |
Output is correct |
4 |
Correct |
846 ms |
21860 KB |
Output is correct |
5 |
Correct |
374 ms |
21196 KB |
Output is correct |
6 |
Correct |
941 ms |
22572 KB |
Output is correct |
7 |
Correct |
290 ms |
21824 KB |
Output is correct |
8 |
Correct |
1170 ms |
31076 KB |
Output is correct |
9 |
Correct |
249 ms |
23388 KB |
Output is correct |
10 |
Correct |
6 ms |
4988 KB |
Output is correct |
11 |
Correct |
778 ms |
21156 KB |
Output is correct |
12 |
Correct |
1188 ms |
31196 KB |
Output is correct |
13 |
Correct |
809 ms |
21860 KB |
Output is correct |
14 |
Correct |
379 ms |
21220 KB |
Output is correct |
15 |
Correct |
1002 ms |
23044 KB |
Output is correct |
16 |
Correct |
267 ms |
22944 KB |
Output is correct |
17 |
Correct |
1116 ms |
28276 KB |
Output is correct |
18 |
Correct |
252 ms |
23388 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
759 ms |
21132 KB |
Output is correct |
21 |
Correct |
1167 ms |
31036 KB |
Output is correct |
22 |
Correct |
844 ms |
24196 KB |
Output is correct |
23 |
Correct |
812 ms |
21196 KB |
Output is correct |
24 |
Correct |
806 ms |
21068 KB |
Output is correct |
25 |
Correct |
816 ms |
21152 KB |
Output is correct |
26 |
Correct |
775 ms |
21096 KB |
Output is correct |
27 |
Correct |
388 ms |
21124 KB |
Output is correct |
28 |
Correct |
978 ms |
22784 KB |
Output is correct |
29 |
Correct |
765 ms |
21076 KB |
Output is correct |
30 |
Correct |
667 ms |
21480 KB |
Output is correct |
31 |
Correct |
302 ms |
22084 KB |
Output is correct |
32 |
Correct |
1298 ms |
28524 KB |
Output is correct |
33 |
Correct |
251 ms |
23388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
4988 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5116 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
974 ms |
21032 KB |
Output is correct |
14 |
Correct |
1205 ms |
31224 KB |
Output is correct |
15 |
Correct |
846 ms |
21860 KB |
Output is correct |
16 |
Correct |
374 ms |
21196 KB |
Output is correct |
17 |
Correct |
941 ms |
22572 KB |
Output is correct |
18 |
Correct |
290 ms |
21824 KB |
Output is correct |
19 |
Correct |
1170 ms |
31076 KB |
Output is correct |
20 |
Correct |
249 ms |
23388 KB |
Output is correct |
21 |
Correct |
6 ms |
4988 KB |
Output is correct |
22 |
Correct |
778 ms |
21156 KB |
Output is correct |
23 |
Correct |
1188 ms |
31196 KB |
Output is correct |
24 |
Correct |
809 ms |
21860 KB |
Output is correct |
25 |
Correct |
379 ms |
21220 KB |
Output is correct |
26 |
Correct |
1002 ms |
23044 KB |
Output is correct |
27 |
Correct |
267 ms |
22944 KB |
Output is correct |
28 |
Correct |
1116 ms |
28276 KB |
Output is correct |
29 |
Correct |
252 ms |
23388 KB |
Output is correct |
30 |
Correct |
6 ms |
5112 KB |
Output is correct |
31 |
Correct |
10 ms |
5240 KB |
Output is correct |
32 |
Correct |
10 ms |
5368 KB |
Output is correct |
33 |
Correct |
10 ms |
5240 KB |
Output is correct |
34 |
Correct |
10 ms |
5368 KB |
Output is correct |
35 |
Correct |
9 ms |
5368 KB |
Output is correct |
36 |
Correct |
10 ms |
5368 KB |
Output is correct |
37 |
Correct |
10 ms |
5240 KB |
Output is correct |
38 |
Correct |
9 ms |
5368 KB |
Output is correct |
39 |
Correct |
10 ms |
5368 KB |
Output is correct |
40 |
Correct |
10 ms |
5368 KB |
Output is correct |
41 |
Correct |
10 ms |
5368 KB |
Output is correct |
42 |
Correct |
8 ms |
5368 KB |
Output is correct |
43 |
Correct |
10 ms |
5368 KB |
Output is correct |
44 |
Correct |
8 ms |
5368 KB |
Output is correct |
45 |
Correct |
6 ms |
5112 KB |
Output is correct |
46 |
Correct |
759 ms |
21132 KB |
Output is correct |
47 |
Correct |
1167 ms |
31036 KB |
Output is correct |
48 |
Correct |
844 ms |
24196 KB |
Output is correct |
49 |
Correct |
812 ms |
21196 KB |
Output is correct |
50 |
Correct |
806 ms |
21068 KB |
Output is correct |
51 |
Correct |
816 ms |
21152 KB |
Output is correct |
52 |
Correct |
775 ms |
21096 KB |
Output is correct |
53 |
Correct |
388 ms |
21124 KB |
Output is correct |
54 |
Correct |
978 ms |
22784 KB |
Output is correct |
55 |
Correct |
765 ms |
21076 KB |
Output is correct |
56 |
Correct |
667 ms |
21480 KB |
Output is correct |
57 |
Correct |
302 ms |
22084 KB |
Output is correct |
58 |
Correct |
1298 ms |
28524 KB |
Output is correct |
59 |
Correct |
251 ms |
23388 KB |
Output is correct |
60 |
Correct |
6 ms |
5112 KB |
Output is correct |
61 |
Correct |
833 ms |
29344 KB |
Output is correct |
62 |
Correct |
1116 ms |
38140 KB |
Output is correct |
63 |
Correct |
840 ms |
28004 KB |
Output is correct |
64 |
Correct |
823 ms |
29156 KB |
Output is correct |
65 |
Correct |
789 ms |
27736 KB |
Output is correct |
66 |
Correct |
806 ms |
29184 KB |
Output is correct |
67 |
Correct |
876 ms |
27932 KB |
Output is correct |
68 |
Correct |
424 ms |
29416 KB |
Output is correct |
69 |
Correct |
1046 ms |
30892 KB |
Output is correct |
70 |
Correct |
871 ms |
29020 KB |
Output is correct |
71 |
Correct |
785 ms |
28776 KB |
Output is correct |
72 |
Correct |
372 ms |
30684 KB |
Output is correct |
73 |
Correct |
1226 ms |
36356 KB |
Output is correct |
74 |
Correct |
305 ms |
33116 KB |
Output is correct |