当前位置:网站首页>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;
}
边栏推荐
- wait()和sleep()的区别理解
- 4PAM在高斯信道与瑞利信道下的基带仿真系统实验
- Reflection - Notes
- Spark SQL common time functions
- Box avoiding mouse
- C#精挑整理知识要点11 委托和事件(建议收藏)
- Phased summary of the research and development of the "library management system -" borrowing and returning "module
- ios 面试题
- Spark SQL null value, Nan judgment and processing
- Is it safe to open futures online? Which company has the lowest handling charge?
猜你喜欢

使用cpolar建立一个商业网站(如何购买域名)

图论及概念

为什么PrepareStatement性能更好更安全?

Idea eye care settings

分布式原理 - 什么是分布式系统

Pytorch学习笔记--SEResNet50搭建

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

Implementation of asynchronous FIFO

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
SQL cultivation manual from scratch - practical part
随机推荐
In depth: micro and macro tasks
How to update JSON values in the database?
Spark partition operators partitionby, coalesce, repartition
ML - Speech - advanced speech model
Args parameter parsing
ML - 自然语言处理 - 关键技术
Notes on inputview and inputaccessoryview of uitextfield
带你详细认识JS基础语法(建议收藏)
The number of query results of maxcompute SQL is limited to 1W
小波变换--dwt2 与wavedec2
SVD奇异值分解推导及应用与信号恢复
UIDocumentInteractionController UIDocumentPickerViewController
Brain racking CPU context switching
matlab 如何保存所有运行后的数据
How to finally generate a file from saveastextfile in spark
Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
wait()和sleep()的区别理解
Understanding the difference between wait() and sleep()
Solve the timeout of dbeaver SQL client connection Phoenix query