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\ ...
随机推荐
- sqlserver导入Excel数据 总是报错:错误 0xc020901c: 数据流任务 1: 输出“Excel 源输出”(55) 上的 输出列“T2”(64) 出错。返回的列状态是:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项
在网络上搜索解决办法,解决办法是把excel导入到access数据库中,再把access数据库导入到sqlsever中,公司机器上不让安装office工具,问了一个同事得到的回答是把数据中很长的那行数 ...
- OpenCvSharp 图像旋转
/// <summary> /// 图像旋转 /// </summary> private Mat MatRotate(Mat src, float angle) { Mat ...
- Mybatis之collection与association标签
collection与association标签的功能就是为了解决查询条件映射到一个类或一个集合上,适用于对于多对一,一对多的映射结果,现在我们就探究其具体使用吧. 环境搭建: 数据库搭建 CREAT ...
- iOS - FMDB数据库的使用
下面不废话了直接上代码
- lumen添加自定义异常
在公用工具类写异常类 <?php namespace Brady\Tool\Exception; use Brady\Tool\Constant\ErrorMsg; use \Exception ...
- springboot系列(八)springboot整合mybatis
本篇介绍一下在springboot中整合mybatis ,使用mysql数据库,集成durid 连接池技术,全部代码是手动生成,没有使用代码生成器来构建代码. 一.创建数据库和表 二.在pom中添加依 ...
- Win 2008 R2——由于管理员设置的策略,该磁盘处于脱机状态
操作系统:Windows 2008R2 现象描述: 1.原系统为Windows 2012挂载了2T的存储,因业务要求重新安装为Windows 2008R2,并没有在磁盘存储空间上重新做映射. 2.系统 ...
- WebClient 与HttpClient 的区别
需要搜索下资料. -------------------------------------------------- 微软文档介绍,新的开发中推荐使用:HttpClient WebClient 文档 ...
- evpp http编程
server.RegisterHandler("/fileupload", [](evpp::EventLoop* loop, const evpp::http::ContextP ...
- 生成器调试---send方式
调试 def creat_num(all_num): a, b = 0, 1 current_num = 0 while current_num < all_num: ret = yield a ...