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\ ...
随机推荐
- intel ipp6.0安装过程
由于最近看到一个代码中使用了intel ipp6.0库,了解到,ipp6.0是一个很强大的图像处理库,将其与opencv联合使用,还能够加速opencv的处理,在图像处理的过程中,是一个很重要的工具. ...
- vue组件上动态添加和删除属性
1.vue组件上动态添加和删除属性 // 添加 this.$set(this.obj, 'propName', val) // 删除 this.$delete(this.obj, 'propName' ...
- 如何理解H264 编码
H264 结构 https://blog.csdn.net/andywang201001/article/details/80274886 H264 源码 https://www.cnblogs.c ...
- Java原子操作类汇总(2)
当程序更新一个变量时,如果是多线程同时更新这个变量,可能得到的结果与期望值不同.比如:有一个变量i,A线程执行i+1,B线程也执行i+1,经过两个线程的操作后,变量i的值可能不是期望的3,而是2.这是 ...
- SQL SERVER-邮件配置
存储发邮件 USE msdb GO EXEC sp_send_dbmail @profile_name = 'mail_pro', @recipients='Jinwei.chang@quantacn ...
- DNS服务——域名解析容错
介绍 DNS服务至关重要,你我每天访问网站不可能是记忆IP地址.因此DNS服务器的可靠性至关重要.下面介绍DNS服务器容错机制,看图 为了防止DNS服务器故障不再提供服务,引入辅助DNS服务器.辅助D ...
- Zookeeper配置安装
单机模式: 1:修改hosts文件 2:下载解压zookeeper.tar文件 3:创建data目录和logs目录 进入zookeeper目录下创建 4:复制一份zoo_sample.cfg配 ...
- Mybatis3.0-[tp_28-29]-映射文件-resultMap_自定义结果集映射规则_及关联环境的搭建
笔记要点出错分析与总结工程组织 1.定义接口 EmployeeMapperPlus.java package com.dao; import com.bean.*; public interface ...
- 前端学习笔记--CSS样式--背景和超链接
1.背景 2.超链接: 举例:
- machine learning(11) -- classification: advanced optimization 去求cost function最小值的方法
其它的比gradient descent快, 在某些场合得到广泛应用的求cost function的最小值的方法 when have a large machine learning problem, ...