#include <bits/stdc++.h>
using namespace std;
#define LIFESUCKS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ld long double
#define ar array
#include<cstdio>
#define vt vector
#include<fstream>
ifstream fin("measurement.in");
ofstream fout("measurement.out");
#include<fstream>
#define pb push_back
#define all(c) (c).begin(), (c).end()
//#define length(x) (int)(x).size()
#define fi first
#define se second
#define vt vector
using namespace std;
const int mxn = 5e4 + 5;
struct th{
int val = 0, sm = 0;
};
int n;
vt<pair<int, int>>adj[mxn + 3];
int depth[mxn + 3] = {0};
th up[mxn + 3][17];
int num[mxn + 3] = {0};
int cr = 1;
bool cmp(int a, int b){
return(num[a] < num[b]);
}
void dfs(int s, int pre){
num[s] = cr++;
for(auto i: adj[s]){
if(i.fi == pre)continue;
int v = i.fi, c = i.se;
depth[v] = depth[s] + 1;
up[v][0].val = s; up[v][0].sm = c;
dfs(v, s);
}
}
void build(){
for(int i = 1; i < 17; i++){
for(int j = 1; j <= n; j++){
up[j][i].val = up[up[j][i - 1].val][i - 1].val;
up[j][i].sm = up[j][i - 1].sm + up[up[j][i - 1].val][i - 1].sm;
}
}
}
ll find(int a, int b, bool ok){
if(depth[a] < depth[b])swap(a, b);
int k =depth[a] - depth[b];
ll curr = 0;
for(int i = 0; i < 17; i++){
if(k & (1 << i)){
curr += up[a][i].sm;
a = up[a][i].val;
}
}
//cout << curr << " ";
if(a == b){
if(ok)return(curr);
return(a);
}
for(int i = 16; i >= 0; i--){
if(up[a][i].val != up[b][i].val){
curr += up[a][i].sm + up[b][i].sm;
a = up[a][i].val; b = up[b][i].val;
}
}
curr += up[a][0].sm + up[b][0].sm;
if(ok)return(curr);
return(up[a][0].val);
}
int main()
{
LIFESUCKS;
cin >> n;
for(int i = 1; i < n; i++){
int u, v, c; cin >> u >> v >> c;
adj[u].pb({v, c});
adj[v].pb({u, c});
}
dfs(0, -1);
build();
int a[5] = {};
int q; cin >> q;
for(int i = 0; i < q; i++){
for(int j = 0; j < 5; j++)cin >> a[j];
sort(a, a + 5, cmp);
ll ans = 0;
for(int j = 0; j < 5; j++){
ans += find(a[j], a[(j + 1) % 5], true);
}
cout << ans / 2 << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
11808 KB |
Output is correct |
2 |
Correct |
33 ms |
14300 KB |
Output is correct |
3 |
Correct |
36 ms |
14244 KB |
Output is correct |
4 |
Correct |
36 ms |
14316 KB |
Output is correct |
5 |
Correct |
33 ms |
14340 KB |
Output is correct |
6 |
Correct |
37 ms |
14300 KB |
Output is correct |
7 |
Correct |
35 ms |
14268 KB |
Output is correct |
8 |
Correct |
34 ms |
14336 KB |
Output is correct |
9 |
Correct |
45 ms |
14412 KB |
Output is correct |
10 |
Correct |
33 ms |
14360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
10324 KB |
Output is correct |
2 |
Correct |
23 ms |
12684 KB |
Output is correct |
3 |
Correct |
24 ms |
12772 KB |
Output is correct |
4 |
Correct |
29 ms |
13528 KB |
Output is correct |
5 |
Correct |
24 ms |
13516 KB |
Output is correct |
6 |
Correct |
24 ms |
13500 KB |
Output is correct |
7 |
Correct |
23 ms |
13524 KB |
Output is correct |
8 |
Correct |
24 ms |
13512 KB |
Output is correct |
9 |
Correct |
25 ms |
13524 KB |
Output is correct |
10 |
Correct |
24 ms |
13532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
37 ms |
11808 KB |
Output is correct |
3 |
Correct |
33 ms |
14300 KB |
Output is correct |
4 |
Correct |
36 ms |
14244 KB |
Output is correct |
5 |
Correct |
36 ms |
14316 KB |
Output is correct |
6 |
Correct |
33 ms |
14340 KB |
Output is correct |
7 |
Correct |
37 ms |
14300 KB |
Output is correct |
8 |
Correct |
35 ms |
14268 KB |
Output is correct |
9 |
Correct |
34 ms |
14336 KB |
Output is correct |
10 |
Correct |
45 ms |
14412 KB |
Output is correct |
11 |
Correct |
33 ms |
14360 KB |
Output is correct |
12 |
Correct |
19 ms |
10324 KB |
Output is correct |
13 |
Correct |
23 ms |
12684 KB |
Output is correct |
14 |
Correct |
24 ms |
12772 KB |
Output is correct |
15 |
Correct |
29 ms |
13528 KB |
Output is correct |
16 |
Correct |
24 ms |
13516 KB |
Output is correct |
17 |
Correct |
24 ms |
13500 KB |
Output is correct |
18 |
Correct |
23 ms |
13524 KB |
Output is correct |
19 |
Correct |
24 ms |
13512 KB |
Output is correct |
20 |
Correct |
25 ms |
13524 KB |
Output is correct |
21 |
Correct |
24 ms |
13532 KB |
Output is correct |
22 |
Correct |
34 ms |
12876 KB |
Output is correct |
23 |
Correct |
40 ms |
11388 KB |
Output is correct |
24 |
Correct |
40 ms |
13856 KB |
Output is correct |
25 |
Correct |
35 ms |
13780 KB |
Output is correct |
26 |
Correct |
36 ms |
13816 KB |
Output is correct |
27 |
Correct |
34 ms |
13764 KB |
Output is correct |
28 |
Correct |
34 ms |
13876 KB |
Output is correct |
29 |
Correct |
33 ms |
13780 KB |
Output is correct |
30 |
Correct |
36 ms |
13884 KB |
Output is correct |
31 |
Correct |
35 ms |
13800 KB |
Output is correct |