#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
struct Edge{
ll v, a, b;
};
vector<Edge> T[200005], C[200005];
ll N, Q, Sum = 0, Ans[200005], Val[200005], P[200005];
pair<ll, ll> dp[200005];
void cal1(ll u, ll p){
for(auto e : T[u]){
if(e.v == p) continue;
Val[1] += e.b;
cal1(e.v, u);
}
}
void cal2(ll u, ll p){
for(auto e : T[u]){
if(e.v == p) continue;
Val[e.v] = Val[u] - e.b + e.a;
cal2(e.v, u);
}
}
ll mxD = 0, U = 1;
void Dia(ll u, ll p, ll d){
if(mxD < d + Val[u]){
mxD = d + Val[u], U = u;
}
for(auto e : T[u]){
if(e.v == p) continue;
Dia(e.v, u, d + e.a + e.b);
}
}
ll F = 0;
void cal3(ll u, ll p){
P[u] = p, dp[u] = {0, u};
for(auto e : T[u]){
if(e.v == p) continue;
cal3(e.v, u);
dp[u] = max(dp[u], {dp[e.v].x + e.a, dp[e.v].y});
F += e.b;
}
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> N;
for(ll i = 1; i < N; i++){
ll u, v, a, b; cin >> u >> v >> a >> b;
T[u].push_back({v, a, b}); T[v].push_back({u, b, a});
Sum += a + b;
}
cal1(1, -1); cal2(1, -1);
Ans[1] = Sum - *max_element(Val + 1, Val + N + 1);
Dia(1, -1, 0);
cal3(U, -1);
priority_queue<pair<ll, ll>> PQ;
PQ.push({dp[U].x, U});
for(ll i = 2; i <= N; i++){
if(i >= 3) Ans[i] = Ans[i - 1];
if(PQ.empty()) continue;
Ans[i] += PQ.top().x;
ll r = PQ.top().y; PQ.pop();
ll u = dp[r].y, pv = 0;
while(1){
for(auto e : T[u]){
if(e.v == P[u] || e.v == pv) continue;
PQ.push({e.a + dp[e.v].x, e.v});
}
if(u == r) break;
pv = u, u = P[u];
}
}
cin >> Q;
while(Q--){
ll t; cin >> t;
cout << (t == 1 ? Ans[t] : Sum - Ans[t] - F) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9700 KB |
Output is correct |
3 |
Correct |
4 ms |
9876 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9816 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
158 ms |
38036 KB |
Output is correct |
3 |
Correct |
234 ms |
62036 KB |
Output is correct |
4 |
Correct |
144 ms |
35664 KB |
Output is correct |
5 |
Correct |
143 ms |
38912 KB |
Output is correct |
6 |
Correct |
163 ms |
41420 KB |
Output is correct |
7 |
Correct |
141 ms |
39680 KB |
Output is correct |
8 |
Correct |
201 ms |
61204 KB |
Output is correct |
9 |
Correct |
155 ms |
42548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
159 ms |
37972 KB |
Output is correct |
3 |
Correct |
214 ms |
62032 KB |
Output is correct |
4 |
Correct |
146 ms |
35668 KB |
Output is correct |
5 |
Correct |
139 ms |
38756 KB |
Output is correct |
6 |
Correct |
158 ms |
42392 KB |
Output is correct |
7 |
Correct |
125 ms |
42040 KB |
Output is correct |
8 |
Correct |
209 ms |
54004 KB |
Output is correct |
9 |
Correct |
128 ms |
41816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9700 KB |
Output is correct |
3 |
Correct |
4 ms |
9876 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9816 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9816 KB |
Output is correct |
13 |
Correct |
5 ms |
10208 KB |
Output is correct |
14 |
Correct |
5 ms |
10332 KB |
Output is correct |
15 |
Correct |
5 ms |
10076 KB |
Output is correct |
16 |
Correct |
5 ms |
10072 KB |
Output is correct |
17 |
Correct |
5 ms |
10076 KB |
Output is correct |
18 |
Correct |
6 ms |
10012 KB |
Output is correct |
19 |
Correct |
5 ms |
10072 KB |
Output is correct |
20 |
Correct |
5 ms |
10076 KB |
Output is correct |
21 |
Correct |
6 ms |
10076 KB |
Output is correct |
22 |
Correct |
5 ms |
10076 KB |
Output is correct |
23 |
Correct |
6 ms |
10128 KB |
Output is correct |
24 |
Correct |
5 ms |
10076 KB |
Output is correct |
25 |
Correct |
5 ms |
10340 KB |
Output is correct |
26 |
Correct |
5 ms |
10072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
158 ms |
38036 KB |
Output is correct |
3 |
Correct |
234 ms |
62036 KB |
Output is correct |
4 |
Correct |
144 ms |
35664 KB |
Output is correct |
5 |
Correct |
143 ms |
38912 KB |
Output is correct |
6 |
Correct |
163 ms |
41420 KB |
Output is correct |
7 |
Correct |
141 ms |
39680 KB |
Output is correct |
8 |
Correct |
201 ms |
61204 KB |
Output is correct |
9 |
Correct |
155 ms |
42548 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
159 ms |
37972 KB |
Output is correct |
12 |
Correct |
214 ms |
62032 KB |
Output is correct |
13 |
Correct |
146 ms |
35668 KB |
Output is correct |
14 |
Correct |
139 ms |
38756 KB |
Output is correct |
15 |
Correct |
158 ms |
42392 KB |
Output is correct |
16 |
Correct |
125 ms |
42040 KB |
Output is correct |
17 |
Correct |
209 ms |
54004 KB |
Output is correct |
18 |
Correct |
128 ms |
41816 KB |
Output is correct |
19 |
Correct |
7 ms |
9820 KB |
Output is correct |
20 |
Correct |
152 ms |
38096 KB |
Output is correct |
21 |
Correct |
224 ms |
62068 KB |
Output is correct |
22 |
Correct |
146 ms |
35780 KB |
Output is correct |
23 |
Correct |
164 ms |
38352 KB |
Output is correct |
24 |
Correct |
168 ms |
37216 KB |
Output is correct |
25 |
Correct |
152 ms |
38344 KB |
Output is correct |
26 |
Correct |
145 ms |
37060 KB |
Output is correct |
27 |
Correct |
143 ms |
38328 KB |
Output is correct |
28 |
Correct |
162 ms |
41676 KB |
Output is correct |
29 |
Correct |
161 ms |
38740 KB |
Output is correct |
30 |
Correct |
159 ms |
37636 KB |
Output is correct |
31 |
Correct |
143 ms |
39676 KB |
Output is correct |
32 |
Correct |
185 ms |
54980 KB |
Output is correct |
33 |
Correct |
134 ms |
42268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9700 KB |
Output is correct |
3 |
Correct |
4 ms |
9876 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9816 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
5 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9816 KB |
Output is correct |
13 |
Correct |
158 ms |
38036 KB |
Output is correct |
14 |
Correct |
234 ms |
62036 KB |
Output is correct |
15 |
Correct |
144 ms |
35664 KB |
Output is correct |
16 |
Correct |
143 ms |
38912 KB |
Output is correct |
17 |
Correct |
163 ms |
41420 KB |
Output is correct |
18 |
Correct |
141 ms |
39680 KB |
Output is correct |
19 |
Correct |
201 ms |
61204 KB |
Output is correct |
20 |
Correct |
155 ms |
42548 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
159 ms |
37972 KB |
Output is correct |
23 |
Correct |
214 ms |
62032 KB |
Output is correct |
24 |
Correct |
146 ms |
35668 KB |
Output is correct |
25 |
Correct |
139 ms |
38756 KB |
Output is correct |
26 |
Correct |
158 ms |
42392 KB |
Output is correct |
27 |
Correct |
125 ms |
42040 KB |
Output is correct |
28 |
Correct |
209 ms |
54004 KB |
Output is correct |
29 |
Correct |
128 ms |
41816 KB |
Output is correct |
30 |
Correct |
4 ms |
9816 KB |
Output is correct |
31 |
Correct |
5 ms |
10208 KB |
Output is correct |
32 |
Correct |
5 ms |
10332 KB |
Output is correct |
33 |
Correct |
5 ms |
10076 KB |
Output is correct |
34 |
Correct |
5 ms |
10072 KB |
Output is correct |
35 |
Correct |
5 ms |
10076 KB |
Output is correct |
36 |
Correct |
6 ms |
10012 KB |
Output is correct |
37 |
Correct |
5 ms |
10072 KB |
Output is correct |
38 |
Correct |
5 ms |
10076 KB |
Output is correct |
39 |
Correct |
6 ms |
10076 KB |
Output is correct |
40 |
Correct |
5 ms |
10076 KB |
Output is correct |
41 |
Correct |
6 ms |
10128 KB |
Output is correct |
42 |
Correct |
5 ms |
10076 KB |
Output is correct |
43 |
Correct |
5 ms |
10340 KB |
Output is correct |
44 |
Correct |
5 ms |
10072 KB |
Output is correct |
45 |
Correct |
7 ms |
9820 KB |
Output is correct |
46 |
Correct |
152 ms |
38096 KB |
Output is correct |
47 |
Correct |
224 ms |
62068 KB |
Output is correct |
48 |
Correct |
146 ms |
35780 KB |
Output is correct |
49 |
Correct |
164 ms |
38352 KB |
Output is correct |
50 |
Correct |
168 ms |
37216 KB |
Output is correct |
51 |
Correct |
152 ms |
38344 KB |
Output is correct |
52 |
Correct |
145 ms |
37060 KB |
Output is correct |
53 |
Correct |
143 ms |
38328 KB |
Output is correct |
54 |
Correct |
162 ms |
41676 KB |
Output is correct |
55 |
Correct |
161 ms |
38740 KB |
Output is correct |
56 |
Correct |
159 ms |
37636 KB |
Output is correct |
57 |
Correct |
143 ms |
39676 KB |
Output is correct |
58 |
Correct |
185 ms |
54980 KB |
Output is correct |
59 |
Correct |
134 ms |
42268 KB |
Output is correct |
60 |
Correct |
4 ms |
9820 KB |
Output is correct |
61 |
Correct |
190 ms |
40760 KB |
Output is correct |
62 |
Correct |
202 ms |
63824 KB |
Output is correct |
63 |
Correct |
159 ms |
38472 KB |
Output is correct |
64 |
Correct |
181 ms |
41140 KB |
Output is correct |
65 |
Correct |
168 ms |
39628 KB |
Output is correct |
66 |
Correct |
175 ms |
41164 KB |
Output is correct |
67 |
Correct |
185 ms |
39628 KB |
Output is correct |
68 |
Correct |
167 ms |
41224 KB |
Output is correct |
69 |
Correct |
186 ms |
44232 KB |
Output is correct |
70 |
Correct |
184 ms |
41216 KB |
Output is correct |
71 |
Correct |
182 ms |
40196 KB |
Output is correct |
72 |
Correct |
162 ms |
42976 KB |
Output is correct |
73 |
Correct |
207 ms |
57008 KB |
Output is correct |
74 |
Correct |
149 ms |
46328 KB |
Output is correct |