Codeforces Round #359 (Div. 2)D. Kay and Snowflake【树的重心】
来源:互联网 发布:黑魂3 黑暗剑数据 编辑:程序博客网 时间:2024/05/16 16:07
- 求某个结点子树的重心
- 情况1:重链上的儿子结点i为根的树结点数小于父结点j为根的树节点数,j为重心
- 情况2:与1相反时,重心必在j到i为跟的树的重心的路径上
/* ***********************************************Author :MaltubEmail :xiang578@foxmail.comBlog :htttp://www.xiang578.com************************************************ */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>//#include <bits/stdc++.h>#define rep(i,a,n) for(int i=a;i<n;i++)#define per(i,a,n) for(int i=n-1;i>=a;i--)#define pb push_backusing namespace std;typedef vector<int> VI;typedef long long ll;const ll mod=1000000007;const int N=300000+10;vector<int>g[N];int n,q,son[N],fa[N],ans[N];void dfs(int x){ son[x]=1; ans[x]=x; for(int i=0; i<g[x].size(); i++) { int y=g[x][i]; dfs(y); son[x]+=son[y]; } for(int i=0;i<g[x].size();i++) { if(son[g[x][i]]*2>son[x]) ans[x]=ans[g[x][i]]; } while(2*(son[x]-son[ans[x]])>son[x]) { ans[x]=fa[ans[x]]; }}int main(){ int t; //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d%d",&n,&q); for(int i=1; i<=n; i++) g[i].clear(); for(int i=2; i<=n; i++) { scanf("%d",&fa[i]); g[fa[i]].push_back(i); } dfs(1); for(int i=0; i<q; i++) { scanf("%d",&t); printf("%d\n",ans[t]); } return 0;}
0 0
- Codeforces Round #359 (Div. 2)D. Kay and Snowflake【树的重心】
- Codeforces Round #359 (Div. 2)D. Kay and Snowflake【树的重心】
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake
- Codeforces Round #359 (Div. 2)D. Kay and Snowflake
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake
- Codeforces Round #359 (Div. 2)D. Kay and Snowflake
- codeforces 686D. Kay and Snowflake 树的重心应用
- Codeforces 686 D Kay and Snowflake【树的重心】
- Codeforces 686 D Kay and Snowflake (树重心)
- Codeforces 685B Kay and Snowflake 树的重心
- Codeforces 685B Kay and Snowflake(树的重心)
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
- 树的重心--cf686d Kay and snowflake
- Kay and Snowflake CodeForces
- Codeforces Round #359 (Div. 1) D. Kay and Eternity ★ ★ ★ ★
- 【24.63%】【codefroces 686D】Kay and Snowflake
- git/github
- 学习Discuz! X3.2记录:快速回复插件涉及到哪些数据库表?
- Navicat Premium 将sqlserver 数据库 导入mysql 中
- Visual Studio Express 在C/C++项目中无法为项目添加新建项
- Android产品研发(十七)-->Hybrid开发
- Codeforces Round #359 (Div. 2)D. Kay and Snowflake【树的重心】
- 【C】IAR Embedded Workbench IDE 中精确延时程序
- 程序设计中,为什么要解耦
- 值类型和引用类型
- AwesomePlayer的消息传递机制
- 1、数据库概述
- MapReduce 中的两表 join 几种方案简介
- 脑残设计--想哪写哪
- 三层代码结构