Codeforces 1129E - Legendary Tree(思维题)
考虑以 \(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(思维题)的更多相关文章
- Codeforces.1129E.Legendary Tree(交互 二分)
题目链接 \(Description\) 有一棵\(n\)个点的树.你需要在\(11111\)次询问内确定出这棵树的形态.每次询问你给定两个非空且不相交的点集\(S,T\)和一个点\(u\),交互库会 ...
- CF--思维练习-- CodeForces - 215C - Crosses(思维题)
ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...
- Codeforces 675C Money Transfers 思维题
原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...
- 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 ...
- 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 ...
- codeforces 1140D(区间dp/思维题)
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 957 水位标记思维题
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
随机推荐
- 2.1 OOP & SOLID
OOP & SOLID Implementing DDD highly relies on the Object Oriented Programming (OOP) and SOLID pr ...
- props&attrs provide inject
defineComponent({ props: {// 1 } setup (props, {attrs, emit}) { } }) 一,组件传值: 父传子: 1.如果没有在定义的props中声明 ...
- Google Object detection配置与使用
Google Object detection 前言: 本文记录了使用Google发布的Object detection(July 1st, 2019)接口,完成了对标注目标的检测.参考了很多博文,在 ...
- MySQL:提高笔记-1
MySQL:提高笔记-1 学完基础的语法后,进一步对 MySQL 进行学习 说明:这是根据 bilibili 上 黑马程序员 的课程 mysql入门到精通 后做的笔记 1. 索引 1.1 索引概述 M ...
- 热身训练1 Calculator
题目出处:Calculator 简要题意: 你有一个确定的函数,f(x)=+...*...^...,其中共有n个操作,从左到右依次计算. 共有m次询问,我们每次询问,1.会修改f(x)中的操作:2.输 ...
- 使用Keil下载Hex文件进STM32
前言 初学STM32时,是通过串口1把Hex文件下载进STM32的,需要一个串口模块,而且还要设置BOOT0和BOOT1电平,然后通过FlyMcu软件进行下载,这也是一种不错的方法,这里我要介绍的是使 ...
- Python ImportError: No module named '_tkinter', please install the python3-tk package
ImportError: No module named '_tkinter', please install the python3-tk package 这个问题的原因是使用的python3环境内 ...
- 议题解析与复现--《Java内存攻击技术漫谈》(一)
解析与复现议题 Java内存攻击技术漫谈 https://mp.weixin.qq.com/s/JIjBjULjFnKDjEhzVAtxhw allowAttachSelf绕过 在Java9及以后的版 ...
- OAuth 2.0 的探险之旅
前言 OAuth 2.0 全称是 Open Authorization 2.0, 是用于授权(authorization)的行业标准协议. OAuth 2.0 专注于客户端开发人员的简单性,同时为 W ...
- 双链路接入(双出口)isp运营商(负载分担)
USG作为校园或大型企业出口网关可以实现内网用户通过两个运营商访问Internet,并保护内网不受网络攻击. 组网需求 某学校网络通过USG连接到Internet,校内组网情况如下: 校内用户主要分布 ...