Codeforces 1129 E.Legendary Tree

解题思路

这题好厉害,我来复读一下官方题解,顺便补充几句。

首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{2\dots n\},i),i\in[2,n]​\) ,来得到以 \(1​\) 为根树的所有节点的子树大小。

然后考虑从子树大小最小的节点开始,为每一个节点找它们的儿子,由于每个节点儿子的子树大小,一定小于这个节点的子树大小,所以可以按照子树大小从小到大排序,每个节点的儿子就一定在其前面。

假设已经找到了一个节点的儿子,那么考虑把这个节点当前找到的这个儿子在序列中删除,对于序列中在其后面的那个节点来说,其前面除了它的儿子以为所有点都不在它的子树中。

那么对于当前做到的序列的第 \(x\) 个位置,可以二分一个位置 \(mid\) ,询问 \((S=\{1\},T=\{a_1\dots a_{mid}\},x)\) ,第一个返回结果不是 \(0\) 的 \(mid\) 一定 \(x\) 的儿子,这样重复做下去直到还原总询问次数是 \(O(n-1+(n-1)logn)\)。

code

/*program by mangoyang*/
#pragma GCC optimize("inline", "Ofast")
#include <bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int ch = 0, f = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int N = 1005;
vector<int> S, T;
vector<pair<int, int> > Edge;
int sz[N], a[N], del[N], n;
inline int query(vector<int> S, vector<int> T, int x){
printf("%d\n", (int) S.size());
for(int i = 0; i < (int) S.size(); i++)
printf("%d ", S[i]);
printf("\n%d\n", (int) T.size());
for(int i = 0; i < (int) T.size(); i++)
printf("%d ", T[i]);
printf("\n%d\n", x);
fflush(stdout);
int ans = 0;
return read(ans), ans;
}
inline bool cmp_size(int x, int y){
return sz[x] < sz[y];
}
inline int check(int mid, int x){
T.clear();
for(int i = 1; i <= mid; i++)
if(!del[i]) T.push_back(a[i]);
if(!(int)T.size()) return 0;
return query(S, T, x) > 0;
}
int main(){
read(n);
S.push_back(1);
for(int i = 2; i <= n; i++) T.push_back(i);
sz[1] = n;
for(int i = 2; i <= n; i++)
sz[i] = query(S, T, i);
for(int i = 1; i <= n; i++) a[i] = i;
sort(a + 1, a + n + 1, cmp_size);
for(int i = 1; i < n; i++){
int u = a[i];
if(sz[u] == 1) continue;
while(1){
int l = 1, r = i - 1, pos = -1;
while(l <= r){
int mid = (l + r) >> 1;
if(check(mid, u)) pos = mid, r = mid - 1;
else l = mid + 1;
}
if(pos == -1) break;
del[pos] = 1;
Edge.push_back(make_pair(u, a[pos]));
}
}
for(int i = 1; i < n; i++)
if(!del[i]) Edge.push_back(make_pair(a[i], 1));
puts("ANSWER");
for(int i = 0; i < (int) Edge.size(); i++)
printf("%d %d\n", Edge[i].first, Edge[i].second);
fflush(stdout);
return 0;
}

Codeforces 1129 E.Legendary Tree的更多相关文章

  1. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  2. Codeforces 1129 D. Isolation

    Codeforces 1129 D. Isolation 解题思路: 令 \(f(l,r)\) 为 \([l,r]\) 中之出现一次的元素个数,然后可以得到暴力 \(\text{dp}\) 的式子. ...

  3. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  4. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

  6. Codeforces 1129E - Legendary Tree(思维题)

    Codeforces 题面传送门 & 洛谷题面传送门 考虑以 \(1\) 为根,记 \(siz_i\) 为 \(i\) 子树的大小,那么可以通过询问 \(S=\{2,3,\cdots,n\}, ...

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

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

  8. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  9. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

随机推荐

  1. 【STSRM13】绵津见

    [算法]扫描线:差分+树状数组 [题意]转化模型后:求每个矩形覆盖多少点和每个点被多少矩形覆盖.n<=10^5. [题解]经典的扫描线问题(二维偏序,二维数点). 数点问题 将所有询问离线并离散 ...

  2. form表单设置input文本属性只读,不可更改

    记住一条好用的,设置readonly属性为true <input     readonly=''true"> 更多方法,转载: http://www.jb51.net/web/6 ...

  3. NYOJ 35 表达式求值 (字符串处理)

    题目链接 描述 ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧. 比如输入:&quo ...

  4. ASP.NET MVC EF直接更新数据(不需查询)

    EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET ASP.NET MVC 项目会自动 ...

  5. DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead extract-text-webpack-plugin 提取css报错

    深入浅出Webpack 1-5 使用pulugin extract-text-webpack-plugin 提取css报错 DeprecationWarning: Tapable.plugin is ...

  6. 南邮综合题writeup

    http://teamxlc.sinaapp.com/web3/b0b0ad119f425408fc3d45253137d33d/index.php fuckjs直接console得到地址 http: ...

  7. docker之安装和基本使用(一)

    前言 开始折腾docker. 主要概念 容器:独立运行的一个或一组应用,与其他应用完全独立. 镜像:用于创建 Docker容器的模板. 仓库:用于收纳镜像文件,可以理解为代码控制中的代码仓库 注意: ...

  8. Battery Charging Specification 1.2 中文详解 来源:www.chengxuyuans.com

    1. Introduction 1.1 Scope 规范定义了设备通过USB端口充电的检测.控制和报告机制,这些机制是USB2.0规范的扩展,用于专用 充电器(DCP).主机(SDP).hub(SDP ...

  9. 64_t1

    TOPCOM-0.17.8-2.fc26.x86_64.rpm 13-Feb-2017 22:09 269054 TOPCOM-devel-0.17.8-2.fc26.i686.rpm 13-Feb- ...

  10. PlantUML——1.Hello

    官网: http://www.plantuml.com/ 第一步: 下载 plantuml.jar文件: 第二步:创建一个demo.txt文件(与plantuml.jar在同一目录),内容如下: @s ...