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. 数据交换格式之 - Json

    Json简介: JSON是JavaScript对象表示法,是一种与语言无关的数据交换的格式,是一种完全独立于语言的文本格式. 使用ajax进行前后台数据交换,移动端与服务端的数据交换. web客户端和 ...

  2. python函数当容器

    def func(): pass func2 = func func2() i = [func,func2] for a in i: a() 函数名就是内存地址,加()代表执行

  3. Simple implementation and results of genetic algorithm.

    This experiment was done for the final assignment of my Professional English class. This part has be ...

  4. Alpha通道是什么意思,和rgb通道有什么区别

    Alpha通道是什么意思,和rgb通道有什么区别 Alpha通道: 阿尔法通道是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度信息,定义透明.不透明和半透明区域,其中白表示不透明,黑表示 ...

  5. Wsgi研究

    //转载自http://blog.kenshinx.me/blog/wsgi-research/ wsgi是一个搞web开发的pythoner必须了解的内容,之前也零散的看过一些文章,但总感觉好多概念 ...

  6. Apache Flink 1.9重磅发布!首次合并阿里内部版本Blink重要功能

    8月22日,Apache Flink 1.9.0 版本正式发布,这也是阿里内部版本 Blink 合并入 Flink 后的首次版本发布.此次版本更新带来的重大功能包括批处理作业的批式恢复,以及 Tabl ...

  7. Ionic Cordova 环境配置window

    1.安装java jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.安 ...

  8. 小希的迷宫 HDU - 1272 (并查集)

    思路: 当图中的集合(连通子图)个数为1并且边数等于顶点数-1(即改图恰好为一棵生成树)时,输出Yes. 此题的坑:(1) 如果只输入0 0算作一组数据的话答案应该输出Yes (2) 输入数据可能并不 ...

  9. jquery 获取图片宽高为0的问题

    原理:页面加载完了,图片不一定加载完了. $(function(){ $("img").on("load",function(){ //核心 var w = $ ...

  10. “Bootstrap做的响应式菜单在iPhone上点击不了二级菜单“的解决办法!

    只需把把点击的a(被点击的)变成button即可.