CF1174F Ehab and the Big Finale(交互+剖分)
做法
\(x\)为隐藏节点,\(dep_x=d(1,x)\)
\((1)\):\(u=1\)
\((2)\):重链剖分,比如\(v\)为\(u\)的重链底部,查询\(dis(x,v)\)的长度,\(y=lca(v,x)\)且在重链上,\(dis(x,v)=dep_v+dep_x-2*dep_y,dep_y=(dep_v+dep_x-dis(x,v))/2\),则我们可以找到\(y\)
\((3)\):但\(dep_y=dep_x\)时,\(y\)为答案,退出
\((4)\):找到\(y\)后,查询\(sec=(y,x)\)上的第二个节点,\(u=sec\)返回\((2)\)
code
#include<bits/stdc++.h>
typedef int LL;
const LL maxn=1e6+9;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3)+(x<<1)+c-'0'; c=getchar();
}return x*f;
}
struct node{
LL to,nxt;
}dis[maxn];
LL n,num;
LL head[maxn],size[maxn],tail[maxn],dep[maxn],fa[maxn],son[maxn];
inline void Add(LL u,LL v){
dis[++num]=(node){v,head[u]}; head[u]=num;
}
void Dfs1(LL u){
size[u]=1;
for(LL i=head[u];i;i=dis[i].nxt){
LL v(dis[i].to);
if(v==fa[u]) continue;
fa[v]=u; dep[v]=dep[u]+1;
Dfs1(v); size[u]+=size[v];
if(size[son[u]]<size[v]) son[u]=v;
}
}
void Dfs2(LL u,LL f){
if(son[u]) Dfs2(son[u],f);
for(LL i=head[u];i;i=dis[i].nxt){
LL v(dis[i].to);
if(v==fa[u] || v==son[u]) continue;
Dfs2(v,v);
}
if(!tail[f]) tail[f]=u;
}
LL Query(LL u,LL len){
if(!len) return u;
return Query(fa[u],len-1);
}
inline LL Query1(LL x){
printf("d %d\n",x);
fflush(stdout);
LL ret; ret=Read();
return ret;
}
inline LL Query2(LL x){
printf("s %d\n",x);
fflush(stdout);
LL ret; ret=Read();
return ret;
}
int main(){
n=Read();
for(LL i=1;i<n;++i){
LL u(Read()),v(Read());
Add(u,v); Add(v,u);
}
Dfs1(1); Dfs2(1,1);
LL depx(Query1(1));
LL u(1),v(tail[u]);
while(true){
LL dis_vx(Query1(v));
LL sum(dep[v]+depx-dis_vx);
LL depy(sum>>1);
LL len(dep[v]-depy);
LL y(Query(v,len));
if(depx==depy){
printf("! %d\n",y);
fflush(stdout);
return 0;
}
u=Query2(y); v=tail[u];
}
}
CF1174F Ehab and the Big Finale(交互+剖分)的更多相关文章
- Codeforces.862D.Mahmoud and Ehab and the binary string(交互 二分)
题目链接 \(Description\) 有一个长为\(n\)的二进制串,保证\(01\)都存在.你可以询问不超过\(15\)次,每次询问你给出一个长为\(n\)的二进制串,交互库会返回你的串和目标串 ...
- Codeforces Round #563 (Div. 2) F. Ehab and the Big Finale
后续: 点分治标程 使用father数组 比使用vis数组优秀(不需要对vis初始化) https://codeforces.com/problemset/problem/1174/F https:/ ...
- Codeforces Round #563 (Div. 2)/CF1174
Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...
- Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)
<题目链接> 题目大意: 有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的 Hamming ...
- BZOJ4732. [清华集训2016]数据交互(树链剖分+线段树+multiset)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4732 题解 首先,一个比较显然的结论是:对于一棵有根树上的两条链 \((x_1, y_1 ...
- Codeforces Round #525 (Div. 2) D. Ehab and another another xor problem(交互题 异或)
题目 题意: 0≤a,b<2^30, 最多猜62次. 交互题,题目设定好a,b的值,要你去猜.要你通过输入 c d : 如果 a^c < b^d ,会反馈 -1 : 如果 a^c = b^ ...
- LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)
题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS ...
- UOJ268 [清华集训2016] 数据交互 【动态DP】【堆】【树链剖分】【线段树】
题目分析: 不难发现可以用动态DP做. 题目相当于是要我求一条路径,所有与路径有交的链的代价加入进去,要求代价最大. 我们把链的代价分成两个部分:一部分将代价加入$LCA$之中,用$g$数组保存:另一 ...
- Codeforces.1088D.Ehab and another another xor problem(交互 思路)
题目链接 边颓边写了半上午A掉啦233(本来就是被无数人过掉的好吗→_→) 首先可以\(Query\)一次得到\(a,b\)的大小关系(\(c=d=0\)). 然后发现我们是可以逐位比较出\(a,b\ ...
随机推荐
- springboot笔记04——读取配置文件+使用slf4j日志
前言 springboot常用的配置文件有yml和properties两种,当然有必要的时候也可以用xml.我个人更加喜欢用yml,所以我在这里使用yml作为例子.yml或properties配置文件 ...
- robot framework 如何处理循环条件下面的变量自增
下面举了一个基础栗子,可以运行的.${num}就是我需要的自增变量.有人也许会问为什么不用${i},不是我不想用,而是我${i}有其他用处,必须另外定义一个变量,需要注意的是定义变量的时候,应该在循环 ...
- python—各种常用函数及库
列表list1.append(x) 将x添加到列表末尾 list1.sort() 对列表元素排序 list1.reverse() 将 ...
- vue中修改第三方组件的样式并不造成污染
vue引用了第三方组件, 需要在组件中局部修改第三方组件的样式, 而又不想去除scoped属性造成组件之间的样式污染. 此时只能通过>>>,穿透scoped. 但是,在sass中存在 ...
- windows系统Android-sdk的下载与环境变量配置
最近一段时间在做app的开发,作为前端而言,开发app并不像android工程师那样熟悉android开发:无论是使用cordova.js或者react-native开发都需要配置android开发环 ...
- 修改tomcat使用的的编码方式
默认情况下,tomcat使用的的编码方式:iso8859-1 修改tomcat下的conf/server.xml文件 找到如下代码: < Connector port="8080 ...
- 基于LPCXpresso54608开发板创建Embedded Wizard UI应用程序
平台集成和构建开发环境:LPCXpresso 54608入门指南 本文主要介绍了创建一个适用于LPCXpresso54608开发板的Embedded Wizard UI应用程序所需的所有必要步骤.请一 ...
- python函数式编程-偏向函数
Python的functools模块提供了很多有用的功能,其中一个就是偏函数(Partial function).要注意,这里的偏函数和数学意义上的偏函数不一样. 在介绍函数参数的时候,我们讲到,通过 ...
- 微服务学习及.net core入门教程
https://www.cnblogs.com/jackyfei/p/12067708.html https://www.cnblogs.com/jesse2013/ http://video.jes ...
- 斜率优化DP总结
HDU3507 Print Article Zero has an old printer that doesn't work well sometimes. As it is antique, he ...