#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
const ll maxn = 5e4 + 10;
unordered_map<ll,ll> adj[maxn];
bool fd(ll x, array<ll,4> sch){
for (ll i = 0;i<4;i++){
if (sch[i] == x) return true;
}
return false;
}
ll dfs(ll x, ll par, array<ll,4> sch){
bool req = false;
ll cost = 0;
// cout<<x<<' '<<fd(x,sch)<<"sch"<<endl;
if (fd(x, sch)){
req = true;
}
for (auto i:adj[x]){
if (i.fi == par) continue;
// cout<<x<<' '<<i.fi<<' '<<i.se<<endl;
ll tcost = dfs(i.fi, x, sch);
if (tcost != -1){
cost += i.se + tcost;
req = true;
}
}
return req ? cost : -1;
}
signed main(){
ll n;cin>>n;
for (ll i = 0;i<n-1;i++){
ll u,v,w;cin>>u>>v>>w;
adj[u][v] = w;
adj[v][u] = w;
}
ll q;cin>>q;
for (ll i = 0;i<q;i++){
ll a;
array<ll,4> sch;
cin>>a;
for (ll j = 0;j<4;j++){
cin>>sch[j];
}
cout<<dfs(a, -1, sch)<<endl;
}
}
# | 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... |