#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define in insert
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define deb(x) cout << #x << " = " << x << '\n';
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
// const int dx[] = {0, 1, 0, -1};
// const int dy[] = {1, 0, -1, 0};
const int inf = 1e9;
const ll INF = 1e18;
const int mod = 1e9 + 7;
const int N = 5e4 + 5;
int n, q, tin[N], tout[N], timer, up[N][19], pref[N], ans, a[10];
vector <pii> g[N];
void dfs(int v, int p) {
tin[v] = ++timer;
up[v][0] = p;
for(int i = 1; i <= 18; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(auto to:g[v]) {
if(to.ff == p) continue;
pref[to.ff] = pref[v] + to.ss;
dfs(to.ff, v);
}
tout[v] = timer;
}
bool notanc(int v) {
int cnt = 0;
for(int i = 1; i <= 5; i++) {
cnt += (tin[v] <= tin[a[i]] && tout[a[i]] <= tout[v]);
}
return cnt == 1;
}
void solve() {
cin >> n;
for(int i = 1; i <= n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(1, 1);
cin >> q;
while(q--) {
for(int i = 1; i <= 5; i++) {
cin >> a[i];
}
ans = 0;
for(int i = 1; i <= 5; i++) {
int v = a[i];
for(int j = 18; j >= 0; j--) {
if(notanc(up[v][j])) v = up[v][j];
}
ans += pref[a[i]] - pref[up[v][0]];
}
cout << ans << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int TT = 1;
// cin >> TT;
while(TT--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
9652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
7768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Incorrect |
37 ms |
9652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |