Codeforces 685B Kay and Snowflake(树的重心)
来源:互联网 发布:淘宝上买阅读器怎样 编辑:程序博客网 时间:2024/05/17 01:27
题意:
给你一棵树,问你每一颗子树的重心是哪一个节点。
解法:
树的重心有一个性质,当把两棵树合并时,新的树的重心肯定在两颗树重心之间的路径上,所以我们只需要看当前节点到他重儿子重心之间的路径中是否有满足条件的点就可以了,(重儿子请去看树链剖分)因为重儿子是最大的,不在这个路径上找,会让重儿子单独出来,肯定不满足条件。
//// Created by CQU_CST_WuErli// Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.////#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <string>#include <vector>#include <map>#include <queue>#include <stack>#include <set>#include <algorithm>#include <sstream>#define CLR(x) memset(x,0,sizeof(x))#define OFF(x) memset(x,-1,sizeof(x))#define MEM(x,a) memset((x),(a),sizeof(x))#define BUG cout << "I am here" << endl#define lookln(x) cout << #x << "=" << x << endl#define SI(a) scanf("%d", &a)#define SII(a,b) scanf("%d%d", &a, &b)#define SIII(a,b,c) scanf("%d%d%d", &a, &b, &c)const int INF_INT=0x3f3f3f3f;const long long INF_LL=0x7f7f7f7f;const int MOD=1e9+7;const double eps=1e-10;const double pi=acos(-1);typedef long long ll;using namespace std;const int N = 300030;int n, q;int ans[N];vector<int> g[N];int par[N], son[N];int sz[N];void dfs(int fa, int u) { par[u] = fa; sz[u] = 1; son[u] = 0; ans[u] = u; for (auto& v : g[u]) { if (v == fa) continue; dfs(u, v); sz[u] += sz[v]; if (!son[u] || sz[son[u]] < sz[v]) son[u] = v; } if (sz[son[u]] * 2 > sz[u]) { int tmp = ans[son[u]]; while ((sz[u] - sz[tmp]) * 2 > sz[u]) tmp = par[tmp]; ans[u] = tmp; }}int main(int argc, char const *argv[]) {#ifdef LOCAL freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin); // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);#endif while(SII(n, q) == 2) { for (int i = 1; i <= n; i++) { g[i].clear(); ans[i] = -1; } for (int u = 2; u <= n; u++) { int v; SI(v); g[u].push_back(v); g[v].push_back(u); } dfs(-1, 1); while (q--) { int x; SI(x); printf("%d\n", ans[x]); } } return 0;}
0 0
- Codeforces 685B Kay and Snowflake 树的重心
- Codeforces 685B Kay and Snowflake(树的重心)
- codeforces 686D. Kay and Snowflake 树的重心应用
- Codeforces 686 D Kay and Snowflake【树的重心】
- 树的重心--cf686d Kay and snowflake
- Codeforces 686 D Kay and Snowflake (树重心)
- Codeforces Round #359 (Div. 2)D. Kay and Snowflake【树的重心】
- Codeforces Round #359 (Div. 2)D. Kay and Snowflake【树的重心】
- Kay and Snowflake CodeForces
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
- 【codeforces685B686D】【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
- 【24.63%】【codefroces 686D】Kay and Snowflake
- python多行字符串拼接使用小括号
- Codeforces 679C Bear and Square Grid(滑动窗口)
- library cache lock 相关的bug list
- Linux下配置vsftp的若干问题
- Struts2校验器详解
- Codeforces 685B Kay and Snowflake(树的重心)
- 代理模式
- bzoj 1031: [JSOI2007]字符加密Cipher
- Hadoop----集群运维(持续更新...)
- Flume的简单了解:
- .NET 程序集
- MySQL必知必会-5去重,截取和排序
- 彻底理解双重检验锁
- 并发包concurrent的学习(一)--重入锁