Codeforces 题面传送门 & 洛谷题面传送门

考虑以 \(1\) 为根,记 \(siz_i\) 为 \(i\) 子树的大小,那么可以通过询问 \(S=\{2,3,\cdots,n\},T=\{1\}\) 以及每个 \(u\) 得出 \(siz_u\) 的值。

考虑将所有点按 \(siz\) 从小到大排序并维护一个集合 \(st\) 表示目前还没有找到父亲的点的集合,那么我们枚举到一个点 \(x\) 时就在 \(st\) 中二分找到所有父亲为 \(x\) 的点并将它们从 \(st\) 中删除,具体步骤是,我们先二分找出 \(st\) 中编号最小的点,即二分出一个 \(mid\) 后就询问 \(S=st\) 中编号最小的 \(mid\) 个点组成的集合,\(T=\{1\}\),\(u=x\),如果交互库返回的值 \(>0\) 就向左二分,否则向右二分,二分出第一个后再找第二个、第三个……以此类推。删完这些点之后加入将 \(x\) 加入集合,如此进行下去即可。最后扫描到 \(1\) 时就将 \(st\) 中的点的父亲全部设为 \(1\) 并清空 \(st\)

时间复杂度 \(n\log^2n\),询问次数 \(n\log n\),实测在 \(7000\) 左右,可以通过此题的限制。

看到没?什么超纲的算法都没有。所以啊,菜是原罪/kk——Codeforces Round #691 (Div.2) 题解

const int MAXN=500;
int n,siz[MAXN+5],ord[MAXN+5];set<int> st;
bool cmp(int x,int y){return siz[x]<siz[y];}
bool check(int x,int l,int r){
set<int>::iterator it=st.begin();
for(int i=1;i<l;i++) ++it;printf("%d\n",r-l+1);
for(int i=1;i<=r-l+1;i++) printf("%d%c",*it++," \n"[i==r-l+1]);
printf("1\n1\n%d\n",x);fflush(stdout);
int t;scanf("%d",&t);return t>0;
}
int main(){
scanf("%d",&n);siz[1]=n;
for(int i=2;i<=n;i++){
printf("%d\n",n-1);
for(int j=2;j<=n;j++) printf("%d%c",j," \n"[j==n]);
printf("1\n1\n%d\n",i);fflush(stdout);
scanf("%d",&siz[i]);
} for(int i=1;i<=n;i++) ord[i]=i;sort(ord+1,ord+n+1,cmp);
vector<pii> ans;
for(int i=1;i<n;i++){
if(!st.empty()){
int cur=0;vector<int> son;
while(cur<st.size()){
int l=cur+1,r=st.size(),p=st.size()+1;
while(l<=r){
int mid=l+r>>1;
if(check(ord[i],cur+1,mid)) p=mid,r=mid-1;
else l=mid+1;
} if(p!=st.size()+1){
set<int>::iterator it=st.begin();
for(int j=1;j<p;j++) ++it;
son.pb(*it);
} cur=p;
} for(int x:son) st.erase(st.find(x)),ans.pb(mp(x,ord[i]));
} st.insert(ord[i]);
} printf("ANSWER\n");
for(int x:st) ans.pb(mp(x,1));
for(pii p:ans) printf("%d %d\n",p.fi,p.se);
fflush(stdout);
return 0;
}

Codeforces 1129E - Legendary Tree(思维题)的更多相关文章

  1. Codeforces.1129E.Legendary Tree(交互 二分)

    题目链接 \(Description\) 有一棵\(n\)个点的树.你需要在\(11111\)次询问内确定出这棵树的形态.每次询问你给定两个非空且不相交的点集\(S,T\)和一个点\(u\),交互库会 ...

  2. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

  3. Codeforces 675C Money Transfers 思维题

    原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...

  4. Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]

    题目链接:https://codeforces.com/contest/1090/problem/D Vasya had an array of n integers, each element of ...

  5. CodeForce - 1189 D1. Add on a Tree (思维题)

    Note that this is the first problem of the two similar problems. You can hack this problem only if y ...

  6. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. Codeforces 957 水位标记思维题

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  8. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  9. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

随机推荐

  1. float 与 double 类型区别

    https://www.runoob.com/w3cnote/float-and-double-different.html float 单精度浮点数在机内占 4 个字节,用 32 位二进制描述. d ...

  2. Java基础-Java8新特性

    一.Lambda表达式 在了解 Lambda 之前,首先回顾以下Java的方法. Java的方法分为实例方法,例如:Integer的equals()方法: public final class Int ...

  3. 字符串与模式匹配算法(三):KMP算法

    一.KMP算法介绍 KMP算法与前面的MP算法一脉相承,都是充分利用先前匹配的过程中已经得到的结果来避免频繁回溯.回顾一下MP算法,如下图的模式串偏移,当前模式字符串P的左端的p0与目标字符串T中tj ...

  4. Python super(Todo,self).__init__() TypeError: super() argument 1 must be type, not classobj

    示例如下 class A(): def __init__(self):pass class B(A): def __init__(self): super(A, self).__init__() 当调 ...

  5. 基于消息队列 RocketMQ 的大型分布式应用上云最佳实践

    作者|绍舒 审核&校对:岁月.佳佳 编辑&排版:雯燕 前言 消息队列是分布式互联网架构的重要基础设施,在以下场景都有着重要的应用: 应用解耦 削峰填谷 异步通知 分布式事务 大数据处理 ...

  6. 分布式技术-Zookeeper概述

    概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目 在大数据技术生态圈中,zookeeper(动物管理员),Hadoop(大象),Hive(蜜蜂),Pig(猪) ...

  7. k8s入坑之路(5)kube-apiserver详解

    API Server kube-apiserver 是 Kubernetes 最重要的核心组件之一,主要提供以下的功能 提供集群管理的 REST API 接口,包括认证授权.数据校验以及集群状态变更等 ...

  8. 一.Promise入门准备阶段

    一.Promise入门准备阶段 1.区别实例对象呵函数对象 2.两种类型的回调函数(同步与异步) 2.1 同步回调 2.2 异步回调 3.JS的error处理 3.1 错误的类型 3.2 错误处理与错 ...

  9. hdfs command

    hadoop fs -ls hdfs dfs -mkdir -p /user/$(whoami) hdfs dfs -chown -R $(whoami) /user/$(whoami) hdfs d ...

  10. 在java中,怎样把一个double数转换为字符串时,不用科学计数法表示。

    解决方法1: 对Double类型的数字进行 格式化输出 ,相对来说不是很精确 import java.text.DecimalFormat;   public class TestDouble_Str ...