//https://oj.uz/problem/view/JOI19_designated_cities
#include<bits/stdc++.h>
#define int long long
const int N = 2e5 + 5;
const long long inf = 1e18;
using namespace std;
typedef pair <long long, long long> ii;
typedef pair <int, ii> iii;
vector <iii> G[N];
ii trace[N][3], it[N << 2], root;
int lazy[N << 2];
int n, q, d[N], f[N], p[N], st[N], en[N], node[N], cnt, ans[N];
long long dp[N][4];
bool dead[N];
bool up(long long&a, long long b){
if (a <= b) return false;
a = b;
return true;
}
void dfs(int u, int p){
dp[u][0] = 0; dp[u][1] = dp[u][2] = dp[u][3] = inf;
for (auto P : G[u]){
int v = P.first, a = P.second.first, b = P.second.second;
if (v == p) continue;
dfs(v, u);
dp[u][3] = min(dp[u][3] + dp[v][0] + a, dp[u][0] + dp[v][3] + b);
dp[u][2] += dp[v][0] + a;
if (up(dp[u][2], dp[u][1] + dp[v][1])) trace[u][2] = ii(trace[u][1].first, trace[v][1].first);
if (up(dp[u][2], dp[u][0] + dp[v][2] + b)) trace[u][2] = trace[v][2];
dp[u][1] += dp[v][0] + a;
if (up(dp[u][1], dp[u][0] + dp[v][1])) trace[u][1] = trace[v][1];
dp[u][0] += dp[v][0] + a;
}
dp[u][3] = min(dp[u][3], dp[u][0]);
if (up(dp[u][2], dp[u][1])) trace[u][2] = ii(u, trace[u][1].first);
if (up(dp[u][1], dp[u][0])) trace[u][1] = ii(u, 0);
}
void redfs(int u){
st[u] = ++cnt; node[cnt] = u;
for (auto P : G[u]){
int v = P.first, a = P.second.first, b = P.second.second;
if (v == p[u]) continue;
d[v] = d[u] + a;
p[v] = u; f[v] = a;
redfs(v);
}
en[u] = cnt;
}
void init(int i, int l, int r){
if (l == r){
it[i] = ii(d[node[l]], node[l]);
return;
}
int mid = (l + r) >> 1;
init(i << 1, l, mid); init(i << 1 | 1, mid+1, r);
it[i] = max(it[i << 1], it[i << 1 | 1]);
}
void dolazy(int i, int l, int r){
if (!lazy[i]) return;
if (l != r){
lazy[i << 1] += lazy[i];
lazy[i << 1 | 1] += lazy[i];
}
it[i].first -= lazy[i];
lazy[i] = 0;
}
void update(int i, int l, int r, int L, int R, int val){
dolazy(i, l, r);
if (L > r || l > R) return;
if (L <= l && r <= R){
lazy[i] = val;
dolazy(i, l, r);
return;
}
int mid = (l + r) >> 1;
update(i << 1, l, mid, L, R, val); update(i << 1 | 1, mid+1, r, L, R, val);
it[i] = max(it[i << 1], it[i << 1 | 1]);
}
void Erase(int u){
int cur = u;
while (u && dead[u] == false){
dead[u] = true;
update(1, 1, n, st[u], en[u], f[u]);
u = p[u];
}
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 2; i <= n; i++){
int u, v, a, b; cin >> u >> v >> a >> b;
G[u].push_back(iii(v, ii(a, b)));
G[v].push_back(iii(u, ii(b, a)));
}
dfs(1, 1);
root = trace[1][2]; ans[1] = dp[1][3]; ans[2] = dp[1][2];
redfs(root.first);
init(1, 1, n);
Erase(root.second);
for (int i = 3; i <= n; i++){
ans[i] = ans[i-1] - it[1].first;
Erase(it[1].second);
}
cin >> q;
while (q--){
int x;
cin >> x;
cout << ans[x] << "\n";
}
}
Compilation message
designated_cities.cpp: In function 'void redfs(long long int)':
designated_cities.cpp:48:46: warning: unused variable 'b' [-Wunused-variable]
int v = P.first, a = P.second.first, b = P.second.second;
^
designated_cities.cpp: In function 'void Erase(long long int)':
designated_cities.cpp:91:9: warning: unused variable 'cur' [-Wunused-variable]
int cur = u;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
743 ms |
57272 KB |
Output is correct |
3 |
Correct |
652 ms |
73928 KB |
Output is correct |
4 |
Correct |
622 ms |
61176 KB |
Output is correct |
5 |
Correct |
704 ms |
60852 KB |
Output is correct |
6 |
Correct |
677 ms |
63024 KB |
Output is correct |
7 |
Correct |
652 ms |
59940 KB |
Output is correct |
8 |
Correct |
692 ms |
74308 KB |
Output is correct |
9 |
Correct |
597 ms |
58884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
734 ms |
57408 KB |
Output is correct |
3 |
Correct |
668 ms |
75904 KB |
Output is correct |
4 |
Correct |
630 ms |
61192 KB |
Output is correct |
5 |
Correct |
711 ms |
60904 KB |
Output is correct |
6 |
Correct |
794 ms |
63224 KB |
Output is correct |
7 |
Correct |
631 ms |
59160 KB |
Output is correct |
8 |
Correct |
716 ms |
69624 KB |
Output is correct |
9 |
Correct |
576 ms |
58908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
11 ms |
5632 KB |
Output is correct |
14 |
Correct |
9 ms |
5760 KB |
Output is correct |
15 |
Correct |
11 ms |
5632 KB |
Output is correct |
16 |
Correct |
11 ms |
5624 KB |
Output is correct |
17 |
Correct |
12 ms |
5632 KB |
Output is correct |
18 |
Correct |
13 ms |
5760 KB |
Output is correct |
19 |
Correct |
10 ms |
5632 KB |
Output is correct |
20 |
Correct |
13 ms |
5804 KB |
Output is correct |
21 |
Correct |
13 ms |
5760 KB |
Output is correct |
22 |
Correct |
11 ms |
5760 KB |
Output is correct |
23 |
Correct |
11 ms |
5760 KB |
Output is correct |
24 |
Correct |
10 ms |
5752 KB |
Output is correct |
25 |
Correct |
11 ms |
5888 KB |
Output is correct |
26 |
Correct |
11 ms |
5760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
743 ms |
57272 KB |
Output is correct |
3 |
Correct |
652 ms |
73928 KB |
Output is correct |
4 |
Correct |
622 ms |
61176 KB |
Output is correct |
5 |
Correct |
704 ms |
60852 KB |
Output is correct |
6 |
Correct |
677 ms |
63024 KB |
Output is correct |
7 |
Correct |
652 ms |
59940 KB |
Output is correct |
8 |
Correct |
692 ms |
74308 KB |
Output is correct |
9 |
Correct |
597 ms |
58884 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
734 ms |
57408 KB |
Output is correct |
12 |
Correct |
668 ms |
75904 KB |
Output is correct |
13 |
Correct |
630 ms |
61192 KB |
Output is correct |
14 |
Correct |
711 ms |
60904 KB |
Output is correct |
15 |
Correct |
794 ms |
63224 KB |
Output is correct |
16 |
Correct |
631 ms |
59160 KB |
Output is correct |
17 |
Correct |
716 ms |
69624 KB |
Output is correct |
18 |
Correct |
576 ms |
58908 KB |
Output is correct |
19 |
Correct |
7 ms |
5092 KB |
Output is correct |
20 |
Correct |
613 ms |
61152 KB |
Output is correct |
21 |
Correct |
648 ms |
76408 KB |
Output is correct |
22 |
Correct |
648 ms |
61312 KB |
Output is correct |
23 |
Correct |
727 ms |
61512 KB |
Output is correct |
24 |
Correct |
667 ms |
61692 KB |
Output is correct |
25 |
Correct |
671 ms |
61812 KB |
Output is correct |
26 |
Correct |
698 ms |
61596 KB |
Output is correct |
27 |
Correct |
719 ms |
61224 KB |
Output is correct |
28 |
Correct |
712 ms |
63224 KB |
Output is correct |
29 |
Correct |
670 ms |
61932 KB |
Output is correct |
30 |
Correct |
691 ms |
60948 KB |
Output is correct |
31 |
Correct |
636 ms |
59912 KB |
Output is correct |
32 |
Correct |
764 ms |
70452 KB |
Output is correct |
33 |
Correct |
651 ms |
59160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
743 ms |
57272 KB |
Output is correct |
14 |
Correct |
652 ms |
73928 KB |
Output is correct |
15 |
Correct |
622 ms |
61176 KB |
Output is correct |
16 |
Correct |
704 ms |
60852 KB |
Output is correct |
17 |
Correct |
677 ms |
63024 KB |
Output is correct |
18 |
Correct |
652 ms |
59940 KB |
Output is correct |
19 |
Correct |
692 ms |
74308 KB |
Output is correct |
20 |
Correct |
597 ms |
58884 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
734 ms |
57408 KB |
Output is correct |
23 |
Correct |
668 ms |
75904 KB |
Output is correct |
24 |
Correct |
630 ms |
61192 KB |
Output is correct |
25 |
Correct |
711 ms |
60904 KB |
Output is correct |
26 |
Correct |
794 ms |
63224 KB |
Output is correct |
27 |
Correct |
631 ms |
59160 KB |
Output is correct |
28 |
Correct |
716 ms |
69624 KB |
Output is correct |
29 |
Correct |
576 ms |
58908 KB |
Output is correct |
30 |
Correct |
7 ms |
5120 KB |
Output is correct |
31 |
Correct |
11 ms |
5632 KB |
Output is correct |
32 |
Correct |
9 ms |
5760 KB |
Output is correct |
33 |
Correct |
11 ms |
5632 KB |
Output is correct |
34 |
Correct |
11 ms |
5624 KB |
Output is correct |
35 |
Correct |
12 ms |
5632 KB |
Output is correct |
36 |
Correct |
13 ms |
5760 KB |
Output is correct |
37 |
Correct |
10 ms |
5632 KB |
Output is correct |
38 |
Correct |
13 ms |
5804 KB |
Output is correct |
39 |
Correct |
13 ms |
5760 KB |
Output is correct |
40 |
Correct |
11 ms |
5760 KB |
Output is correct |
41 |
Correct |
11 ms |
5760 KB |
Output is correct |
42 |
Correct |
10 ms |
5752 KB |
Output is correct |
43 |
Correct |
11 ms |
5888 KB |
Output is correct |
44 |
Correct |
11 ms |
5760 KB |
Output is correct |
45 |
Correct |
7 ms |
5092 KB |
Output is correct |
46 |
Correct |
613 ms |
61152 KB |
Output is correct |
47 |
Correct |
648 ms |
76408 KB |
Output is correct |
48 |
Correct |
648 ms |
61312 KB |
Output is correct |
49 |
Correct |
727 ms |
61512 KB |
Output is correct |
50 |
Correct |
667 ms |
61692 KB |
Output is correct |
51 |
Correct |
671 ms |
61812 KB |
Output is correct |
52 |
Correct |
698 ms |
61596 KB |
Output is correct |
53 |
Correct |
719 ms |
61224 KB |
Output is correct |
54 |
Correct |
712 ms |
63224 KB |
Output is correct |
55 |
Correct |
670 ms |
61932 KB |
Output is correct |
56 |
Correct |
691 ms |
60948 KB |
Output is correct |
57 |
Correct |
636 ms |
59912 KB |
Output is correct |
58 |
Correct |
764 ms |
70452 KB |
Output is correct |
59 |
Correct |
651 ms |
59160 KB |
Output is correct |
60 |
Correct |
7 ms |
5120 KB |
Output is correct |
61 |
Correct |
777 ms |
62624 KB |
Output is correct |
62 |
Correct |
667 ms |
74460 KB |
Output is correct |
63 |
Correct |
718 ms |
62760 KB |
Output is correct |
64 |
Correct |
704 ms |
63040 KB |
Output is correct |
65 |
Correct |
727 ms |
62840 KB |
Output is correct |
66 |
Correct |
781 ms |
62976 KB |
Output is correct |
67 |
Correct |
766 ms |
62828 KB |
Output is correct |
68 |
Correct |
728 ms |
63056 KB |
Output is correct |
69 |
Correct |
725 ms |
64640 KB |
Output is correct |
70 |
Correct |
603 ms |
63196 KB |
Output is correct |
71 |
Correct |
678 ms |
62740 KB |
Output is correct |
72 |
Correct |
690 ms |
62244 KB |
Output is correct |
73 |
Correct |
739 ms |
70432 KB |
Output is correct |
74 |
Correct |
679 ms |
62196 KB |
Output is correct |