This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define all(x) x.begin(), x.end()
#define pb push_back
const char nd = '\n';
struct edge {
ll v, w;
};
vector<vector<edge>> graph;
bitset<50001> visto;
ll res = 0;
ll dfs(ll u, ll p, ll cup) {
ll cdown = 0, temp;
if (visto[u]) cup++, cdown++;
for (auto v : graph[u]) {
if (v.v == p) continue;
temp = dfs(v.v, u, cup);
if (cup && temp) res += v.w;
cdown += temp;
}
return cdown;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//
ll n;
cin >> n;
graph.assign(n + 1, vector<edge>());
ll u, v, w;
for (ll i = 1; i < n; i++) {
cin >> u >> v >> w;
graph[u].pb({v, w});
graph[v].pb({u, w});
}
ll q;
cin >> q;
vl a(5);
while (q--) {
for (auto &v : a) cin >> v, visto[v] = true;
res = 0;
dfs(0, -1, 0);
for (auto &v : a) visto[v] = false;
cout << res << nd;
}
}
# | 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... |