题解:P10553 [ICPC2024 Xi'an I] Guess The Tree
AK_IOI2012 · · 题解
我就不写思路了,直接上代码:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
// #define int long long
#define mp(x, y) make_pair(x, y)
#define eb(...) emplace_back(__VA_ARGS__)
#define fro(i, x, y) for (int i = (x); i <= (y); i++)
#define pre(i, x, y) for (int i = (x); i >= (y); i--)
inline void JYFILE19();
using i64 = long long;
using PII = pair<int, int>;
bool ST;
const int N = 1100;
const int mod = 998244353;
int n, m, fa[N], sz[N], mp[N][N];
vector<int> to[N];
inline int lca(int x, int y) {
if (mp[x][y]) return mp[x][y];
cout << "? " << x << " " << y << endl;
cin >> mp[x][y];
return mp[y][x] = mp[x][y];
}
inline void upd(int x, int y, int k) {
if (mp[x][y]) return;
mp[x][y] = mp[y][x] = k;
}
inline auto sol(vector<int> all, int las = 0) {
if (all.size() == 0) return 0;
if (all.size() == 1) return all[0];
if (all.size() == 2) return fa[all[0]] = las, all[1];
if (all.size() == 3) {
int x = lca(all[0], all[1]);
for (auto i : all) if (i != x) fa[i] = x;
return x;
}
int x = all[0]; vector<int> res;
for (auto i : all) {
if (lca(i, x) == x) {
for (auto j : all) {
if (i == j) break;
upd(i, j, lca(x, j));
}
x = i;
}
}
for (auto i : all) {
sz[lca(i, x)]++;
res.eb(lca(i, x));
}
sort(res.begin(), res.end(), [&](int x, int y) {
return sz[x] < sz[y];
});
res.erase(unique(res.begin(), res.end()), res.end());
for (int i = 0; i < res.size() - 1; i++)
fa[res[i]] = res[i + 1];
for (auto i : all) {
for (auto j : res) if (i == j) goto ed;
if (lca(i, x) != i) to[lca(i, x)].eb(i);
ed:;
}
for (auto i : res) {
auto nt = to[i];
to[i].clear();
auto x = sol(nt, i);
if (x != i) fa[x] = i;
}
return res.back();
}
signed main() {
JYFILE19();
cin >> n, n = (1 << n) - 1;
vector<int> all;
fill(fa + 1, fa + n + 1, -1);
fro(i, 1, n) all.eb(i), mp[i][i] = i;
sol(all);
cout << "! ";
fro(i, 1, n) cout << fa[i] << " ";
cout << endl;
return 0;
}
bool ED;
inline void JYFILE19() {
// freopen("", "r", stdin);
// freopen("", "w", stdout);
srand(random_device{}());
ios::sync_with_stdio(0), cin.tie(0);
double MIB = fabs((&ED - &ST) / 1048576.), LIM = 32;
// cerr << "MEMORY: " << MIB << endl, assert(MIB <= LIM);
}