[爆标]CF2106G2
FerventTemp0 · · 题解
教大家一个怎么在单
顺便也优化一下在算出来根之后的查询次数(除一个
规定
操作
对一半候选者进行求和,翻转
操作
对其所有候选者进行求和,规定
翻转
操作
上述操作最多只会被执行
注意到
如此,算法以最坏
操作
定义函数
容易证明
操作
开局问一个根的
2025.09.17 更新
这是一道非常神仙的构造题,只是被 div.3 糟蹋了。
时隔一个月,如上文所说,挑选了一下 bfs 顺序,现在可以取到
理论上复杂度左边那个
\Theta(1) 放到分母外面已经价值\Theta(\frac{n}{\log^2 n}) 了,为什么我还要写右边那个O(\log n) 呢?这是为了强调这是找根的复杂度。
操作
对于前者,每个
对于后者则略微有点复杂,我会先给出一个结论,再证明这个结论成立,因为我实在不知道怎么光靠眼睛看出这个招笑的东西。
对于一条链,分配深度为
考虑怎么让这个东西不成立,最坏的情况肯定是根是
这个级数很容易计算,结果刚好是
但是幸运的是,这个问题中根本不可能出现无穷项的链,最后几项总是会被抛弃,因此首项的符号一定决定了权值之和的符号。
我不太确定链和散点之间的联系,但是这两者都恰好完全使用了所有的信息熵,也许它们之间冥冥中是相同的?题外话就先到这里吧。
操作
先找到根,接下来如果当前深度最深的树超过
现在森林共有
将深度为
具体做法:假设某个点
这个题我最近一周有空就拿出来做,现在终于做完了,完结撒花!
2025.09.24 更新
参考代码,有勇敢的建议自己尝试实现一下,非常好玩。一道纯血交互干了 7KB 上去,彻底畏惧了。
原题要求询问次数小于
代码应该写的非常的有可读性?提交链接。
#include "bits/stdc++.h"
using namespace std;
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
vector<int> Q[1001];
int n,L,root,val[1001],father[1001],pre[1001];
namespace findroot {
bool proot[1001];
int weight[1001];
void dfs1(int x,int fa,int& sum,int &res) {
int val=0;
weight[x]=1;
for(int i:Q[x])
if(i!=fa&&proot[i]) {
dfs1(i,x,sum,res);
weight[x]+=weight[i];
val=max(val,weight[i]);
}
val=max(val,sum-weight[x]);
if(val<=sum/2) res=x;
}
int gravity() {
int sum=0,res=0;
for(int i=1;i<=n;++i) sum+=proot[i];
for(int i=1;i<=n;++i)
if(proot[i]) {
dfs1(i,0,sum,res);
break;
}
if(sum==1) return -res;
return res;
}
bool checkroot(vector<int> candidate,int x) {
int res1,res2;
cout << "? 1 " << candidate.size() << ' ';
assert(candidate.size()<=n);
for(int i:candidate) cout << i << ' ';
cout << endl;
cin >> res1;
cout << "? 2 " << x << endl;
cout << "? 1 " << candidate.size() << ' ';
for(int i:candidate) cout << i << ' ';
cout << endl;
cin >> res2;
return abs(res1-res2)/2==candidate.size();
}
int weightsum(int sz,int p) {
if(p<=0||sz==0) return false;
int bs=sz/p,lc=sz%p,sc=p-lc;
return lc*(lc-1)/2*(bs+1)+(lc*2+sc-1)*sc/2*(bs);
}
int fa(int x) {
vector<int> candidate;
for(int i:Q[x])
if(proot[i])
candidate.push_back(i);
if(checkroot(candidate,x))
return x;
while(candidate.size()!=1) {
for(int p=candidate.size();;--p)
if(weightsum(candidate.size(),p)<=n) {
vector<vector<int>> group(p);
for(int i=0,j=0;i<(int)candidate.size();++i,j=(j+1)%p)
group[j].push_back(candidate[i]);
int res1,res2;
cout << "? 1 " << weightsum(candidate.size(),p) << ' ';
for(int i=0;i<p;++i)
for(int j:group[i])
for(int k=0;k<i;++k)
cout << j << ' ';
cout << endl;
cin >> res1;
cout << "? 2 " << x << endl;
cout << "? 1 " << weightsum(candidate.size(),p) << ' ';
for(int i=0;i<p;++i)
for(int j:group[i])
for(int k=0;k<i;++k)
cout << j << ' ';
cout << endl;
cin >> res2;
candidate=group[abs(weightsum(candidate.size(),p)-abs(res1-res2)/2)];
break;
}
}
return candidate[0];
}
void dfs2(int x,int fa) {
proot[x]=false;
for(int i:Q[x])
if(i!=fa)
dfs2(i,x);
}
int root() {
for(int i=1;i<=n;++i) proot[i]=true;
while(true) {
int g=gravity();
if(g<0) return -g;
int f=fa(g);
if(f==g) return g;
dfs2(g,f);
}
}
};
namespace valquery {
int depth[1001];
vector<int> chain(vector<int> node,int inital) {
assert(node.size()<=L);
int val=0;
vector<int> res;
cout << "? 1 " << (1<<(int)node.size())-1 << ' ';
for(int i=node.size()-1,j=1;i>=0;--i,j*=2)
for(int k=0;k<j;++k)
cout << node[i] << ' ';
cout << endl;
cin >> val;
for(int i=node.size()-1,j=1;i>=0;--i,j*=2)
val-=inital*j;
for(int i=0;i<(int)node.size();++i) {
int weight=1<<((int)node.size()-i-1);
if(val>0) {
res.push_back(1);
val-=weight;
val-=(weight-1);
} else {
res.push_back(-1);
val+=weight;
val+=(weight-1);
}
}
return res;
}
vector<int> separate(vector<int> node,vector<int> inital) {
assert(node.size()<=L);
int val=0;
vector<int> res;
cout << "? 1 " << (1<<(int)node.size())-1 << ' ';
for(int i=node.size()-1,j=1;i>=0;--i,j*=2)
for(int k=0;k<j;++k)
cout << node[i] << ' ';
cout << endl;
cin >> val;
for(int i=node.size()-1,j=1;i>=0;--i,j*=2)
val-=inital[i]*j;
for(int i=0;i<(int)node.size();++i) {
int weight=1<<((int)node.size()-i-1);
if(val>0) {
res.push_back(1);
val-=weight;
}
else {
res.push_back(-1);
val+=weight;
}
}
return res;
}
void dfs1(int x,int fa) {
for(int i:Q[x])
if(i!=fa) {
dfs1(i,x);
depth[x]=max(depth[x],depth[i]+1);
}
if(val[x]!=0) depth[x]=0;
else depth[x]=max(depth[x],1);
}
vector<int> findchain() {
dfs1(root,0);
int maxdepth=0,maxidx=0;
for(int i=1;i<=n;++i)
if(depth[i]>maxdepth) {
maxdepth=depth[i];
maxidx=i;
}
if(maxdepth<L) return {};
vector<int> res={maxidx};
for(int i=1,v=maxidx;i<L;++i) {
for(int j:Q[v])
if(depth[j]==depth[v]-1) {
res.push_back(j);
v=j;
break;
}
}
return res;
}
void dfs2(int x,int fa,int dep) {
if(val[x]) dep=0;
depth[x]=dep;
for(int i:Q[x])
if(i!=fa)
dfs2(i,x,dep+1);
}
void getval() {
while(true) {
vector<int> t=findchain();
if(t.empty()) break;
vector<int> v=chain(t,pre[father[t[0]]]);
for(int j=0;j<(int)t.size();++j) {
val[t[j]]=v[j];
pre[t[j]]=val[t[j]]+pre[father[t[j]]];
}
}
dfs2(root,0,1);
vector<vector<int>> group(L+1);
for(int i=1;i<=n;++i)
if(depth[i]!=0)
group[depth[i]].push_back(i);
for(int i=1;i<=L;++i) {
while(!group[i].empty()) {
vector<int> inital,query,res;
for(int j=1,v=min((int)group[i].size(),L);j<=v;++j) {
query.push_back(group[i].end()[-1]);
inital.push_back(pre[father[group[i].end()[-1]]]);
group[i].pop_back();
}
res=separate(query,inital);
for(int j=0;j<(int)query.size();++j) {
val[query[j]]=res[j];
pre[query[j]]=val[query[j]]+pre[father[query[j]]];
}
}
}
}
};
void dfs(int x,int fa) {
father[x]=fa;
for(int i:Q[x])
if(i!=fa)
dfs(i,x);
}
void clear() {
for(int i=0;i<=n;++i) {
Q[i].clear();
val[i]=0;
father[i]=0;
pre[i]=0;
findroot::proot[i]=false;
findroot::weight[i]=0;
valquery::depth[i]=0;
}
n=0;L=0;root=0;
}
void DestinVictoricaDT() {
cin >> n;
for(int i=1,u,v;i<n;++i) {
cin >> u >> v;
Q[u].push_back(v);
Q[v].push_back(u);
}
L=__lg(n+1);
root=findroot::root();
dfs(root,0);
valquery::getval();
cout << "! ";
for(int i=1;i<=n;++i)
cout << val[i] << ' ';
cout << endl;
clear();
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T=1; cin >> T;
while(T--) DestinVictoricaDT();
}
这红宝石刺刀挺好看的,给大伙看看。