Codeforces Round #359 (Div. 2) D - Kay and Snowflake
题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点。
定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点的个数的两倍不会超过n。
性质 1 :树中所有点到某个点的距离和中,到重心的距离和是最小的,如果有两个距离和,他们的距离和一样。
性质 2 :把两棵树通过某一点相连得到一颗新的树,新的树的重心必然在连接原来两棵树重心的路径上。
性质 3 :一棵树添加或者删除一个节点,树的重心最多只移动一条边的位置。
#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define read(x) scanf("%d",&x)
#define sread(x) scanf("%s",x)
#define dread(x) scanf("%lf",&x)
#define lread(x) scanf("%lld",&x)
using namespace std; const int N=1e6+;
const int M=1e7+;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+; int n,q,sum[N],fa[N],ans[N];
vector<int> edge[N]; int dfs(int u)
{
sum[u]=; ans[u]=u;
for(int v:edge[u])
sum[u]+=dfs(v); for(int v:edge[u])
if(sum[v]*>sum[u])
ans[u]=ans[v]; while((sum[u]-sum[ans[u]])*>sum[u])
ans[u]=fa[ans[u]]; return sum[u];
} int main()
{
read(n); read(q);
for(int i=;i<=n;i++)
{
int pre; read(pre);
edge[pre].push_back(i);
fa[i]=pre;
}
dfs();
while(q--)
{
int u; read(u);
printf("%d\n",ans[u]);
}
return ;
}
/* */
Codeforces Round #359 (Div. 2) D - Kay and Snowflake的更多相关文章
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP
D. Kay and Snowflake After the piece of a devilish mirror hit the Kay's eye, he is no longer int ...
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心
题目链接: 题目 D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题
A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...
- Codeforces Round #359 (Div. 2)C - Robbers' watch
C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #359 (Div. 2) C. Robbers' watch (暴力DFS)
题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b &l ...
- Codeforces Round #359 (Div. 1)
A http://codeforces.com/contest/685/standings 题意:给你n和m,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的 ...
- Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力
A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...
- Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题
B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...
随机推荐
- luogu P3191 [HNOI2007]紧急疏散EVACUATE
传送门 qwq这题好大力 首先可以预处理出每个人到每个门前面那个格子的最早时间,然后答案如果比最小答案大的话也是合法的,所以可以二分最终答案.检查\(mid\)是否合法就考虑每个人要去哪个门才会合法, ...
- MySQL中几个关于时间/时区的变量
一.log_timestamps 1.1.官方解释 log_timestamps: Log timestamp format. Added in MySQL 5.7.2.This variable c ...
- stm32中字节对齐问题(__align(n),__packed用法)
ARM下的对齐处理 from DUI0067D_ADS1_2_CompLib 3.13 type qulifiers 有部分摘自ARM编译器文档对齐部分 对齐的使用: 1.__align(n ...
- python2的比较函数,cmp
class Shu(object): def __init__(self,ss): self.ss = ss def __str__(self): return '(%s: %s)' % (self. ...
- python - 计算器 程序练习
v1.0 计算器(数据内不含括号方式:) import re def jisuan(a,b,c): sun_count = 0 if c =="+": sun_count = st ...
- 实用技能之Python打包制作成EXE可执行程序
制作环境:Andconda3,python3.6 一.安装pyInstaller 方式一): 在命令行输入:pip install pyinstaller 方式二): ① 下载pyInstalle ...
- oracle_集合函数
查询10和20号部门的员工 SQL> 1. select * from emp where deptno in (10,20); SQL> 2. select * from emp whe ...
- Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- UML和模式应用5:细化阶段(8)---逻辑架构和UML包图
1.前言 本章是从面向分析的工作过度到软件设计 典型的OO系统设计的基础是若干架构层,如UI层.应用逻辑(领域)层 本章简要考察逻辑分层架构和相关UML表示法 2.逻辑架构和层 逻辑架构 逻辑架构是软 ...
- 关于GTID模式下备份时 --set-gtid-purged=OFF 参数的实验【转】
刚刚听了吴老师是复制章节课程,对于GTID模式下备份数据--set-gtid-purged=OFF 参数有些不理解,于是乎做了实验,加深理解,得出些结论,如有错漏请批评指正! 部分备份: [root@ ...