#include <bits/stdc++.h>
#define pb push_back
#define S second
#define F first
const int N = 1e5 + 10;
const int inf = 2e9;
const int lg = 17;
using namespace std;
vector<pair<int,int>>g[N];
int up[N][lg],in[N],out[N],timer = 0,dist[N];
void dfs(int x,int par) {
in[x] = ++timer;
if(par != -1) up[x][0] = par;
if(par != -1) for(int j = 1; j < lg; j++) up[x][j] = up[up[x][j - 1]][j - 1];
for(auto X : g[x]) {
if(X.F == par) continue;
dist[X.F] = dist[x] + X.S;
dfs(X.F,x);
}
out[x] = timer;
}
bool intree(int u,int v) {
return (in[u] <= in[v] && out[u] >= out[v]);
}
int lca(int u,int v) {
if(intree(u,v)) return u;
if(intree(v,u)) return v;
for(int j = lg - 1; j >= 0; j--) {
if(up[u][j] > 0 && !intree(up[u][j],v)) u = up[u][j];
}
return up[u][0];
}
bool cmp(int A,int B) {
return (in[A] <= in[B]);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for(int i = 1; i < n; i++) {
int u,v,w;
cin >> u >> v >> w;
g[u].pb({v,w});
g[v].pb({u,w});
}
dfs(0,-1);
int q;
cin >> q;
while(q--) {
vector<int>vec;
for(int i = 0; i < 5; i++) {
int X;
cin >> X;
vec.pb(X);
}
sort(vec.begin(),vec.end(),cmp);
vec.pb(vec[0]);
int res = 0;
for(int i = 0; i < 5; i++) {
res += dist[vec[i]];
res -= dist[lca(vec[i],vec[i + 1])];
}
cout << res << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
14356 KB |
Output is correct |
2 |
Correct |
36 ms |
15704 KB |
Output is correct |
3 |
Correct |
31 ms |
15700 KB |
Output is correct |
4 |
Correct |
51 ms |
15764 KB |
Output is correct |
5 |
Correct |
31 ms |
15708 KB |
Output is correct |
6 |
Correct |
37 ms |
15808 KB |
Output is correct |
7 |
Correct |
31 ms |
15700 KB |
Output is correct |
8 |
Correct |
31 ms |
15708 KB |
Output is correct |
9 |
Correct |
31 ms |
15700 KB |
Output is correct |
10 |
Correct |
30 ms |
15696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
12420 KB |
Output is correct |
2 |
Correct |
26 ms |
14936 KB |
Output is correct |
3 |
Correct |
26 ms |
14936 KB |
Output is correct |
4 |
Correct |
25 ms |
14940 KB |
Output is correct |
5 |
Correct |
26 ms |
14936 KB |
Output is correct |
6 |
Correct |
25 ms |
14932 KB |
Output is correct |
7 |
Correct |
29 ms |
14928 KB |
Output is correct |
8 |
Correct |
27 ms |
14932 KB |
Output is correct |
9 |
Correct |
25 ms |
14940 KB |
Output is correct |
10 |
Correct |
25 ms |
14852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4184 KB |
Output is correct |
2 |
Correct |
32 ms |
14356 KB |
Output is correct |
3 |
Correct |
36 ms |
15704 KB |
Output is correct |
4 |
Correct |
31 ms |
15700 KB |
Output is correct |
5 |
Correct |
51 ms |
15764 KB |
Output is correct |
6 |
Correct |
31 ms |
15708 KB |
Output is correct |
7 |
Correct |
37 ms |
15808 KB |
Output is correct |
8 |
Correct |
31 ms |
15700 KB |
Output is correct |
9 |
Correct |
31 ms |
15708 KB |
Output is correct |
10 |
Correct |
31 ms |
15700 KB |
Output is correct |
11 |
Correct |
30 ms |
15696 KB |
Output is correct |
12 |
Correct |
20 ms |
12420 KB |
Output is correct |
13 |
Correct |
26 ms |
14936 KB |
Output is correct |
14 |
Correct |
26 ms |
14936 KB |
Output is correct |
15 |
Correct |
25 ms |
14940 KB |
Output is correct |
16 |
Correct |
26 ms |
14936 KB |
Output is correct |
17 |
Correct |
25 ms |
14932 KB |
Output is correct |
18 |
Correct |
29 ms |
14928 KB |
Output is correct |
19 |
Correct |
27 ms |
14932 KB |
Output is correct |
20 |
Correct |
25 ms |
14940 KB |
Output is correct |
21 |
Correct |
25 ms |
14852 KB |
Output is correct |
22 |
Correct |
34 ms |
14164 KB |
Output is correct |
23 |
Correct |
28 ms |
12884 KB |
Output is correct |
24 |
Correct |
46 ms |
15188 KB |
Output is correct |
25 |
Correct |
34 ms |
15192 KB |
Output is correct |
26 |
Correct |
33 ms |
15184 KB |
Output is correct |
27 |
Correct |
35 ms |
15436 KB |
Output is correct |
28 |
Correct |
33 ms |
15208 KB |
Output is correct |
29 |
Correct |
40 ms |
15196 KB |
Output is correct |
30 |
Correct |
33 ms |
15336 KB |
Output is correct |
31 |
Correct |
33 ms |
15184 KB |
Output is correct |