#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pl = pair<ll,ll>;
#define pb push_back
#define form(m,it) for(auto it=m.begin(); it!=m.end(); it++)
#define forp(i,a,b) for(ll i=a; i<=b; i++)
#define forn(i,a,b) for(ll i=a; i>=b; i--)
#define newl '\n'
#define ff first
#define ss second
const ll mod = 1000000007;
const ll maxn = 50005;
ll n,sz,tim=0,cnt=0;
vector<pl> adj[maxn];
ll up[maxn][20];
ll tin[maxn];
ll tout[maxn];
ll order[maxn];
ll depth[maxn];
void dfs(ll i, ll pa){
order[i]=cnt++;
tin[i]=tim++;
up[i][0]=pa;
forp(j,1,sz){
up[i][j]=up[up[i][j-1]][j-1];
}
for(auto u:adj[i]){
if(u.ff!=pa){
depth[u.ff]=depth[i]+u.ss;
dfs(u.ff,i);
}
}
tout[i]=tim++;
}
bool is_anc(ll u, ll v){
return (tin[v]<=tin[u] && tout[u]<=tout[v]);
}
ll lca(ll u, ll v){
if(is_anc(u,v)){
return v;
}
if(is_anc(v,u)){
return u;
}
forn(i,sz,0){
if(!is_anc(v,up[u][i])){
u=up[u][i];
}
}
return up[u][0];
}
void solve(){
cin>>n; sz=ceil(log2(n));
forp(i,1,n-1){
ll u,v,w; cin>>u>>v>>w; u++;v++;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
dfs(1,1);
ll q; cin>>q;
forp(i,1,q){
ll a[5];
forp(j,0,4){
cin>>a[j]; a[j]++;
}
sort(a,a+5,[](ll u, ll v){
return order[u]<order[v];
});
ll sum=0;
forp(j,0,4){
sum+=(depth[a[j]]+depth[a[(j+1)%5]]-2*depth[lca(a[j],a[(j+1)%5])]);
}
cout<<sum/2<<newl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1; //cin>>t;
while(t--)solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
14512 KB |
Output is correct |
2 |
Correct |
46 ms |
15784 KB |
Output is correct |
3 |
Correct |
47 ms |
15804 KB |
Output is correct |
4 |
Correct |
46 ms |
15888 KB |
Output is correct |
5 |
Correct |
47 ms |
15840 KB |
Output is correct |
6 |
Correct |
49 ms |
15904 KB |
Output is correct |
7 |
Correct |
45 ms |
15824 KB |
Output is correct |
8 |
Correct |
45 ms |
15800 KB |
Output is correct |
9 |
Correct |
45 ms |
15864 KB |
Output is correct |
10 |
Correct |
44 ms |
15796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
13372 KB |
Output is correct |
2 |
Correct |
42 ms |
15368 KB |
Output is correct |
3 |
Correct |
42 ms |
15428 KB |
Output is correct |
4 |
Correct |
42 ms |
15428 KB |
Output is correct |
5 |
Correct |
46 ms |
15456 KB |
Output is correct |
6 |
Correct |
42 ms |
15496 KB |
Output is correct |
7 |
Correct |
42 ms |
15428 KB |
Output is correct |
8 |
Correct |
42 ms |
15440 KB |
Output is correct |
9 |
Correct |
42 ms |
15428 KB |
Output is correct |
10 |
Correct |
44 ms |
15428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
47 ms |
14512 KB |
Output is correct |
3 |
Correct |
46 ms |
15784 KB |
Output is correct |
4 |
Correct |
47 ms |
15804 KB |
Output is correct |
5 |
Correct |
46 ms |
15888 KB |
Output is correct |
6 |
Correct |
47 ms |
15840 KB |
Output is correct |
7 |
Correct |
49 ms |
15904 KB |
Output is correct |
8 |
Correct |
45 ms |
15824 KB |
Output is correct |
9 |
Correct |
45 ms |
15800 KB |
Output is correct |
10 |
Correct |
45 ms |
15864 KB |
Output is correct |
11 |
Correct |
44 ms |
15796 KB |
Output is correct |
12 |
Correct |
37 ms |
13372 KB |
Output is correct |
13 |
Correct |
42 ms |
15368 KB |
Output is correct |
14 |
Correct |
42 ms |
15428 KB |
Output is correct |
15 |
Correct |
42 ms |
15428 KB |
Output is correct |
16 |
Correct |
46 ms |
15456 KB |
Output is correct |
17 |
Correct |
42 ms |
15496 KB |
Output is correct |
18 |
Correct |
42 ms |
15428 KB |
Output is correct |
19 |
Correct |
42 ms |
15440 KB |
Output is correct |
20 |
Correct |
42 ms |
15428 KB |
Output is correct |
21 |
Correct |
44 ms |
15428 KB |
Output is correct |
22 |
Correct |
48 ms |
14480 KB |
Output is correct |
23 |
Correct |
50 ms |
13552 KB |
Output is correct |
24 |
Correct |
56 ms |
15544 KB |
Output is correct |
25 |
Correct |
54 ms |
15684 KB |
Output is correct |
26 |
Correct |
54 ms |
15468 KB |
Output is correct |
27 |
Correct |
60 ms |
15500 KB |
Output is correct |
28 |
Correct |
53 ms |
15556 KB |
Output is correct |
29 |
Correct |
52 ms |
15556 KB |
Output is correct |
30 |
Correct |
53 ms |
15536 KB |
Output is correct |
31 |
Correct |
54 ms |
15556 KB |
Output is correct |