Codeforces.1129E.Legendary Tree(交互 二分)
\(Description\)
有一棵\(n\)个点的树。你需要在\(11111\)次询问内确定出这棵树的形态。每次询问你给定两个非空且不相交的点集\(S,T\)和一个点\(u\),交互库会告诉你满足\(x\in S,y\in T\),且\(x\to y\)经过了\(u\)的点对\((x,y)\)的数量。
\(n\leq500\)。
\(Solution\)
不妨假设以\(1\)为根。首先如果想知道\(y\)是否在\(x\)的子树内,询问\(S=\{1\},T=\{y\},u=x\)就可以了(同样可以扩展到某点集中有多少个点在\(x\)子树内)。
那么对于每个点\(i\),询问\(S=\{1\},T=\{2,3,...,n\},u=i\),就可以知道\(i\)子树的大小\(size_i\)。
有什么用呢。。把所有点按\(size_i\)从大到小排序,那么该序列中每个点的父节点一定在它的左边。
(PS:这个序列还可以增量构造出来:考虑在已有\(1...i\)的序列中加入\(i+1\),二分找到一个最靠右的点\(p\),满足\(a_1,a_2,...,a_p\)没有点在\(i+1\)的子树中,然后把\(i+1\)插入到\(a_p\)后面即可。需要\(O(n\log n)\)次询问。)
考虑从右往左扫这个序列,对每个节点找出它直属的儿子。
假设当前是点\(i\),设\(i\)后面还没有找到父亲的点集是\(P\)。首先查一次\(P\)中是否没有点在\(i\)的子树中。\(S=\{1\},T=P,u=i\)询问一次即可。
若\(P\)中存在\(i\)子树内的点,可以二分找出\(P\)中最靠左的一个\(i\)的儿子\(P_j\),连边\((i,p)\)。然后再对\(P'=\{P_{j+1},P_{j+2},...\}\)继续重复上边过程即可。
询问次数\(O(n\log n)+2n\)。(数据实测最多\(<5500\))(有\(200\)组数据=-=)
#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
#define pc putchar
#define Flush() fflush(stdout)
#define gc() getchar()
typedef long long LL;
const int N=505;
int id[N],sz[N],fa[N];
inline int read()
{
int now=0,f=1;register char c=gc();
for(;!isdigit(c);c=='-'&&(f=-1),c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now*f;
}
inline bool cmp(int a,int b)
{
return sz[a]>sz[b];
}
int Query_Size(int x,int n)
{
printf("1\n1\n%d\n",n-1);
for(int i=2; i<=n; ++i) printf("%d ",i);
return printf("\n%d\n",x),Flush(),read();
}
int Query_Exist(int x,const std::vector<int> &vec,int r)//vec中存在x子树中的点
{
printf("1\n1\n%d\n",r);
for(int i=0; i<r; ++i) printf("%d ",vec[i]);
return printf("\n%d\n",x),Flush(),read();
}
int main()
{
const int n=read();
for(int i=1; i<=n; ++i) id[i]=i;
sz[1]=n;
for(int i=2; i<=n; ++i) sz[i]=Query_Size(i,n);
std::sort(id+1,id+1+n,cmp);
std::vector<int> vec;
for(int i=n; i; --i)
{
int x=id[i];
std::vector<int> P=vec,tmp;
while(!P.empty()&&Query_Exist(x,P,P.size()))
{
int l=1,r=P.size(),mid;
while(l<r)
if(Query_Exist(x,P,mid=l+r>>1)) r=mid;
else l=mid+1;
fa[P[--l]]=x;
auto it=P.begin();
while(l--) tmp.push_back(*it++);
P.erase(P.begin(),++it);
}
for(auto v:P) tmp.push_back(v);
vec=tmp, vec.push_back(x);
}
puts("ANSWER");
for(int i=2; i<=n; ++i) printf("%d %d\n",fa[i],i);
return 0;
}
Codeforces.1129E.Legendary Tree(交互 二分)的更多相关文章
- Codeforces 1129E - Legendary Tree(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 考虑以 \(1\) 为根,记 \(siz_i\) 为 \(i\) 子树的大小,那么可以通过询问 \(S=\{2,3,\cdots,n\}, ...
- Codeforces.714D.Searching Rectangles(交互 二分)
题目链接 \(Description\) 在一个\(n*n\)的二维平面中有两个不相交的整点矩形,每次可以询问两个矩形有几个完全在你给出的一个矩形中.200次询问内确定两个矩形坐标. \(Soluti ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...
- Codeforces.862D.Mahmoud and Ehab and the binary string(交互 二分)
题目链接 \(Description\) 有一个长为\(n\)的二进制串,保证\(01\)都存在.你可以询问不超过\(15\)次,每次询问你给出一个长为\(n\)的二进制串,交互库会返回你的串和目标串 ...
- Codeforces.810D.Glad to see you!(交互 二分)
题目链接 \(Description\) 有一个大小为\(k\)的集合\(S\),元素两两不同且在\([1,n]\)内.你可以询问不超过\(60\)次,每次询问你给出\(x,y\),交互库会返回\(\ ...
随机推荐
- Oracle ORA-08104报错处理方法及注意事项
[环境介绍] 系统环境:IBM P740 8205-E6C (AIX) + 11.2.0.3.0 Oracle RAC [背景介绍] 故障描述:数据库表空间超过90%,无法进行扩容表空间,需要业务侧清 ...
- mongodb增加新字段报错解决方法
今天想在项目的一个集合里增加一个新字段 db.article.update({},{$set:{status:0}},{multi:true}) multi : 可选,mongodb 默认是false ...
- JS+CSS实现弹出全屏灰黑色透明遮罩效果的方法
本文实例讲述了js+CSS实现弹出一个全屏灰黑色透明遮罩效果的方法.分享给大家供大家参考.具体分析如下: 在众多的网站都有这样的效果,当进行一定的操作之后,会弹出一个灰黑色的半透明的遮罩,在上面可以操 ...
- LeetCode刷题-004两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1:nums1 = [1, 3]num ...
- sqli注入--利用information_schema配合双查询报错注入
目录 sqli-labs 5.6双查询报错注入通关 0x01 获取目标库名 0x02 获取库中表的数量 0x03 获取库中表名 0x04 获取目标表中的列数 0x05 获取目标表的列名 0x06 从列 ...
- 基于YOLOv3和Qt5的车辆行人检测(C++版本)
概述 YOLOv3: 车辆行人检测算法 GitHub Qt5: 制作简单的GUI OpenCV:主要用于putText.drawRec等 Step YOLOv3检测结果 Fig 1. input im ...
- SVM小白教程(1):目标函数
关于 SVM(支持向量机),网上教程实在太多了,但真正能把内容讲清楚的少之又少.这段时间在网上看到一个老外的 svm 教程,几乎是我看过的所有教程中最好的.这里打算通过几篇文章,把我对教程的理解记录成 ...
- Java基础12-工具类;变长参数;IO
作业解析 取出整数的16进制表示形式 \u00ff /** * int2hex * */ public static String int2hex(int i) { String str = &quo ...
- First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- 解决sqlite 删除记录后数据库文件大小不变
最的做的项目中要有到sqlite数据存储,写了测试程序进行测试,存入300万条记录,占用flash大小为 86.1M,当把表中的记录全部删除后发后数据库文件大小依然是 86.1M: 原因是:sqlit ...