#include <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <cmath>
#include <queue>
#include <map>
#include <set>
// Akhmet Issa
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 5e4 + 100;
const ll INF = (ll)2e18;
const int inf = (ll)2e9;
const int maxl = 20;
const int MOD = 1e9 + 7;
int n, t, q;
vector<pii> g[maxn];
int p[maxl][maxn];
int sum[maxn];
int dep[maxn];
int tin[maxn];
int tout[maxn];
void dfs(int v){
tin[v] = ++t;
for(int k = 1; k < maxl; k++){
p[k][v] = p[k-1][p[k-1][v]];
}
for(auto [to, w]: g[v]){
if(to == p[0][v]) continue;
p[0][to] = v;
sum[to] = sum[v] + w;
dep[to] = dep[v] + 1;
dfs(to);
}
tout[v] = t;
}
bool ok(int a, int b){
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int get(int a, int b){
if(ok(a, b)) return a;
if(ok(b, a)) return b;
for(int k = maxl - 1; k >= 0; k--){
if(p[k][a] && !ok(p[k][a], b)) a = p[k][a];
}
return p[0][a];
}
void test(){
cin >> n;
for(int i = 1; i < n; i++){
int a, b, c;
cin >> a >> b >> c;
a++; b++;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
dfs(1);
cin >> q;
vector<pair<int,int>>vec;
for(int t = 1; t <= q; t++){
vec.clear();
for(int i = 0; i < 5; i++){
int x;
cin >> x;
x++;
vec.emplace_back(tin[x], x);
}
sort(vec.begin(), vec.end());
ll ans = 0;
for (int i = 0 ; i < 5 ; ++ i) {
int x = vec[i].second;
int y = vec[i == 4 ? 0 : i + 1].second;
int z = get(x, y);
ans += sum[x] + sum[y] - 2 * sum[z];
}
cout << ans / 2 << ent;
}
}
int main(){
// freopen("cows.in", "r", stdin);
// freopen("cows.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
while(t--) test();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
9800 KB |
Output is correct |
2 |
Correct |
29 ms |
11732 KB |
Output is correct |
3 |
Correct |
30 ms |
11720 KB |
Output is correct |
4 |
Correct |
30 ms |
11680 KB |
Output is correct |
5 |
Correct |
30 ms |
11748 KB |
Output is correct |
6 |
Correct |
28 ms |
11724 KB |
Output is correct |
7 |
Correct |
27 ms |
11660 KB |
Output is correct |
8 |
Correct |
27 ms |
11764 KB |
Output is correct |
9 |
Correct |
28 ms |
11720 KB |
Output is correct |
10 |
Correct |
28 ms |
11672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
8020 KB |
Output is correct |
2 |
Correct |
26 ms |
10964 KB |
Output is correct |
3 |
Correct |
25 ms |
10952 KB |
Output is correct |
4 |
Correct |
24 ms |
11024 KB |
Output is correct |
5 |
Correct |
24 ms |
11016 KB |
Output is correct |
6 |
Correct |
24 ms |
11036 KB |
Output is correct |
7 |
Correct |
23 ms |
10932 KB |
Output is correct |
8 |
Correct |
24 ms |
10964 KB |
Output is correct |
9 |
Correct |
25 ms |
10964 KB |
Output is correct |
10 |
Correct |
25 ms |
10992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
30 ms |
9800 KB |
Output is correct |
3 |
Correct |
29 ms |
11732 KB |
Output is correct |
4 |
Correct |
30 ms |
11720 KB |
Output is correct |
5 |
Correct |
30 ms |
11680 KB |
Output is correct |
6 |
Correct |
30 ms |
11748 KB |
Output is correct |
7 |
Correct |
28 ms |
11724 KB |
Output is correct |
8 |
Correct |
27 ms |
11660 KB |
Output is correct |
9 |
Correct |
27 ms |
11764 KB |
Output is correct |
10 |
Correct |
28 ms |
11720 KB |
Output is correct |
11 |
Correct |
28 ms |
11672 KB |
Output is correct |
12 |
Correct |
18 ms |
8020 KB |
Output is correct |
13 |
Correct |
26 ms |
10964 KB |
Output is correct |
14 |
Correct |
25 ms |
10952 KB |
Output is correct |
15 |
Correct |
24 ms |
11024 KB |
Output is correct |
16 |
Correct |
24 ms |
11016 KB |
Output is correct |
17 |
Correct |
24 ms |
11036 KB |
Output is correct |
18 |
Correct |
23 ms |
10932 KB |
Output is correct |
19 |
Correct |
24 ms |
10964 KB |
Output is correct |
20 |
Correct |
25 ms |
10964 KB |
Output is correct |
21 |
Correct |
25 ms |
10992 KB |
Output is correct |
22 |
Correct |
35 ms |
9904 KB |
Output is correct |
23 |
Correct |
26 ms |
8096 KB |
Output is correct |
24 |
Correct |
34 ms |
11020 KB |
Output is correct |
25 |
Correct |
36 ms |
11116 KB |
Output is correct |
26 |
Correct |
45 ms |
11084 KB |
Output is correct |
27 |
Correct |
31 ms |
11116 KB |
Output is correct |
28 |
Correct |
34 ms |
11084 KB |
Output is correct |
29 |
Correct |
32 ms |
11104 KB |
Output is correct |
30 |
Correct |
36 ms |
11072 KB |
Output is correct |
31 |
Correct |
33 ms |
11144 KB |
Output is correct |