#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
struct edge {
int u, f, b;
};
const int MN = 200005;
int n, q, u, v, w, x, cost, ans[MN];
vector<edge> a[MN];
void dfs1(int cur, int par) {
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
cost += nxt.f;
dfs1(nxt.u, cur);
}
}
void dfs2(int cur, int par) {
ans[1] = min(ans[1], cost);
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
cost += nxt.b - nxt.f;
dfs2(nxt.u, cur);
cost += nxt.f - nxt.b;
}
}
void solve1() {
dfs1(1, 0);
dfs2(1, 0);
}
int32_t main() {
boost();
memset(ans, 0x3f, sizeof(ans));
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v >> w >> x;
a[u].push_back({v, w, x});
a[v].push_back({u, x, w});
}
solve1();
cin >> q;
while (q--) {
cin >> x;
printf("%lld\n", ans[x]);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6476 KB |
Output is correct |
2 |
Correct |
223 ms |
25964 KB |
Output is correct |
3 |
Correct |
239 ms |
36312 KB |
Output is correct |
4 |
Correct |
178 ms |
24592 KB |
Output is correct |
5 |
Correct |
178 ms |
25888 KB |
Output is correct |
6 |
Correct |
208 ms |
27668 KB |
Output is correct |
7 |
Correct |
166 ms |
24780 KB |
Output is correct |
8 |
Correct |
245 ms |
36756 KB |
Output is correct |
9 |
Correct |
120 ms |
23924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6476 KB |
Output is correct |
2 |
Correct |
223 ms |
25964 KB |
Output is correct |
3 |
Correct |
239 ms |
36312 KB |
Output is correct |
4 |
Correct |
178 ms |
24592 KB |
Output is correct |
5 |
Correct |
178 ms |
25888 KB |
Output is correct |
6 |
Correct |
208 ms |
27668 KB |
Output is correct |
7 |
Correct |
166 ms |
24780 KB |
Output is correct |
8 |
Correct |
245 ms |
36756 KB |
Output is correct |
9 |
Correct |
120 ms |
23924 KB |
Output is correct |
10 |
Incorrect |
3 ms |
6476 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |