// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define sz(x) int(x.size())
#define f first
#define s second
#define mp make_pair
#define pb push_back
using ll = long long;
using pi = pair<int, int>;
template<class T> using V = vector<T>;
using vl = V<ll>;
using vpi = V<pi>;
using vi = V<int>;
using E = array<ll, 3>;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N;
V<V<E>> adj(N); vi deg(N); ll W = 0;
for(int i = 0; i < N-1; i++) {
int u, v, w, wi; cin >> u >> v >> w >> wi; --u, --v;
adj[u].pb({v, w, wi});
adj[v].pb({u, wi, w});
deg[u]++, deg[v]++;
}
vl F(N);
function<void(int, int)> dfs = [&](int u, int p) {
for(auto& e : adj[u]) {
auto [v, tw, fw] = e; if (v == p) continue;
W += tw; F[v] = F[u] + fw - tw;
dfs(v, u);
}
};
dfs(0, -1);
vl ans(N+1); ans[1] = W + *min_element(begin(F), end(F));
vi dead(N); auto get = [&](int x) {
for(auto& e : adj[x]) {
if (!dead[e[0]]) return mp(e[2], e[0]);
}
return mp(-1LL, -1LL);
};
priority_queue<pi, vpi, greater<pi>> q;
for(int i = 0; i < N; i++) if (deg[i] == 1) {
q.push(mp(get(i).f, i));
}
while(sz(q) > 2) {
int u, w; tie(w, u) = q.top(); q.pop();
// cout << u << " " << w << endl;
dead[u] = 1;
int p = get(u).s; deg[p]--;
if (deg[p] == 1) q.push(mp(get(p).f + w, p));
else {
// cout << sz(q) << " " << w << endl;
ans[sz(q)] = ans[sz(q) + 1] + w;
// cout << ans[sz(q)] << " <= " << ans[sz(q) + 1] << endl;
}
}
int Q; cin >> Q;
for(int i = 0; i < Q; i++) {
int e; cin >> e;
cout << ans[e] << nl;
}
exit(0-0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
193 ms |
30444 KB |
Output is correct |
3 |
Correct |
137 ms |
44624 KB |
Output is correct |
4 |
Correct |
174 ms |
29072 KB |
Output is correct |
5 |
Correct |
210 ms |
30728 KB |
Output is correct |
6 |
Correct |
208 ms |
32584 KB |
Output is correct |
7 |
Correct |
158 ms |
29952 KB |
Output is correct |
8 |
Correct |
148 ms |
45244 KB |
Output is correct |
9 |
Correct |
138 ms |
30976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
172 ms |
30276 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
193 ms |
30444 KB |
Output is correct |
3 |
Correct |
137 ms |
44624 KB |
Output is correct |
4 |
Correct |
174 ms |
29072 KB |
Output is correct |
5 |
Correct |
210 ms |
30728 KB |
Output is correct |
6 |
Correct |
208 ms |
32584 KB |
Output is correct |
7 |
Correct |
158 ms |
29952 KB |
Output is correct |
8 |
Correct |
148 ms |
45244 KB |
Output is correct |
9 |
Correct |
138 ms |
30976 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
172 ms |
30276 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
193 ms |
30444 KB |
Output is correct |
14 |
Correct |
137 ms |
44624 KB |
Output is correct |
15 |
Correct |
174 ms |
29072 KB |
Output is correct |
16 |
Correct |
210 ms |
30728 KB |
Output is correct |
17 |
Correct |
208 ms |
32584 KB |
Output is correct |
18 |
Correct |
158 ms |
29952 KB |
Output is correct |
19 |
Correct |
148 ms |
45244 KB |
Output is correct |
20 |
Correct |
138 ms |
30976 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Incorrect |
172 ms |
30276 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |