#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 1e6 + 5;
const int H = __lg(SIZE) + 1;
int n, k, q, t;
int dep[SIZE], ans[SIZE];
struct Tree {
vector<int> adj[SIZE];
int root, to[SIZE][H + 1], h[SIZE];
void init() {
FOR (i, 1, n) {
int pa;
cin >> pa;
if (pa != -1) adj[pa].pb(i);
else root = i;
}
}
void dfs(int pos) {
for (int np : adj[pos]) {
h[np] = h[pos] + 1;
to[np][0] = pos;
dfs(np);
}
}
void build() {
h[root] = 1;
dfs(root);
FOR (j, 1, H) FOR (i, 1, n) to[i][j] = to[to[i][j - 1]][j - 1];
}
int jump(int pos, int k) {
int cnt = 0;
while (k) {
if (k & 1) pos = to[pos][cnt];
cnt++;
k >>= 1;
}
return pos;
}
int lca(int a, int b) {
if (h[a] < h[b]) swap(a, b);
a = jump(a, h[a] - h[b]);
if (a == b) return a;
for (int i = H; i >= 0; i--) if (to[a][i] != to[b][i]) {
a = to[a][i];
b = to[b][i];
}
return to[a][0];
}
} tree1, tree2;
void solve() {
cin >> n >> k >> q >> t;
tree1.init();
tree2.init();
tree1.build();
tree2.build();
vector<int> V;
int cnt = 0;
auto dfs = [&](auto dfs, int pos)->void {
if (cnt == k) return;
V.pb(pos);
cnt++;
for (int np : tree1.adj[pos]) dfs(dfs, np);
};
dfs(dfs, tree1.root);
for (int x : V) cout << x << ' ';
cout << endl;
FOR (i, 1, k - 1) cout << "? " << tree1.root << ' ' << V[i] << endl;
cout << "!" << endl;
FOR (i, 1, k - 1) {
int foo;
cin >> foo >> dep[V[i]] >> foo >> foo;
}
FOR (i, 1, t) {
int a, b;
cin >> a >> b;
int c = tree1.lca(a, b);
ans[i] = dep[a] - dep[c] + dep[b] - dep[c];
}
FOR (i, 1, t) cout << ans[i] << endl;
}
int main() {
Waimai;
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
956 ms |
183188 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1000 ms |
183216 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
734 ms |
181124 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1592 ms |
316420 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1734 ms |
318416 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |