当前位置:网站首页>Cf685b find the center of gravity of each subtree of a rooted tree
Cf685b find the center of gravity of each subtree of a rooted tree
2022-07-25 15:33:00 【Brother Tazi is here】
The main idea of the topic :
The title is very clear .
Topic ideas :
According to the nature of the center of gravity of the tree : Two subtrees merge , The center of gravity of the new tree must be on the connecting line of the centers of gravity of the two subtrees .
So when merging subtrees , Let the center of gravity of the subtrees jump to their ancestors . Until the end of jumping to the center of gravity .
PS: Determine the center of gravity : 2 * The largest subtree sz <= sz[u] that will do .
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define fi first
#define se second
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
int sz[maxn] , res[maxn] , f[maxn] , mx[maxn];
vi e[maxn];
void dfs (int u , int fa){
sz[u] = 1; res[u] = u;f[u] = fa;
for (auto & v : e[u]){
if (v == fa) continue;
dfs(v , u);
sz[u] += sz[v];
mx[u] = max(mx[u] , sz[v]);
}
for (auto & v : e[u]){
if (v == fa) continue;
int now = res[v];
// Burst jump
bool ok = false;
do{
int tmx = max(mx[now] , sz[u] - sz[now]);
if (2 * tmx <= sz[u]){
res[u] = now;
ok = true;
break;
}
now = f[now];
if (ok) break;
}while (now != u);
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
int n , m; cin >> n >> m;
for (int i = 2 ; i <= n ; i++) {
int x; cin >> x;
e[x].pb(i);
}
dfs(1 , 0);
for (int i = 1 ; i <= m ; i++){
int x; cin >> x;
cout << res[x] << endl;
}
return 0;
}
边栏推荐
- 苹果内购和Apple Pay 的区别
- Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
- ML - natural language processing - Key Technologies
- CGO is realy Cool!
- JVM knowledge brain map sharing
- 本地缓存--Ehcache
- 小波变换--dwt2 与wavedec2
- Implementation of asynchronous FIFO
- 分布式原理 - 什么是分布式系统
- HDU3873-有依赖的最短路(拓扑排序)
猜你喜欢

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac

Understanding the execution order of T-SQL query from the execution order of join on and where

Solve the timeout of dbeaver SQL client connection Phoenix query

Games101 review: linear algebra

GAMES101复习:变换

4PAM在高斯信道与瑞利信道下的基带仿真系统实验

Reflection - Notes

ML - natural language processing - Basics

ML - 语音 - 深度神经网络模型

Remember that spark foreachpartition once led to oom
随机推荐
Remember that spark foreachpartition once led to oom
Local cache --ehcache
图论及概念
Args parameter parsing
matlab 优化工具 manopt 安装
Spark submission parameters -- use of files
4PAM在高斯信道与瑞利信道下的基带仿真系统实验
Delayed loading source code analysis:
Spark SQL common time functions
<栈模拟递归>
单例模式3--单例模式
ZOJ - 4114 Flipping Game-dp,合理状态表示
获取键盘按下的键位对应ask码
ML - 语音 - 高级语音模型
PAT甲级1152 Google Recruitment (20 分)
在网页上实现任意格式的音视频快速播放功能的开发总结。
Node learning
CF365-E - Mishka and Divisors,数论+dp
ML - natural language processing - Introduction to natural language processing
数据系统分区设计 - 分区再平衡(rebalancing)