#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 = 5e5 + 5;
int n, q, tin[N], tout[N], timer, up[N][22], pref[N], ans, a[105], cnt[105];
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;
}
int get(int v) {
int cur = 0;
for(int j = 1; j <= 5; j++) {
if(tin[v] <= tin[a[j]] && tout[a[j]] <= tout[v]) cur++;
}
return cur;
}
void solve() {
cin >> n;
for(int i = 1; i <= n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u++;
v++;
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];
a[i]++;
}
for(int i = 1; i <= 5; i++) {
cnt[i] = 0;
for(int j = 1; j <= 5; j++) {
if(tin[a[i]] <= tin[a[j]] && tout[a[j]] <= tout[a[i]]) cnt[i]++;
}
}
ans = 0;
for(int i = 1; i <= 5; i++) {
int v = a[i];
for(int j = 20; j >= 0; j--) {
if(get(up[v][j]) == cnt[i]) 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 |
3 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
29096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
28220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Incorrect |
62 ms |
29096 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |