#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<std::vector<std::pair<int, std::pair<long long, long long>>>> adj(
n + 1);
long long tot = 0;
for (int i = 0, a, b, c, d; i < n - 1; ++i) {
std::cin >> a >> b >> c >> d;
adj[a].push_back({b, {c, d}});
adj[b].push_back({a, {d, c}});
tot += c + d;
}
std::vector<long long> path_up(n + 1), path_down(n + 1);
long long up = 0;
auto dfs = [&](auto &&self, int u, int p) -> void {
for (auto &[i, pr] : adj[u]) {
if (i == p) {
continue;
}
auto [c, d] = pr;
up += d;
path_up[i] = path_up[u] + d;
path_down[i] = path_down[u] + c;
self(self, i, u);
}
};
dfs(dfs, 1, 0);
long long ans = 0;
for (int i = 1; i <= n; ++i) {
ans = std::max(ans, up - path_up[i] + path_down[i]);
}
int q;
std::cin >> q;
while (q--) {
int x;
std::cout << tot - ans << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |