另一种根号做法,不需要交互库不自适应这个性质。
考虑设计一个函数:,其中 是当前树的根, 是当前 子树中所有的点,这个函数递归解决这个子树内的连边。初始调用 即可。
初始求出所有点到 的距离。这样我们执行函数的时候,首先可以获知 的所有儿子,也就是 中距离 为 的点。现在我们需要知道剩余的点属于哪些儿子,然后继续递归。当然可以直接调用求子树的函数,但是这样很浪费,因为最后一次查询可以通过之前的询问剩下来的直接得到。
这启发我们,如果我们可以知道重链剖分的重儿子,单独不查询重儿子即可做到 次查询。但是这在自适应的交互库(虽然这题不是)下是困难的。于是退而求其次,我们发现我们已经知道每个点的深度,联想到长链剖分。有结论:长链剖分每个点到根经过的轻边个数总和是 的。现在只需要求出 子树内最深点属于哪个二层子树,直接暴力即可。操作总数 ,可以通过。甚至直接交到加强版也过了。
AC 代码
#include<bits/stdc++.h>
#define M 100005
using namespace std;
int n, dis[M], fa[M], mk[M];
vector<int> to[M];
int get(int x, int y)
{
cout << "? 1 " << x << ' ' << y << endl;
cin >> x;
return x;
}
vector<int> query(int x)
{
cout << "? 2 " << x << endl;
int n;
cin >> n;
vector<int> v;
for(int i = 1, y; i <= n; i++)
{
cin >> y;
if(y != x) v.push_back(y);
}
return v;
}
void solve(int x, vector<int> v)
{
vector<int> seq;
for(auto t : v) if(dis[t] == dis[x] + 1) fa[t] = x, seq.push_back(t);
int ps = -1;
for(auto t : v) if(ps == -1 || dis[t] > dis[ps]) ps = t;
if(ps == -1 || dis[ps] == dis[x] + 1) return;
int ban = -1;
for(auto t : seq) if(dis[ps] - dis[t] == get(ps, t))
{
ban = t;
break;
}
for(auto t : v) mk[t] = 0;
for(auto t : seq) mk[t] = 1;
for(auto t : seq) if(t != ban) to[t] = query(t);
for(auto t : seq) if(t != ban) for(auto t : to[t]) mk[t] = 1;
for(auto t : v) if(!mk[t]) to[ban].push_back(t);
for(auto t : seq) solve(t, to[t]);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
vector<int> v;
for(int i = 2; i <= n; i++) dis[i] = get(1, i), v.push_back(i);
solve(1, v);
cout << "! ";
for(int i = 2; i <= n; i++) cout << fa[i] << ' ';
cout << endl;
return 0;
}