#include <bits/stdc++.h>
#define ll long long
#define int ll
#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 = 1e5 + 5;
int n, q, tin[N], tout[N], timer, up[N][21], pref[N], ans, a[21];
vector <pii> g[N];
void dfs(int v, int p) {
tin[v] = ++timer;
up[v][0] = p;
for(int i = 1; i <= 20; 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 < 2);
}
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 = 20; 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
17876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
16220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Incorrect |
47 ms |
17876 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |