跳到正文

题解

三道 T3 是什么场

qczrz6v4nhp6u约 11 分钟
原文
文章信息与版本

文章信息

发布
2025年8月19日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

CTT2020 D1T3

Statement

有一个由 nn 个点构成的 有根二叉树,你不知道根。你可以向交互库给出一个点集 SS,交互库会返回 SS 构成的虚树有多少个点,你需要求出这棵树的形态并指出父子关系,或报告无法确定。

Subtask 1:1≤n≤5001\le n\le 500,保证不存在只有 11 个儿子的节点,操作次数限制为 45004500; Subtask 2:1≤n≤10001\le n\le 1000,操作次数限制为 1050010500。

Solution

考虑 Sub 1 怎么做。

设全集为 UU,首先可以通过 [ask(U∖{x})=n−1][{\rm ask}(U\setminus\{x\})=n-1] 问出 xx 是否是叶子,考虑类似拓扑排序从底向上做。尝试找到一个叶子的父亲。

设问出的叶子集合为 LL,那么可以发现 ask(L∖{x})=n−2{\rm ask}(L\setminus\{x\})=n-2 中不会包含 fax{\rm fa}_x,加入 fax{\rm fa}_x 后就会变成 11,可以二分求出 fax{\rm fa}_x。这部分的操作次数为 O(nlog⁡n)O(n\log n)。

然后考虑 Sub 2。以下默认一个点的度数为它的儿子的数量。

头脑风暴一下可以发现若存在一个一度点的儿子度数不为 22,那么就无解,因为若一个一度点的儿子度数不为 22,则这两个节点无论怎么问答案都相同。显然这是充要。

那么现在就可以发现,一个合法的树的形态,一定是一个度数不为 11 的树,选择若干条不连向叶子的边并在其中加入一个点的形式。尝试把这些一度点找出来,剩下的用 Sub 1 做法。

首先可以通过 [ask(U∖{x})=n−1][{\rm ask}(U\setminus\{x\})=n-1] 判断 xx 是否是一度点或叶子,记这些点构成的集合为 VV。接下来问 ask(V∖{x}){\rm ask}(V\setminus\{x\}),若其为 n−2n-2 则 xx 是叶子,否则 xx 是一度点 或是叶子且其父亲是一度点。

考虑对 ask(V∖{x})=n−2{\rm ask}(V\setminus \{x\})=n-2 的 xx 找出父亲。这有可能找不到,因为我们没有找出所有的叶子,此时无解。否则我们一定找出了所有的叶子且所有一度点的儿子都是二度点,已经可以将除去一度点的部分问出来了。

然后考虑找出一度点在哪条边上。既然已经知道了树的形态,不妨建出全局平衡二叉树,然后通过 ask({u,v}){\rm ask}(\{u,v\}) 查 u,vu,v 是否存在祖孙关系即可。操作次数 O(nlog⁡n)O(n\log n)。


记一个小春哥做法。考虑重剖,然后按照到根的轻边数将重链分类,显然同一类的重链之间不会存在祖先关系。查询时先花 O(log⁡log⁡n)O(\log\log n) 次数找到它在哪一层,然后再把这一层拍在一起二分即可。据说比标算优???

Code

丐版全局平衡二叉树,树剖线段树但是找带权中心。

C++
#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);}
原文