//https://oj.uz/problem/view/JOI19_designated_cities
#include<bits/stdc++.h>
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 n, q, f[N], p[N], st[N], en[N], node[N], cnt;
long long dp[N][4], d[N], ans[N], lazy[N << 2];
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(int)':
designated_cities.cpp:46: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(int)':
designated_cities.cpp:89:9: warning: unused variable 'cur' [-Wunused-variable]
int cur = u;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5188 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5092 KB |
Output is correct |
2 |
Correct |
648 ms |
53744 KB |
Output is correct |
3 |
Correct |
659 ms |
66496 KB |
Output is correct |
4 |
Correct |
658 ms |
53764 KB |
Output is correct |
5 |
Correct |
677 ms |
53416 KB |
Output is correct |
6 |
Correct |
688 ms |
55724 KB |
Output is correct |
7 |
Correct |
595 ms |
52516 KB |
Output is correct |
8 |
Correct |
681 ms |
67096 KB |
Output is correct |
9 |
Correct |
541 ms |
51608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5092 KB |
Output is correct |
2 |
Correct |
658 ms |
53548 KB |
Output is correct |
3 |
Correct |
678 ms |
68704 KB |
Output is correct |
4 |
Correct |
576 ms |
53720 KB |
Output is correct |
5 |
Correct |
673 ms |
53672 KB |
Output is correct |
6 |
Correct |
740 ms |
55952 KB |
Output is correct |
7 |
Correct |
607 ms |
51848 KB |
Output is correct |
8 |
Correct |
687 ms |
62328 KB |
Output is correct |
9 |
Correct |
523 ms |
51736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5188 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 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 |
12 ms |
5760 KB |
Output is correct |
15 |
Correct |
12 ms |
5640 KB |
Output is correct |
16 |
Correct |
14 ms |
5760 KB |
Output is correct |
17 |
Correct |
11 ms |
5760 KB |
Output is correct |
18 |
Correct |
11 ms |
5760 KB |
Output is correct |
19 |
Correct |
12 ms |
5760 KB |
Output is correct |
20 |
Correct |
11 ms |
5632 KB |
Output is correct |
21 |
Correct |
11 ms |
5632 KB |
Output is correct |
22 |
Correct |
13 ms |
5632 KB |
Output is correct |
23 |
Correct |
11 ms |
5632 KB |
Output is correct |
24 |
Correct |
11 ms |
5632 KB |
Output is correct |
25 |
Correct |
10 ms |
5760 KB |
Output is correct |
26 |
Correct |
12 ms |
5632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5092 KB |
Output is correct |
2 |
Correct |
648 ms |
53744 KB |
Output is correct |
3 |
Correct |
659 ms |
66496 KB |
Output is correct |
4 |
Correct |
658 ms |
53764 KB |
Output is correct |
5 |
Correct |
677 ms |
53416 KB |
Output is correct |
6 |
Correct |
688 ms |
55724 KB |
Output is correct |
7 |
Correct |
595 ms |
52516 KB |
Output is correct |
8 |
Correct |
681 ms |
67096 KB |
Output is correct |
9 |
Correct |
541 ms |
51608 KB |
Output is correct |
10 |
Correct |
6 ms |
5092 KB |
Output is correct |
11 |
Correct |
658 ms |
53548 KB |
Output is correct |
12 |
Correct |
678 ms |
68704 KB |
Output is correct |
13 |
Correct |
576 ms |
53720 KB |
Output is correct |
14 |
Correct |
673 ms |
53672 KB |
Output is correct |
15 |
Correct |
740 ms |
55952 KB |
Output is correct |
16 |
Correct |
607 ms |
51848 KB |
Output is correct |
17 |
Correct |
687 ms |
62328 KB |
Output is correct |
18 |
Correct |
523 ms |
51736 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
719 ms |
54008 KB |
Output is correct |
21 |
Correct |
656 ms |
68856 KB |
Output is correct |
22 |
Correct |
604 ms |
53692 KB |
Output is correct |
23 |
Correct |
719 ms |
54136 KB |
Output is correct |
24 |
Correct |
642 ms |
54064 KB |
Output is correct |
25 |
Correct |
646 ms |
54008 KB |
Output is correct |
26 |
Correct |
641 ms |
54008 KB |
Output is correct |
27 |
Correct |
600 ms |
53468 KB |
Output is correct |
28 |
Correct |
645 ms |
55800 KB |
Output is correct |
29 |
Correct |
658 ms |
54408 KB |
Output is correct |
30 |
Correct |
603 ms |
53372 KB |
Output is correct |
31 |
Correct |
610 ms |
52320 KB |
Output is correct |
32 |
Correct |
695 ms |
62968 KB |
Output is correct |
33 |
Correct |
651 ms |
51676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5188 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5092 KB |
Output is correct |
13 |
Correct |
648 ms |
53744 KB |
Output is correct |
14 |
Correct |
659 ms |
66496 KB |
Output is correct |
15 |
Correct |
658 ms |
53764 KB |
Output is correct |
16 |
Correct |
677 ms |
53416 KB |
Output is correct |
17 |
Correct |
688 ms |
55724 KB |
Output is correct |
18 |
Correct |
595 ms |
52516 KB |
Output is correct |
19 |
Correct |
681 ms |
67096 KB |
Output is correct |
20 |
Correct |
541 ms |
51608 KB |
Output is correct |
21 |
Correct |
6 ms |
5092 KB |
Output is correct |
22 |
Correct |
658 ms |
53548 KB |
Output is correct |
23 |
Correct |
678 ms |
68704 KB |
Output is correct |
24 |
Correct |
576 ms |
53720 KB |
Output is correct |
25 |
Correct |
673 ms |
53672 KB |
Output is correct |
26 |
Correct |
740 ms |
55952 KB |
Output is correct |
27 |
Correct |
607 ms |
51848 KB |
Output is correct |
28 |
Correct |
687 ms |
62328 KB |
Output is correct |
29 |
Correct |
523 ms |
51736 KB |
Output is correct |
30 |
Correct |
7 ms |
5120 KB |
Output is correct |
31 |
Correct |
11 ms |
5632 KB |
Output is correct |
32 |
Correct |
12 ms |
5760 KB |
Output is correct |
33 |
Correct |
12 ms |
5640 KB |
Output is correct |
34 |
Correct |
14 ms |
5760 KB |
Output is correct |
35 |
Correct |
11 ms |
5760 KB |
Output is correct |
36 |
Correct |
11 ms |
5760 KB |
Output is correct |
37 |
Correct |
12 ms |
5760 KB |
Output is correct |
38 |
Correct |
11 ms |
5632 KB |
Output is correct |
39 |
Correct |
11 ms |
5632 KB |
Output is correct |
40 |
Correct |
13 ms |
5632 KB |
Output is correct |
41 |
Correct |
11 ms |
5632 KB |
Output is correct |
42 |
Correct |
11 ms |
5632 KB |
Output is correct |
43 |
Correct |
10 ms |
5760 KB |
Output is correct |
44 |
Correct |
12 ms |
5632 KB |
Output is correct |
45 |
Correct |
7 ms |
5120 KB |
Output is correct |
46 |
Correct |
719 ms |
54008 KB |
Output is correct |
47 |
Correct |
656 ms |
68856 KB |
Output is correct |
48 |
Correct |
604 ms |
53692 KB |
Output is correct |
49 |
Correct |
719 ms |
54136 KB |
Output is correct |
50 |
Correct |
642 ms |
54064 KB |
Output is correct |
51 |
Correct |
646 ms |
54008 KB |
Output is correct |
52 |
Correct |
641 ms |
54008 KB |
Output is correct |
53 |
Correct |
600 ms |
53468 KB |
Output is correct |
54 |
Correct |
645 ms |
55800 KB |
Output is correct |
55 |
Correct |
658 ms |
54408 KB |
Output is correct |
56 |
Correct |
603 ms |
53372 KB |
Output is correct |
57 |
Correct |
610 ms |
52320 KB |
Output is correct |
58 |
Correct |
695 ms |
62968 KB |
Output is correct |
59 |
Correct |
651 ms |
51676 KB |
Output is correct |
60 |
Correct |
8 ms |
5028 KB |
Output is correct |
61 |
Correct |
707 ms |
55172 KB |
Output is correct |
62 |
Correct |
694 ms |
66936 KB |
Output is correct |
63 |
Correct |
608 ms |
55160 KB |
Output is correct |
64 |
Correct |
673 ms |
55384 KB |
Output is correct |
65 |
Correct |
703 ms |
55264 KB |
Output is correct |
66 |
Correct |
715 ms |
55544 KB |
Output is correct |
67 |
Correct |
654 ms |
55032 KB |
Output is correct |
68 |
Correct |
772 ms |
55084 KB |
Output is correct |
69 |
Correct |
737 ms |
56688 KB |
Output is correct |
70 |
Correct |
724 ms |
55544 KB |
Output is correct |
71 |
Correct |
630 ms |
54624 KB |
Output is correct |
72 |
Correct |
648 ms |
54352 KB |
Output is correct |
73 |
Correct |
710 ms |
62328 KB |
Output is correct |
74 |
Correct |
653 ms |
54388 KB |
Output is correct |