三道 T3 是什么场
qczrz6v4nhp6u · · 题解
CTT2020 D1T3
Statement
有一个由
n 个点构成的 有根二叉树,你不知道根。你可以向交互库给出一个点集S ,交互库会返回S 构成的虚树有多少个点,你需要求出这棵树的形态并指出父子关系,或报告无法确定。Subtask 1:
1\le n\le 500 ,保证不存在只有1 个儿子的节点,操作次数限制为4500 ;
Subtask 2:1\le n\le 1000 ,操作次数限制为10500 。
Solution
考虑 Sub 1 怎么做。
设全集为
设问出的叶子集合为
然后考虑 Sub 2。以下默认一个点的度数为它的儿子的数量。
头脑风暴一下可以发现若存在一个一度点的儿子度数不为
那么现在就可以发现,一个合法的树的形态,一定是一个度数不为
首先可以通过
考虑对
然后考虑找出一度点在哪条边上。既然已经知道了树的形态,不妨建出全局平衡二叉树,然后通过
记一个小春哥做法。考虑重剖,然后按照到根的轻边数将重链分类,显然同一类的重链之间不会存在祖先关系。查询时先花
Code
丐版全局平衡二叉树,树剖线段树但是找带权中心。
#include<bits/stdc++.h>
#include "tree.h"
using namespace std;
using ui=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
using u128=__uint128_t;
using pii=pair<int,int>;
#define fi first
#define se second
namespace xwb{
constexpr int N=1005;
int n;bool in[N];
inline int ask(){
vector<int> ver;
for(int i=1;i<=n;i++)
if(in[i])
ver.emplace_back(i);
if(!ver.size())return 0;
return Query(ver);
}
int tot,rt,fa[N],w[N],deg[N],t[N][2],bro[N];
bool del[N];vector<int> V,tr,two,one,leaf[2],uleaf;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
bool solve(){
for(const auto &o:two)tr.emplace_back(o);
for(const auto &o:leaf[0])tr.emplace_back(o);
if(!tr.size())return false;
bool cur=0;
while((int)leaf[cur].size()>1){
leaf[!cur].clear();
tot=0;
for(const auto &i:tr)
tot+=!del[i];
uleaf.clear();
for(const auto &i:tr)
if(deg[i])
uleaf.emplace_back(i);
assert(uleaf.size());
for(const auto &x:leaf[cur]){
if(fa[x])continue;
shuffle(uleaf.begin(),uleaf.end(),rnd);
int l=0,r=(int)uleaf.size()-1,mid;
while(l<r){
mid=(l+r)>>1;
for(int i=1;i<=n;i++)
in[i]=0;
for(const auto &o:leaf[cur])
if(o!=x)
in[o]=1;
for(int i=l;i<=mid;i++)
in[uleaf[i]]=1;
if(ask()==tot-1)r=mid;
else l=mid+1;
}
fa[x]=uleaf[r];
if(!--deg[fa[x]]){
leaf[!cur].emplace_back(fa[x]);
uleaf.erase(find(uleaf.begin(),uleaf.end(),fa[x]));
}
}
for(const auto &x:leaf[cur])
if(!deg[fa[x]])
del[x]=1;
else
leaf[!cur].emplace_back(x);
if(leaf[cur]==leaf[!cur])
return false;
cur^=1;
}
for(const auto &o:tr)
if(deg[o])
return false;
rt=leaf[cur].back();
return true;
}
inline bool Ask(int u,int v){
for(int i=1;i<=n;i++)in[i]=0;
in[u]=in[v]=1;
return ask()==2;
}
int siz[N],hv[N],lt[N],dfc,dfn[N],rnk[N],top[N],bot[N];
void dfs1(int x){
siz[x]=1;
if(!t[x][0]||!t[x][1]){
assert(!t[x][0]&&!t[x][1]);
return;
}
dfs1(t[x][0]),siz[x]+=siz[t[x][0]];
dfs1(t[x][1]),siz[x]+=siz[t[x][1]];
hv[x]=t[x][siz[t[x][0]]<=siz[t[x][1]]];
lt[x]=bro[hv[x]];
}
void dfs2(int x,int tp){
rnk[dfn[x]=++dfc]=x,top[x]=tp;
if(!hv[x]){
bot[tp]=x;
return;
}
dfs2(hv[x],tp);
dfs2(lt[x],lt[x]);
}
struct sgt1{
int idx,Rt[N],L[N<<1],R[N<<1],M[N<<1],Ls[N<<1],Rs[N<<1];
void build(int l,int r,int &p){
p=++idx,L[p]=l,R[p]=r;
if(l==r)return;
M[p]=(l+r)>>1;
build(L[p],M[p],Ls[p]);
build(M[p]+1,R[p],Rs[p]);
}
inline int qry(int p,int o){
if(L[p]==R[p])return rnk[L[p]];
if(Ask(lt[rnk[M[p]]],o))
return qry(Ls[p],o);
else
return qry(Rs[p],o);
}
}t1;
struct sgt2{
int idx,Rt[N],L[N<<1],R[N<<1],M[N<<1],Ls[N<<1],Rs[N<<1];
void build(int l,int r,int &p){
p=++idx,L[p]=l,R[p]=r;
if(l==r)return;
int sum=0,cur=0,val=n+1,pos=-1,tmp;
for(int i=l;i<=r;i++)
sum+=siz[lt[rnk[i]]];
for(int i=l;i<r;i++){
cur+=siz[lt[rnk[i]]];
tmp=max(cur,sum-cur);
if(tmp<val)
pos=i,val=tmp;
}
M[p]=pos;
build(L[p],M[p],Ls[p]);
build(M[p]+1,R[p],Rs[p]);
}
inline int qry(int p,int o){
if(L[p]==R[p])return rnk[L[p]];
if(Ask(rnk[M[p]+1],o))
return qry(Rs[p],o);
else
return qry(Ls[p],o);
}
}t2;
void Solve(int _n){
n=_n;
for(int x=1;x<=n;x++){
for(int i=1;i<=n;i++)
in[i]=x!=i;
if(ask()==n-1)
V.emplace_back(x);
else
two.emplace_back(x),deg[x]=2;
}
for(const auto &x:V){
for(int i=1;i<=n;i++)in[i]=0;
for(const auto &i:V)in[i]=x!=i;
if(ask()==n-2)
leaf[0].emplace_back(x);
else
one.emplace_back(x),deg[x]=1;
}
if(!solve()){
Report(-1,-1);
return;
}
for(const auto &i:tr){
if(!fa[i])continue;
if(!t[fa[i]][0])
t[fa[i]][0]=i;
else{
t[fa[i]][1]=i;
bro[i]=t[fa[i]][0],bro[t[fa[i]][0]]=i;
}
}
dfs1(rt),dfs2(rt,rt);
for(const auto &x:tr)
if(top[x]==x){
t1.build(dfn[x],dfn[bot[x]],t1.Rt[x]);
t2.build(dfn[x],dfn[bot[x]],t2.Rt[x]);
}
for(const auto &o:one){
int u=rt;
while(1){
if(Ask(bot[u],o)){
u=t1.qry(t1.Rt[u],o);
break;
}
else
u=lt[t2.qry(t2.Rt[u],o)];
}
if(w[u]){
Report(-1,-1);
return;
}
w[u]=o;
}
for(const auto &x:tr){
if(w[x]){
Report(w[x],x);
if(fa[x])
Report(fa[x],w[x]);
}
else if(fa[x])
Report(fa[x],x);
}
}
}
void Solve(int N){xwb::Solve(N);}