//#pragma GCC optomize ("Ofast")
//#pragma GCC optomize ("unroll-loops")
//#pragma GCC target ("avx,avx2,fma")
#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define int long long
#define pb push_back
#define all(x) (x.begin(),x.end())
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const ll N = 2e5+9, INF = 1e18 , inf = 1e9 , mod = 1e9+7;
vector<pair<int,int>>g[N];
int p[N];
int dep[N];
int pref[N];
bool was[N];
void dfs(int v){
was[v]=1;
for(auto to:g[v]){
if(to.F==p[v]||was[to.F])continue;
p[to.F]=v;
dep[to.F]=dep[v]+1;
pref[dep[to.F]]=pref[dep[v]]+to.S;
dfs(to.F);
}
}
signed main(){
// freopen("ladder.in","r",stdin);
// freopen("ladder.out","w",stdout);
ios;
int tt=1;
// cin>>tt;
while(tt--){
int n;
cin>>n;
int sum=0;
for(int i=1;i<n;i++){
int a,b,c;
cin>>a>>b>>c;
sum+=c;
g[a].pb({b,c});
g[b].pb({a,c});
}
int q;
cin>>q;
int mx=0;
vector<int>v;
for(int i=0;i<n;i++){
int x=g[i].size();
mx=max(mx,x);
if(x==1)v.pb(i);
}
if(n==5&&q==1){
int a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
cout<<sum;
}
else if(mx<=2){
dfs(v[0]);
for(int i=1;i<=q;i++){
int a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
int l=min({dep[a],dep[b],dep[c],dep[d],dep[e]}),r=max({dep[a],dep[b],dep[c],dep[d],dep[e]});
cout<<pref[r]-pref[l-1]<<'\n';
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
11808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
7952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
21 ms |
11808 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |