CF1305D Kuroni and the Celebration

题意:

给你一棵有 n 个节点的树。对于每次询问,提交两个点,评测机会返回这两个点的 LCA。求树根。

询问格式为 ? u v,此时评测机会返回 u 和 v 的 LCA。

提交格式为 ! x,表示你得出树根为点 x。

你可以最多询问 ⌊n2⌋\lfloor \frac{n}{2}\rfloor⌊2n​⌋次

题解:

如果一个叶子和另一个点的LCA是这个叶子,那么这个叶子一定为根
否则,这个叶子一定不是根
所以我们可以每次询问两个叶子的LCA即可,如果是其中一个点,则那个点就是根;否则删除这个两个点,有可能得到新的叶子节点,用得到的LCA去和剩下的叶子节点继续求LCA
最坏情况每次删除两个叶子节点,那么⌊n2⌋\lfloor \frac{n}{2}\rfloor⌊2n​⌋次后最多只剩下一个点,这就是根

代码:

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{#ifdef ONLINE_JUDGE
#elsestartTime = clock ();freopen("data.in", "r", stdin);
#endif
}
void Time_test()
{#ifdef ONLINE_JUDGE
#elseendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn=1020;
vector<int>vec[maxn];
int d[maxn];
int main()
{//rd_test();int n;read(n);for(int i=1;i<n;i++){int u,v;read(u,v);vec[u].push_back(v);vec[v].push_back(u);d[u]++;d[v]++;}queue<int>q;for(int i=1;i<=n;i++){if(d[i]==1)q.push(i);}int lca;while(!q.empty()){//      if(q.size()==0)printf("! %d\n",lca);int x=q.front();q.pop();if(q.size()==0)return printf("! %d\n",x),0;int y=q.front();q.pop();printf("? %d %d\n",x,y);cin>>lca;if(lca==x)return printf("! %d\n",x),0;else if(lca==y)return printf("! %d\n",y),0;for(auto v:vec[x])if((--d[v])==1)q.push(v);for(auto v:vec[y])if((--d[v])==1)q.push(v);//     if(tot>n/2)break;}
//  cout<<lca<<endl;//Time_test();
}

CF1305D Kuroni and the Celebration相关推荐

  1. Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated) D. Kuroni and the Celebration 交互 + 思维

    传送门 文章目录 题意: 思路: 题意: 给你一颗树,每次可以询问两个点的lcalcalca,询问次数不能超过⌊n2⌋\left \lfloor \frac{n}{2} \right \rfloor⌊ ...

  2. CodeForces - 1305D Kuroni and the Celebration(思维,互动题)

    题目链接:点击查看 题目大意:给出一个由 n 个点组成的树,现在可以询问 n/2 次(向下取整)LCA,确定根节点是哪个节点 题目分析:因为最多只能求 n/2 次lca,每次需要两个节点作为参数,也就 ...

  3. Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!)

    Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!) 题号 题目 知识点 A Kuroni and the Gifts ...

  4. APOC 15 Years Celebration

    最近很忙,没有及时更新博客,也没有参加各种活动,唯一的活动就是接下来要讲的APOC 15 Years Celebration.不知不觉,自己也加入APOC有一年多了,正如大家所说"岁月是把杀 ...

  5. Kuroni and Impossible Calculation CodeForces - 1305C(鸽巢原理)

    To become the king of Codeforces, Kuroni has to solve the following problem. He is given n numbers a ...

  6. Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)

    To become the king of Codeforces, Kuroni has to solve the following problem. He is given n numbers a ...

  7. Ozon Tech Challenge 2020 (Div.1 + Div.2) F. Kuroni and the Punishment 随机化

    传送门 文章目录 题意: 思路: 题意: 给你nnn个数,每次操作可以选择将某个数+1,−1+1,-1+1,−1,求最少进行多少次操作使得所有数都为正数且gcd>1gcd>1gcd> ...

  8. Ozon Tech Challenge 2020 (Div.1 + Div.2) E.Kuroni and the Score Distribution 构造

    传送门 文章目录 题意: 思路: 题意: 思路: 不难想到,长度为nnn的数组最多的满足条件的三元组序列是1,2,3....,n1,2,3....,n1,2,3....,n,对于每一个位置贡献为i−1 ...

  9. Ozon Tech Challenge 2020 (Div.1 + Div.2) C. Kuroni and Impossible Calcul 抽屉原理

    传送门 文章目录 题意: 思路: 题意: 给你一个数组ana_nan​,求∏1≤i<j≤n∣aj−ai∣modm\begin{matrix} \prod_{1\le i<j\le n} | ...

最新文章

  1. 运维自动化之Cobbler安装配置
  2. 常用 Git 命令清单(转)
  3. 无限乱斗机器人ap出装_LOL:无限火力仅剩4天,这5个英雄一定要尝试!
  4. Hightchart y轴不允许显示小数
  5. 基于torch.nn.functional.conv2d实现CNN
  6. java实现人脸识别源码【含测试效果图】——Service层(IUserService)
  7. Apple Music已达成目标 库中歌曲全部达到无损标准
  8. Android 屏幕刷新机制
  9. [转载] python数学计算模块之math常用函数学习使用
  10. 《精通LabVIEW虚拟仪器程序设计与案例实现》一2.6 VI的创建与编辑
  11. word2007修复工具_使用Word 2007作为博客工具
  12. NetDxf读取DXF文件
  13. 中文简体与繁体的转换
  14. 阿里云商标注册申请进度查询方法
  15. 串扰——Crosstalk
  16. R语言中的函数1:outer(张量积)
  17. [CF1336F]Journey
  18. Intent.createChooser方法源码解析(按我的解析,只有一个Intent是不会启动的??Android源码7.1.2)
  19. C++链表的排序实现
  20. Zbrush 风格化头发的工具

热门文章

  1. 如何用数学方法估算一个女生前男友的数量?
  2. 全世界的狗都没有“生殖隔离” | 今日趣图
  3. 粉丝回馈 | 用30天换来了这辈子独一无二的纪念
  4. 明天放假,我放价!一个国庆假期教你学会数学建模
  5. php prettyprinter,gdb运行时错误:prettyprinter已注册:libstdc++v6
  6. spring 单例 获取多例的位_Spring系列第6篇:玩转bean scope,避免跳坑里!
  7. php 建立自己的框架,利用 Composer 一步一步构建自己的 PHP 框架(一)——基础准备...
  8. mysql连接主备_mysql主备配置方法
  9. 机器人等级考试一级教具_全国青少年机器人技术等级考试(一级):基本结构...
  10. java final 修改_“无法改变的设计”——浅谈Java中的final关键字