CF772E Verifying Kingdom

有趣的交互题(交互题都挺有意思的)

%ywy

增量法构造

考虑加入了前i个叶子

那么树是前i个叶子构成的虚树!

最后n个叶子构成的虚树就是答案!

怎样确定第i+1个叶子的位置?

用点分治“二分”!

每次找到当前连通块的重心rt,注意rt不能是叶子

维护rt的两个儿子,父亲,两个儿子内部随意一个叶子has[0],has[1]

用has[0],has[1],i+1进行ask,X,Y,Z唯一确定一个方向:fa、rson,lson

递归进去二分。

边界:

1.往fa走没有fa,新建

2.往fa、son已经vis了,这个边上长出一个点和一个叶子来

3.当前点是叶子,新建节点,叶子和i+1连在下面。

询问次数O(nlogn)

复杂度O(n^2)(每次现找重心,只递归一边。)

注意:

1.点分治找到rt之后再dfs才是真正size。。

2.rt和进入点x不能弄混了。。

Code

#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
char ch;x=;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);(fl==true)&&(x=-x);}
template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
template<class T>il void ot(T x){if(x<) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
namespace Modulo{
const int mod=;
int ad(int x,int y){return (x+y)>=mod?x+y-mod:x+y;}
void inc(int &x,int y){x=ad(x,y);}
int mul(int x,int y){return (ll)x*y%mod;}
void inc2(int &x,int y){x=mul(x,y);}
int qm(int x,int y=mod-){int ret=;while(y){if(y&) ret=mul(x,ret);x=mul(x,x);y>>=;}return ret;}
}
//using namespace Modulo;
namespace Miracle{
const int N=;
int n;
char s[];
int fa[N],ch[N][],has[N][];//has a leaf
int tot;
int ask(int a,int b,int c){
printf("%d %d %d\n",a,b,c);
fflush(stdout);
scanf("%s",s+);
return (s[]-'X');
}
bool vis[N];
int rt,sz[N];
int nowsz;
void dfs(int x,int od){
if(!x) return;
// cout<<" x "<<x<<" od "<<od<<" "<<fa[x]<<" "<<ch[x][0]<<" "<<ch[x][1]<<" visf "<<vis[fa[x]]<<" "<<(fa[x]!=od)<<endl;
sz[x]=;
int mx=,now;
if(!vis[fa[x]]&&fa[x]!=od) dfs(fa[x],x),mx=max(mx,sz[fa[x]]),sz[x]+=sz[fa[x]];
if(!vis[ch[x][]]&&ch[x][]!=od) dfs(ch[x][],x),mx=max(mx,sz[ch[x][]]),sz[x]+=sz[ch[x][]];
if(!vis[ch[x][]]&&ch[x][]!=od) dfs(ch[x][],x),mx=max(mx,sz[ch[x][]]),sz[x]+=sz[ch[x][]]; if((ch[x][]||ch[x][])&&(max(mx,nowsz-sz[x])<=nowsz/)) rt=x;
}
void dfs2(int x,int od){
if(!x) return;
// cout<<" x "<<x<<" od "<<od<<" "<<fa[x]<<" "<<ch[x][0]<<" "<<ch[x][1]<<" visf "<<vis[fa[x]]<<" "<<(fa[x]!=od)<<endl;
sz[x]=;
if(!vis[fa[x]]&&fa[x]!=od) dfs2(fa[x],x),sz[x]+=sz[fa[x]];
if(!vis[ch[x][]]&&ch[x][]!=od) dfs2(ch[x][],x),sz[x]+=sz[ch[x][]];
if(!vis[ch[x][]]&&ch[x][]!=od) dfs2(ch[x][],x),sz[x]+=sz[ch[x][]];
}
void fin(int x,int id){
// cout<<" fin "<<x<<" nowsz "<<nowsz<<endl;
if(!ch[x][]&&!ch[x][]){//leaf
// cout<<" leaf "<<endl;
++tot;
int d=ch[fa[x]][]==x;
ch[tot][]=x;ch[tot][]=id;fa[tot]=fa[x];ch[fa[x]][d]=tot;
fa[x]=tot;fa[id]=tot;
has[tot][]=x;has[tot][]=id;
return ;
}
rt=;
dfs(x,);
x=rt;
dfs2(rt,);
// cout<<" rt "<<rt<<" has "<<has[rt][0]<<" "<<has[rt][1]<<endl;
int bc=ask(has[rt][],has[rt][],id);
vis[rt]=;
if(bc==){
if(fa[x]){
if(vis[fa[x]]){
int y=fa[x];
int d=ch[y][]==x;
ch[y][d]=++tot;
fa[tot]=y;
ch[tot][d]=x;fa[x]=tot;
ch[tot][d^]=id;
fa[id]=tot;
has[tot][d]=has[x][];
has[tot][d^]=id;
}else{
nowsz=sz[fa[x]];
fin(fa[x],id);
}
}else{
// cout<<" nofa "<<endl;
fa[x]=++tot;
ch[tot][]=x;
ch[tot][]=id;
has[tot][]=has[x][];
has[tot][]=id;
fa[id]=tot;
}
}else {
int p;
if(bc==) p=;
else p=;
if(ch[x][p]){
int z=ch[x][p];
// cout<<" z "<<z<<endl;
if(vis[z]){
++tot;
ch[x][p]=tot;fa[tot]=x;
ch[tot][p]=z;fa[z]=tot;
ch[tot][p^]=id;fa[id]=tot;
has[tot][p^]=id;
has[tot][p]=has[z][];
}else{
nowsz=sz[z];
fin(z,id);
}
}else{
assert(->);
}
}
}
int main(){
rd(n);
tot=n;
++tot;
has[tot][]=ch[tot][]=;fa[]=tot;
has[tot][]=ch[tot][]=;fa[]=tot;
for(reg i=;i<=n;++i){
// cout<<"ii ----------------------- "<<i<<endl;
memset(vis,,sizeof vis);
memset(sz,,sizeof sz);
// for(reg j=1;j<i;++j) vis[j]=1;
nowsz=*(i-)-;
// cout<<" vis[55] "<<vis[55]<<endl;
fin(tot,i);
// for(reg i=1;i<=tot;++i){
// cout<<i<<" : fa "<<fa[i]<<" ls "<<ch[i][0]<<" rs "<<ch[i][1]<<" hasls "<<has[i][0]<<" hasrs "<<has[i][1]<<endl;
// }
}
// prt(fa,1,tot);
printf("-1\n");
for(reg i=;i<=tot;++i){
if(!fa[i]) fa[i]=-;
ot(fa[i]);
}
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
*/

交互题都是要找到一个方向去构造的

比如这个就是增量构造

10*n,类似nlogn,二分?

考虑定位,发现点分治最合适了。

CF772E Verifying Kingdom的更多相关文章

  1. ZJOI2018游记Round1

    广告 ZJOI2018Round2游记 All Falls Down 非常感谢学弟学妹们捧场游记虽然这是一篇假游记 ZJOI Round1今天正式落下帷幕.在这过去的三天里遇到了很多朋友,见识了很多有 ...

  2. 「WC2018」即时战略

    「WC2018」即时战略 考虑对于一条链:直接随便找点,然后不断问即可. 对于一个二叉树,树高logn,直接随便找点,然后不断问即可. 正解: 先随便找到一个点,问出到1的路径 然后找别的点,考虑问出 ...

  3. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  4. 拓扑排序 --- hdu 4948 : Kingdom

    Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  5. codeforces 613D:Kingdom and its Cities

    Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...

  6. HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  8. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  9. HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

随机推荐

  1. PAT甲级——A1050 String Subtraction

    Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be the remaining string after taking ...

  2. IO流14 --- 打印流的使用 --- 技术搬运工(尚硅谷)

    PrintStream 字节打印流PrintWriter 字符打印流 @Test public void test9() throws Exception { FileOutputStream fos ...

  3. Python爬虫笔记【一】模拟用户访问之Tesseract-ocr验证码训练(5)

    验证码处理之后就需要对处理的验证码进行识别训练,这里用Tesseract-ocr工具进行识别,用jTessBoxeditor进行训练生成模板. 一,对图片进行处理 利用上一篇代码对图片进行降噪处理,得 ...

  4. LA2965 Jurassic Remains

    Jurassic Remains https://vjudge.net/problem/UVALive-2965 Paleontologists in Siberia have recently fo ...

  5. Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal前序与中序构造二叉树

    根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15 ...

  6. stream的map用法

    List<String> list = new ArrayList<>();list.add("1");list.add("2");li ...

  7. 使用koa-body中间件后DELETE请求中ctx.request.body内容为空

    gitbook浏览此随笔 出现场景 在使用koa-body 做文件上传的时候,发现使用DELETE请求时,request.body中的内容为空对象{} app.js //code... const K ...

  8. 零开始Android逆向教程(一)——初探Android逆向

    这段时间因为某些业务驱动,开始研究一些逆向相关的东西,浏览了下其所包含的大致内容,发现真是一个新大陆,跟之前耳听目染过的一些门面介绍完全不是一个层级的,真正的印证了下手难这一说法.   谨此以本文开始 ...

  9. 计算机组成原理(电脑硬件&语言分类)

    计算机组成原理 一.电脑硬件配置 CPU :中央处理器(人类的大脑) -飞机 内存:存放一些临时数据(人类的短暂记忆-右脑) -高铁 硬盘:存储永久数据(左脑-长期记忆) - 汽车 输入输出:键盘鼠标 ...

  10. ZooKeeper的分布式锁实现

    分布式锁一般有三种实现方式: 1. 数据库乐观锁: 2. 基于Redis的分布式锁: 3. 基于ZooKeeper的分布式锁. 本篇博客将介绍第三种方式,基于Zookeeper实现分布式锁.虽然网上已 ...