#include <bits/stdc++.h>
using namespace std;
// 123
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<vector<tuple<int, int, int>>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v, a, b;
cin >> u >> v >> a >> b;
u--, v--;
adj[u].emplace_back(v, a, b);
adj[v].emplace_back(u, b, a);
}
vector<int64_t> f(n, 0);
function<void(int, int)> dfs1 = [&](int u, int p) {
for (auto [v, a, b] : adj[u]) { // a(u->v), b(v->u)
if (v == p) continue;
dfs1(v, u);
f[u] += a + f[v];
}
};
vector<int64_t> g(n, 0);
function<void(int, int)> dfs2 = [&](int u, int p) {
for (auto [v, a, b] : adj[u]) { // a(u->v), b(v->u)
if (v == p) continue;
g[v] = g[u] - a + b;
dfs2(v, u);
}
};
dfs1(0, -1);
g[0] = f[0];
dfs2(0, -1);
int q;
cin >> q;
while (q--) {
int e;
cin >> e;
assert(e == 1);
cout << *min_element(g.begin(), g.end()) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
136 ms |
22756 KB |
Output is correct |
3 |
Correct |
194 ms |
36044 KB |
Output is correct |
4 |
Correct |
115 ms |
21404 KB |
Output is correct |
5 |
Correct |
122 ms |
22664 KB |
Output is correct |
6 |
Correct |
133 ms |
24852 KB |
Output is correct |
7 |
Correct |
118 ms |
22784 KB |
Output is correct |
8 |
Correct |
179 ms |
36764 KB |
Output is correct |
9 |
Correct |
107 ms |
23324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
136 ms |
22756 KB |
Output is correct |
3 |
Correct |
194 ms |
36044 KB |
Output is correct |
4 |
Correct |
115 ms |
21404 KB |
Output is correct |
5 |
Correct |
122 ms |
22664 KB |
Output is correct |
6 |
Correct |
133 ms |
24852 KB |
Output is correct |
7 |
Correct |
118 ms |
22784 KB |
Output is correct |
8 |
Correct |
179 ms |
36764 KB |
Output is correct |
9 |
Correct |
107 ms |
23324 KB |
Output is correct |
10 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |