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. 隐藏和显示<td>

    <td id="ifXc"><input type="text" value="1"></td>隐藏$( ...

  2. Spring cloud properties与yml配置说明

    encrypt说明 名称 默 认 描述 encrypt.fail-on-error true 标记说,如果存在加密或解密错误,进程将失败. encrypt.key 对称密钥.作为一个更强大的替代方案, ...

  3. 2019-7-15-win10-uwp-在笔迹开始书写拿到书写移动事件

    title author date CreateTime categories win10 uwp 在笔迹开始书写拿到书写移动事件 lindexi 2019-7-15 8:58:5 +0800 201 ...

  4. [原创]最优化/Optimization文章合集

    转载请注明出处:https://www.codelast.com/ 最优化(Optimization)是应用数学的一个分支,它是研究在给定约束之下如何寻求某些因素(的量),以使某一(或某些)指标达到最 ...

  5. Leetcode532.K-diff Pairs in an Array数组中的K-diff数对

    给定一个整数数组和一个整数 k, 你需要在数组里找到不同的 k-diff 数对.这里将 k-diff 数对定义为一个整数对 (i, j), 其中 i 和 j 都是数组中的数字,且两数之差的绝对值是 k ...

  6. PHP CURL header 设置HOST主机头进行访问并 POST提交數據

    $host = array("Host: act.qzone.qq.com");// 域名不帶http://$data = array(            'aa' => ...

  7. TZ_13_微服务场景Eureka

    1.搭建Eureka的注册中心 1.1Eureka几个时间间隔配置详解 1 >客户端信息上报到eureka服务的时间周期,配置的值越小,上报越频繁,eureka服务器应用状态管理一致性越高 #客 ...

  8. hdu 1505 && hdu1506 &&hdu 2830 && 2870 总结---------DP之状图选最大矩形

    /* 多谢了“闭眼,睁眼” 同学给我一套dp专题,不然真是没接触过这种题型. 做个4个简单的,很高兴有所收获. 2013-08-06 /* HDU 1506 最基础的一道题目,其主要精髓就在于两个数组 ...

  9. Connection reset问题,INFO: I/O exception (java.net.SocketException) caught when processing reques

    困扰我多年的Connection reset问题 第一次出现:是thrift的python client去请求server,发现偶尔出现这个问题 第二次:接入第三方的api,去请求数据时,发现一个接入 ...

  10. Dockerfile 编写

    转: https://blog.fundebug.com/2017/05/15/write-excellent-dockerfile/如何编写最佳的Dockerfile 译者按: Dockerfile ...