#include <bits/stdc++.h>
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#define ent '\n'
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair < int, int> pii;
typedef pair < ll, ll > pll;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
const int INF = 1e9;
const int LOG = 20;
const int K = 400;
const int P = 31;
int n, m;
vector < pii > g[maxn];
int pref[maxn], p[LOG][maxn], tin[maxn], tout[maxn], timer, depth[maxn], a[6];
ll ans = 0;
void dfs(int v){
for(int i = 1;i < LOG;i++){
p[i][v] = p[i - 1][p[i - 1][v]];
}
tin[v] = ++timer;
for(auto to: g[v]){
if(to.f == p[0][v])continue;
p[0][to.f] = v;
depth[to.f] = depth[v] + 1;
pref[to.f] = pref[v] + to.s;
dfs(to.f);
}
tout[v] = ++timer;
}
bool upper(int a, int b){return tin[a] <= tin[b] && tout[a] >= tout[b];}
int lca(int a, int b){
if(upper(a, b))return a;
if(upper(b, a))return b;
for(int i = LOG - 1;i >= 0;i--){
if(!upper(p[i][a], b) && p[i][a] != 0)a = p[i][a];
}
return p[0][a];
}
void solve(){
cin >> n;
for(int i = 1;i < n;i++){
int u, v, w;
cin >> u >> v >> w;
u++, v++;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dfs(1);
cin >> m;
for(int i = 1;i <= m;i++){
cin >> a[1];
int LCA = a[1] + 1, ans = 0;
for(int j = 2;j <= 5;j++){
cin >> a[j];
LCA = lca(LCA, a[j] + 1);
}
for(int j = 1;j <= 5;j++){
int q = LCA;
for(int k = 1;k < j;k++){
int w = lca(a[j] + 1, a[k] + 1);
if(depth[q] < depth[w]){
q = w;
}
}
ans += pref[a[j] + 1] - pref[q];
}
cout << ans << ent;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T = 1;
// cin >> T;
for(int i = 1;i <= T;i++){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
13592 KB |
Output is correct |
2 |
Correct |
46 ms |
15404 KB |
Output is correct |
3 |
Correct |
32 ms |
15404 KB |
Output is correct |
4 |
Correct |
31 ms |
15408 KB |
Output is correct |
5 |
Correct |
30 ms |
15436 KB |
Output is correct |
6 |
Correct |
32 ms |
15484 KB |
Output is correct |
7 |
Correct |
30 ms |
15508 KB |
Output is correct |
8 |
Correct |
31 ms |
15476 KB |
Output is correct |
9 |
Correct |
30 ms |
15436 KB |
Output is correct |
10 |
Correct |
31 ms |
15436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
11680 KB |
Output is correct |
2 |
Correct |
42 ms |
14588 KB |
Output is correct |
3 |
Correct |
29 ms |
14656 KB |
Output is correct |
4 |
Correct |
26 ms |
14740 KB |
Output is correct |
5 |
Correct |
26 ms |
14664 KB |
Output is correct |
6 |
Correct |
39 ms |
14800 KB |
Output is correct |
7 |
Correct |
33 ms |
14788 KB |
Output is correct |
8 |
Correct |
26 ms |
14680 KB |
Output is correct |
9 |
Correct |
27 ms |
14796 KB |
Output is correct |
10 |
Correct |
27 ms |
14756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
41 ms |
13592 KB |
Output is correct |
3 |
Correct |
46 ms |
15404 KB |
Output is correct |
4 |
Correct |
32 ms |
15404 KB |
Output is correct |
5 |
Correct |
31 ms |
15408 KB |
Output is correct |
6 |
Correct |
30 ms |
15436 KB |
Output is correct |
7 |
Correct |
32 ms |
15484 KB |
Output is correct |
8 |
Correct |
30 ms |
15508 KB |
Output is correct |
9 |
Correct |
31 ms |
15476 KB |
Output is correct |
10 |
Correct |
30 ms |
15436 KB |
Output is correct |
11 |
Correct |
31 ms |
15436 KB |
Output is correct |
12 |
Correct |
24 ms |
11680 KB |
Output is correct |
13 |
Correct |
42 ms |
14588 KB |
Output is correct |
14 |
Correct |
29 ms |
14656 KB |
Output is correct |
15 |
Correct |
26 ms |
14740 KB |
Output is correct |
16 |
Correct |
26 ms |
14664 KB |
Output is correct |
17 |
Correct |
39 ms |
14800 KB |
Output is correct |
18 |
Correct |
33 ms |
14788 KB |
Output is correct |
19 |
Correct |
26 ms |
14680 KB |
Output is correct |
20 |
Correct |
27 ms |
14796 KB |
Output is correct |
21 |
Correct |
27 ms |
14756 KB |
Output is correct |
22 |
Correct |
44 ms |
13612 KB |
Output is correct |
23 |
Correct |
45 ms |
11756 KB |
Output is correct |
24 |
Correct |
40 ms |
14804 KB |
Output is correct |
25 |
Correct |
39 ms |
14688 KB |
Output is correct |
26 |
Correct |
38 ms |
14836 KB |
Output is correct |
27 |
Correct |
42 ms |
14884 KB |
Output is correct |
28 |
Correct |
37 ms |
14796 KB |
Output is correct |
29 |
Correct |
39 ms |
14768 KB |
Output is correct |
30 |
Correct |
37 ms |
14856 KB |
Output is correct |
31 |
Correct |
40 ms |
14832 KB |
Output is correct |