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. pycharm 安装与激活

    ---恢复内容开始--- 环境:Windows 专业版 1.下载安装 1.到官网下载专业版(专业版功能更全(但要激活码),社区版免费) 2.下载完后双击.exe 文件进行安装 NEXT 下一步 NEX ...

  2. [群晖] DSM6.2用winscp通过root权限登录

    http://www.nas1.cn/thread-86048-1-1.html 以前DSM6.0的时候可以通过改root密码的方式,来通过winscp来登录nas,这样可以获得最高权限可以任意修改文 ...

  3. 装饰者模式(Decorator、Compoment)(早餐销售装饰,动态添加职责)

    适用于以下情况: (1)需要扩展一个类的功能,或给一个类添加附加职责. (2)需要动态的给·一个对象添加功能,这些功能可以再动态的撤销. (3)需要增加由一些基本功能的排列组合而产生的非常大量的功能, ...

  4. centos 6.8 搭建禅道 Linux一件安装、进程自起

    禅道官网:http://www.zentao.net/ linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅道.Linux 64位一键安装包(适用于L ...

  5. Gym - 102082G

    Gym - 102082Ghttps://vjudge.net/problem/2198225/origin对于数列中任意一个数,要么从最左边到它不递减,要么从最右边到到它不递减,为了满足这个条件,就 ...

  6. import schedule ImportError: No module named schedule

    安装pip sudo apt-get install python-pip 安装schedule模块 pip install schedule PS: 如果已经安装pip,可能出现以下问题,按照提示重 ...

  7. HDU2056

    /*  * 指数型母函数  */ #include<cstdio> #define mod 100 typedef long long LL;//杭电需用int64 int powerMo ...

  8. drf作业01

    api\urls from django.conf.urls import url from . import views urlpatterns = [ url(r'^cars/$',views.C ...

  9. 实用Jupyter Notebook扩展工具——提升你的工作效率

    Jupyter Notebook 现已成为数据分析,机器学习的必备工具.因为它可以让数据分析师集中精力向用户解释整个分析过程.通过安装一些扩展工具,可以让你在Jupyter Notebook上的工作效 ...

  10. oracle定制定时执行任务

    1.引言 定制定时执行的任务有两种形式,系统级别和数据库级别, 从操作系统级别来讲, windows系统我们可以使用任务计划来实现, 对于winXP系统,设置步骤如下,开始---设置---控制面板-- ...