#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 50000+5 , INF = 1e18 , MOD = 1e9+7;
vector<pair<ll,ll>> adj[N];
vector<vector<ll>> up(N,vector<ll>(log2(N)+5));
vector<ll> level(N) , tin(N) , dist(N);
ll timer = 0;
void dfs(ll s , ll p){
timer++;
tin[s] = timer;
for(auto u : adj[s]){
if(u.first == p) continue;
level[u.first] = level[s]+1;
dist[u.first] = dist[s]+u.second;
up[u.first][0] = s;
for(ll i = 1; i <= log2(N); i++) up[u.first][i] = up[up[u.first][i-1]][i-1];
dfs(u.first,s);
}
}
ll jump(ll a , ll k){
for(ll i = 0; i <= log2(N); i++){
if(k&(1ll<<i)){
a = up[a][i];
}
}
return a;
}
ll lca(ll a , ll b){
if(level[a] > level[b]) swap(a,b);
b = jump(b,level[b]-level[a]);
if(a == b) return a;
for(ll i = log2(N); i >= 0; i--){
if(up[a][i] != up[b][i]){
a = up[a][i];
b = up[b][i];
}
}
return up[a][0];
}
ll distanc(ll a , ll b){
return dist[a]+dist[b]-2*dist[lca(a,b)];
}
bool comp(ll a , ll b){
return tin[a] < tin[b];
}
void solve(){
ll n;
cin >> n;
for(ll i = 1; i < n; i++){
ll a,b,c;
cin >> a >> b >> c;
a++;b++;
adj[a].push_back(make_pair(b,c));
adj[b].push_back(make_pair(a,c));
}
dist[1] = 0;
level[1] = 0;
for(ll i = 0; i <= log2(N); i++) up[1][i] = 0;
dfs(1,0);
ll q;
cin >> q;
while(q--){
vector<ll> nodes;
for(ll i = 1; i <= 5; i++){
ll x;
cin >> x;
nodes.push_back(x+1);
}
sort(nodes.begin(),nodes.end(),comp);
ll ans = 0;
for(ll i = 1; i < 5; i++) ans += distanc(nodes[i],nodes[i-1]);
ans += distanc(nodes[0],nodes[4]);
ans /= 2;
cout << ans << "\n";
}
}
int main(){
fast;
ll tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
17088 KB |
Output is correct |
2 |
Correct |
87 ms |
18540 KB |
Output is correct |
3 |
Correct |
79 ms |
18460 KB |
Output is correct |
4 |
Correct |
72 ms |
18428 KB |
Output is correct |
5 |
Correct |
85 ms |
18648 KB |
Output is correct |
6 |
Correct |
93 ms |
18532 KB |
Output is correct |
7 |
Correct |
72 ms |
18520 KB |
Output is correct |
8 |
Correct |
72 ms |
18512 KB |
Output is correct |
9 |
Correct |
82 ms |
18504 KB |
Output is correct |
10 |
Correct |
78 ms |
18472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
15688 KB |
Output is correct |
2 |
Correct |
53 ms |
17748 KB |
Output is correct |
3 |
Correct |
57 ms |
17768 KB |
Output is correct |
4 |
Correct |
49 ms |
17756 KB |
Output is correct |
5 |
Correct |
50 ms |
17776 KB |
Output is correct |
6 |
Correct |
54 ms |
17700 KB |
Output is correct |
7 |
Correct |
61 ms |
17704 KB |
Output is correct |
8 |
Correct |
55 ms |
17788 KB |
Output is correct |
9 |
Correct |
57 ms |
17800 KB |
Output is correct |
10 |
Correct |
53 ms |
17924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12372 KB |
Output is correct |
2 |
Correct |
81 ms |
17088 KB |
Output is correct |
3 |
Correct |
87 ms |
18540 KB |
Output is correct |
4 |
Correct |
79 ms |
18460 KB |
Output is correct |
5 |
Correct |
72 ms |
18428 KB |
Output is correct |
6 |
Correct |
85 ms |
18648 KB |
Output is correct |
7 |
Correct |
93 ms |
18532 KB |
Output is correct |
8 |
Correct |
72 ms |
18520 KB |
Output is correct |
9 |
Correct |
72 ms |
18512 KB |
Output is correct |
10 |
Correct |
82 ms |
18504 KB |
Output is correct |
11 |
Correct |
78 ms |
18472 KB |
Output is correct |
12 |
Correct |
43 ms |
15688 KB |
Output is correct |
13 |
Correct |
53 ms |
17748 KB |
Output is correct |
14 |
Correct |
57 ms |
17768 KB |
Output is correct |
15 |
Correct |
49 ms |
17756 KB |
Output is correct |
16 |
Correct |
50 ms |
17776 KB |
Output is correct |
17 |
Correct |
54 ms |
17700 KB |
Output is correct |
18 |
Correct |
61 ms |
17704 KB |
Output is correct |
19 |
Correct |
55 ms |
17788 KB |
Output is correct |
20 |
Correct |
57 ms |
17800 KB |
Output is correct |
21 |
Correct |
53 ms |
17924 KB |
Output is correct |
22 |
Correct |
72 ms |
17064 KB |
Output is correct |
23 |
Correct |
61 ms |
16072 KB |
Output is correct |
24 |
Correct |
80 ms |
18140 KB |
Output is correct |
25 |
Correct |
74 ms |
18092 KB |
Output is correct |
26 |
Correct |
83 ms |
18048 KB |
Output is correct |
27 |
Correct |
80 ms |
18252 KB |
Output is correct |
28 |
Correct |
72 ms |
18112 KB |
Output is correct |
29 |
Correct |
95 ms |
18052 KB |
Output is correct |
30 |
Correct |
71 ms |
18172 KB |
Output is correct |
31 |
Correct |
78 ms |
18144 KB |
Output is correct |