题目链接:Educational Codeforces Round 25 G. Tree Queries

题意:

给你一棵树,一开始所有的点全是黑色,有两种操作。

1 x 将x这个点变为黑色,保证第一个操作是这个。

2 x 询问x到任意黑色的点的简单路径上的最小节点编号。

题解:

首先将一个变为黑色的点当成树根,然后dfs一下,预处理出所有点的答案。

然后开一个变量记录一下当前变黑的点的答案cur=min(cur,dp[x])。

每次询问的时候答案就是min(cur,dp[x])。

如果觉得很神奇,自己模拟一遍就知道了。

 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=(a);i<=(b);++i)
 3 using namespace std;
 4
 5 const int N=1e6+7;
 6 vector<int>g[N];
 7 int n,m,x,y,dp[N];
 8
 9 void dfs(int x,int f)
10 {
11     dp[x]=min(x,dp[f]);
12     for(int &it:g[x])if(it!=f)dfs(it,x);
13 }
14
15 int main(){
16     scanf("%d%d",&n,&m);
17     F(i,2,n)
18     {
19         scanf("%d%d",&x,&y);
20         g[x].push_back(y);
21         g[y].push_back(x);
22     }
23     dp[0]=N;
24     int last=0,cur=N;
25     F(i,1,m)
26     {
27         scanf("%d%d",&x,&y);
28         y=(y+last)%n+1;
29         if(x==1)
30         {
31             if(i==1)dfs(y,0);
32             cur=min(cur,dp[y]);
33         }else printf("%d\n",last=min(cur,dp[y]));
34     }
35     return 0;
36 }

View Code

转载于:https://www.cnblogs.com/bin-gege/p/7205090.html

Educational Codeforces Round 25 G. Tree Queries相关推荐

  1. Educational Codeforces Round 25 E. Minimal Labelshdu1258

    这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...

  2. Educational Codeforces Round 32 G. Xor-MST 01tire + 分治 + Boruvka

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn序列aaa,每两个点之间的边权为ai⊕aja_i\oplus a_jai​⊕aj​,问你最小生成树的权值是多少. n≤2e5,ai< ...

  3. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  4. Educational Codeforces Round 25

    这一场是暑期的第一场,做了4个题,被HACK两个,都是很粗心的错误,手生的问题. [A]Binary Protocol 题意:给你一串字符串,只有0和1.用m个0将字符串分为m+1段,每段字符串中'1 ...

  5. Educational Codeforces Round 25 C. Multi-judge Solving

    题目链接:http://codeforces.com/contest/825/problem/C C. Multi-judge Solving time limit per test 1 second ...

  6. Educational Codeforces Round 39 G Almost Increasing Array

    传送门 非减子序列 普通lcs Dp TLE ,换成upper_bound 竟然过了,神奇 #include <bits/stdc++.h> using namespace std; ty ...

  7. Educational Codeforces Round 21 G Anthem of Berland

    题目链接 Berland has a long and glorious history. To increase awareness about it among younger citizens, ...

  8. Educational Codeforces Round 54 (Rated for Div.2)

    Educational Codeforces Round 54 (Rated for Div.2) D. Edge Deletion 题意:一张n个点的无向图,保留其中k条边,使得有尽可能多的点与1的 ...

  9. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

最新文章

  1. 终于有人把Python讲清楚了!
  2. java object monitor_Java-线程状态、ObjectMonitor
  3. 算法导论中C语言代码,算法导论-学习笔记与进度
  4. 学习springboot基础必备
  5. xml 转map dom4j
  6. Spring Cloud各组件超时
  7. 遍历某路径下的所有文件
  8. Java 并发编程之 ConcurrentHashMap,ConcurrentSkipListMap
  9. 桌面上的计算机图标一直被选定,常见电脑桌面图标问题十则
  10. python 中的metaclass和baseclasses
  11. smbus使用 树莓派_Linux控制I2C/SMBus设备
  12. 【转】用photoshop批量修改图片尺寸
  13. c语言中rcu缩写,ICU、MICU、EICU、NICU、NCU、CCU、RCU,科室英文缩写大全
  14. 华三服务器bios中查看硬盘,H3C服务器升级BIOS
  15. 计算机量子化学计算焓变,中国科学技术大学超级计算中心
  16. Java实现支付功能(支付宝)
  17. 白岩松:40岁左右的男人,比你想象中更难
  18. 用 Wasm 为数据库增加用户定义函数
  19. Python导入第三方库以及报错 Error..........处理办法
  20. C语言编码图书借阅系统

热门文章

  1. git pull keeping local changes
  2. Lesson 4 Part 2 Softmax Regression
  3. 基于病害区域图像的植物病害识别深度学习(创新点好理解)
  4. 卷积神经网络处理猫和狗图片
  5. [MATLAB]设置坐标轴标签
  6. app error login.php,如何解决uniapp登录错误提示问题
  7. PDF文档解析,公司公告信息抽取(附数据集)
  8. php可逆加密解密函数,php 好用可逆的 加密解密 函数。
  9. 红外接收头图片_亿光红外线接收头IRM-56384内部构造原理图
  10. 每天执行一次批处理_关于静态批处理/动态批处理/GPU Instancing /SRP Batcher的详细剖析...