当前位置:网站首页>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;
}
边栏推荐
- Solve the timeout of dbeaver SQL client connection Phoenix query
- Pytorch学习笔记--常用函数总结3
- Is it safe to open futures online? Which company has the lowest handling charge?
- The difference between Apple buy in and apple pay
- In depth: micro and macro tasks
- 2021上海市赛-D-卡特兰数变种,dp
- Spark SQL common time functions
- JVM dynamic bytecode technology details
- matlab--CVX优化工具包安装
- Notes on inputview and inputaccessoryview of uitextfield
猜你喜欢

Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)

Are you ready to break away from the "involution circle"?

Games101 review: Transformation

ML - 语音 - 高级语音模型

matlab 优化工具 manopt 安装

你准备好脱离“内卷化怪圈”了吗?

matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值

理解“平均负载”

Spark submission parameters -- use of files

ML - Speech - advanced speech model
随机推荐
CF566A-贪心+字典树
Box avoiding mouse
苹果内购和Apple Pay 的区别
Flex layout
SVD奇异值分解推导及应用与信号恢复
C # carefully sorting out key points of knowledge 11 entrustment and events (recommended Collection)
JVM parameter configuration details
The difference between Apple buy in and apple pay
p4552-差分
盒子躲避鼠标
Seata中jdbc下只有一个lib嘛?
ML - 自然语言处理 - 基础知识
你准备好脱离“内卷化怪圈”了吗?
The number of query results of maxcompute SQL is limited to 1W
MySQL transactions and mvcc
Brain racking CPU context switching
How spark gets columns in dataframe --column, $, column, apply
GAMES101复习:线性代数
How to update JSON values in the database?
C#精挑整理知识要点12 异常处理(建议收藏)