#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (500005)
ll V;
vector<pair<ll,ll> > v[MAXN];
ll parent[20][MAXN];
ll depth[MAXN];
ll dist[MAXN];
ll visited[MAXN];
void distdfs(ll x){
visited[x] = 1;
for(auto u : v[x]){
if(visited[u.first] == 0){
dist[u.first] = dist[x] + u.second;
visited[u.first] = 1;
distdfs(u.first);
}
}
}
void dfs(ll x,ll nodeparent,ll nodedepth){
parent[0][x] = nodeparent;
depth[x] = nodedepth;
for(ll i = 0;i < v[x].size();i++){
if(v[x][i].first != nodeparent){
dfs(v[x][i].first,x,nodedepth + 1);
}
}
}
void lcainit(){
for(ll i = 1;i < 20;i++){
for(ll j = 0;j < V;j++){
parent[i][j] = parent[i - 1][parent[i - 1][j]];
}
}
}
long long lca(ll u,ll v){
if(depth[u] > depth[v]){
swap(u,v);
}
ll diff = depth[v] - depth[u];
for(ll i = 0;i < 20;i++){
if(diff & (1<<i)){
v = parent[i][v];
}
}
if(u == v){
return u;
}
for(ll i = 19;i >= 0;i--){
if(parent[i][u] != parent[i][v]){
u = parent[i][u];
v = parent[i][v];
}
}
return parent[0][u];
}
ll pre[MAXN];
ll cnt = 0;
void order(ll x,ll p){
pre[x] = cnt++;
for(auto u : v[x]){
if(u.first != p){
order(u.first,x);
}
}
}
bool cmp(ll a,ll b){
return pre[a] < pre[b];
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>V;
for(ll i = 0;i < V - 1;i++){
ll a,b,w;
cin>>a>>b>>w;
v[a].push_back(make_pair(b,w));
v[b].push_back(make_pair(a,w));
}
dist[0] = 0;
visited[0] = 1;
distdfs(0);
dfs(0,0,0);
lcainit();
order(0,-1);
ll Q;
cin>>Q;
for(ll q = 0;q < Q;q++){
ll arr[5];
for(ll i = 0;i < 5;i++){
cin>>arr[i];
}
sort(arr,arr + 5,cmp);
ll leader = arr[0]; //lca of all 5 nodes
for(ll i = 1;i < 5;i++){
leader = lca(leader,arr[i]);
}
ll sum = (dist[leader] + dist[arr[0]] - 2 * dist[leader]);
for(ll i = 1;i < 5;i++){
ll a = lca(arr[i - 1],arr[i]);
ll b = arr[i];
sum += (dist[a] + dist[b] - 2 * dist[a]);
}
cout<<sum<<'\n';
}
}
Compilation message
roadsideadverts.cpp: In function 'void dfs(ll, ll, ll)':
roadsideadverts.cpp:24:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(ll i = 0;i < v[x].size();i++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
26736 KB |
Output is correct |
2 |
Correct |
90 ms |
28516 KB |
Output is correct |
3 |
Correct |
62 ms |
28488 KB |
Output is correct |
4 |
Correct |
66 ms |
28516 KB |
Output is correct |
5 |
Correct |
72 ms |
28408 KB |
Output is correct |
6 |
Correct |
70 ms |
28480 KB |
Output is correct |
7 |
Correct |
65 ms |
28472 KB |
Output is correct |
8 |
Correct |
62 ms |
28520 KB |
Output is correct |
9 |
Correct |
64 ms |
28396 KB |
Output is correct |
10 |
Correct |
63 ms |
28492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
24832 KB |
Output is correct |
2 |
Correct |
42 ms |
27588 KB |
Output is correct |
3 |
Correct |
42 ms |
27596 KB |
Output is correct |
4 |
Correct |
42 ms |
27596 KB |
Output is correct |
5 |
Correct |
46 ms |
27600 KB |
Output is correct |
6 |
Correct |
49 ms |
27648 KB |
Output is correct |
7 |
Correct |
41 ms |
27652 KB |
Output is correct |
8 |
Correct |
42 ms |
27592 KB |
Output is correct |
9 |
Correct |
43 ms |
27596 KB |
Output is correct |
10 |
Correct |
47 ms |
27548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12244 KB |
Output is correct |
2 |
Correct |
78 ms |
26736 KB |
Output is correct |
3 |
Correct |
90 ms |
28516 KB |
Output is correct |
4 |
Correct |
62 ms |
28488 KB |
Output is correct |
5 |
Correct |
66 ms |
28516 KB |
Output is correct |
6 |
Correct |
72 ms |
28408 KB |
Output is correct |
7 |
Correct |
70 ms |
28480 KB |
Output is correct |
8 |
Correct |
65 ms |
28472 KB |
Output is correct |
9 |
Correct |
62 ms |
28520 KB |
Output is correct |
10 |
Correct |
64 ms |
28396 KB |
Output is correct |
11 |
Correct |
63 ms |
28492 KB |
Output is correct |
12 |
Correct |
34 ms |
24832 KB |
Output is correct |
13 |
Correct |
42 ms |
27588 KB |
Output is correct |
14 |
Correct |
42 ms |
27596 KB |
Output is correct |
15 |
Correct |
42 ms |
27596 KB |
Output is correct |
16 |
Correct |
46 ms |
27600 KB |
Output is correct |
17 |
Correct |
49 ms |
27648 KB |
Output is correct |
18 |
Correct |
41 ms |
27652 KB |
Output is correct |
19 |
Correct |
42 ms |
27592 KB |
Output is correct |
20 |
Correct |
43 ms |
27596 KB |
Output is correct |
21 |
Correct |
47 ms |
27548 KB |
Output is correct |
22 |
Correct |
70 ms |
26736 KB |
Output is correct |
23 |
Correct |
82 ms |
25292 KB |
Output is correct |
24 |
Correct |
83 ms |
27932 KB |
Output is correct |
25 |
Correct |
74 ms |
27968 KB |
Output is correct |
26 |
Correct |
78 ms |
27988 KB |
Output is correct |
27 |
Correct |
86 ms |
27904 KB |
Output is correct |
28 |
Correct |
75 ms |
28004 KB |
Output is correct |
29 |
Correct |
79 ms |
27956 KB |
Output is correct |
30 |
Correct |
75 ms |
27940 KB |
Output is correct |
31 |
Correct |
76 ms |
27904 KB |
Output is correct |