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. Java中"str1.equals(str2)"和"str1==str2"的区别

    大家好,这是我的第一篇博客,作为即将入职的学生,我现在的心情是既好奇又兴奋,对未知的职场生活充满了无限的憧憬,也想赶紧对大学生活say goodbye,因为自己的能力现在还比较有限,我想通过博客这个平 ...

  2. 【JZOJ5179】【NOI2017模拟6.29】哈哈

    题意 给定一个长度为n的序列,你可以进行若干次操作: 选择一个区间,删掉,并获得Val[Len]的得分,Len为这个区间的长度: 其中这个区间满足: 1.相邻两个数差的绝对值为1 2.每个数都大于相邻 ...

  3. httpclient遇到java.net.URISyntaxException: Illegal character in scheme name at index 0:

    正准备按照大佬的解决办法处理, 看会一条回复,说url有空格 检查了一下,还真是有空格 去除url中的空格,问题解除

  4. 2019-3-8-win10-uwp-一张图说明水平对齐和垂直对齐

    title author date CreateTime categories win10 uwp 一张图说明水平对齐和垂直对齐 lindexi 2019-03-08 10:45:40 +0800 2 ...

  5. Linux时间介绍

    Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟, ...

  6. Luogu P3254 圆桌问题(最大流)

    P3254 圆桌问题 题面 题目描述 假设有来自 \(m\) 个不同单位的代表参加一次国际会议.每个单位的代表数分别为 \(r_i (i =1,2,--,m)\) . 会议餐厅共有 \(n\) 张餐桌 ...

  7. 碰撞的小球 ccf (模拟)

    问题描述 试题编号: 201803-2 试题名称: 碰撞的小球 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐 ...

  8. 前端--HTML简介

    软件开发架构: c/s架构 客户端 服务端 b/s架构 浏览器 服务端 本质:b/s架构也是c/s架构 HTTP协议 超文本传输协议:规定了客户端与服务端之间消息传输的格式 四个特性: 1.基于TCP ...

  9. day18 12.丢失更新介绍与悲观锁

    共享锁在一条记录上是可以加多个的,共享嘛.排它锁的意思是指这条记录上如果有任何其他的锁我排它锁是加不上的,有了排它锁其他锁也是加不上的,唯一的.比如说现在我的记录上没锁,加了排它锁其他人使用不了,我这 ...

  10. day36 11-Hibernate中的事务:当前线程中的session

    如果你没有同一个session开启事务的话,那它两是一个独立的事务.必须是同一个session才有效.它给我们提供一个本地线程的session.这个session就保证了你是同一个session.其实 ...