이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
typedef long long ll;
struct Edge {
ll v, w, i;
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n;
std::cin >> n;
std::vector<std::vector<Edge>> adj(n);
std::vector<ll> weights(n - 1);
for (ll i = 0, u, v, w; i < n - 1; ++i) {
std::cin >> u >> v >> w;
adj[u].push_back({v, w, i});
adj[v].push_back({u, w, i});
weights[i] = w;
}
ll q;
std::cin >> q;
while (q--) {
std::vector<ll> nodes(5);
for (auto &i : nodes) {
std::cin >> i;
}
std::set<ll> st;
for (ll i = 0; i < 5; ++i) {
std::vector<ll> depth(n);
std::vector<std::pair<ll, ll>> up(n);
std::function<void(ll, ll)> dfs;
dfs = [&](ll node, ll par) {
for (auto &[i, w, idx] : adj[node]) {
if (i == par) {
continue;
}
depth[i] = depth[node] + 1;
up[i] = {node, idx};
dfs(i, node);
}
};
dfs(nodes[i], -1);
for (ll j = i + 1; j < 5; ++j) {
ll node = nodes[j];
while (node != nodes[i]) {
st.insert(up[node].second);
node = up[node].first;
}
}
}
ll ans = 0;
for (auto &i : st) {
ans += weights[i];
}
std::cout << 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... |