CF1009F Dominant Indices 长链剖分
题目传送门
https://codeforces.com/contest/1009/problem/F
题解
长链剖分的板子吧。
令 \(dp[x][i]\) 表示 \(x\) 的子树中的深度为 \(i\) 的点的个数。
那么转移的时候就是一般的长链剖分指针移位来维护。
然后就可以在转移的时候通过被转移的那一位被更新的值来更新当前这个点的最优解就可以了。
时间复杂度 \(O(n)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 1e6 + 7;
int n;
int len[N], son[N];
int a[N], *dp[N], *now, mx[N];
struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }
inline void dfs1(int x, int fa = 0) {
len[x] = 1;
for fec(i, x, y) if (y != fa) dfs1(y, x), smax(len[x], len[y] + 1) && (son[x] = y);
}
inline void init(int x) { dp[x] = now, now += len[x]; }
inline void dfs2(int x, int fa = 0) {
if (son[fa] != x) init(x);
dp[x][0] = 1;
if (son[x]) dp[son[x]] = dp[x] + 1, dfs2(son[x], x), mx[x] = mx[son[x]] + 1;
if (dp[x][mx[x]] == 1) mx[x] = 0;
for fec(i, x, y) if (y != fa && y != son[x]) {
dfs2(y, x);
for (int i = 0; i < len[y]; ++i) if (i + 1 < len[x]) {
dp[x][i + 1] += dp[y][i];
if (dp[x][i + 1] > dp[x][mx[x]] || (dp[x][i + 1] == dp[x][mx[x]] && i + 1 < mx[x])) mx[x] = i + 1;
}
}
}
inline void work() {
dfs1(1);
now = a;
dfs2(1);
for (int i = 1; i <= n; ++i) printf("%d\n", mx[i]);
}
inline void init() {
read(n);
int x, y;
for (int i = 1; i < n; ++i) read(x), read(y), adde(x, y);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
CF1009F Dominant Indices 长链剖分的更多相关文章
- CF1009F Dominant Indices——长链剖分优化DP
原题链接 \(EDU\)出一道长链剖分优化\(dp\)裸题? 简化版题意 问你每个点的子树中与它距离为多少的点的数量最多,如果有多解,最小化距离 思路 方法1. 用\(dsu\ on\ tree\)做 ...
- Codeforces 1009 F. Dominant Indices(长链剖分/树上启发式合并)
F. Dominant Indices 题意: 给一颗无向树,根为1.对于每个节点,求其子树中,哪个距离下的节点数量最多.数量相同时,取较小的那个距离. 题目: 这类题一般的做法是树上的启发式合并,复 ...
- CF 1009 F Dominant Indices —— 长链剖分+指针
题目:http://codeforces.com/contest/1009/problem/F 也可以用 dsu on tree 的做法,全局记录一个 dep,然后放进堆里,因为字典序要最小,所以再记 ...
- 【CF1009F】Dominant Indices(长链剖分)
[CF1009F]Dominant Indices(长链剖分) 题面 洛谷 CF 翻译: 给定一棵\(n\)个点,以\(1\)号点为根的有根树. 对于每个点,回答在它子树中, 假设距离它为\(d\)的 ...
- 【CF1009F】 Dominant Indices (长链剖分+DP)
题目链接 \(O(n^2)\)的\(DP\)很容易想,\(f[u][i]\)表示在\(u\)的子树中距离\(u\)为\(i\)的点的个数,则\(f[u][i]=\sum f[v][i-1]\) 长链剖 ...
- 【CF1009F】Dominant Indices(长链剖分优化DP)
点此看题面 大致题意: 设\(d(x,y)\)表示\(x\)子树内到\(x\)距离为\(y\)的点的个数,对于每个\(x\),求满足\(d(x,y)\)最大的最小的\(y\). 暴力\(DP\) 首先 ...
- CF1009F Dominant Indices(树上DSU/长链剖分)
题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...
- 2019.01.08 codeforces 1009F. Dominant Indices(长链剖分)
传送门 长链剖分模板题. 题意:给出一棵树,设fi,jf_{i,j}fi,j表示iii的子树中距离点iii距离为jjj的点的个数,现在对于每个点iii要求出使得fif_ifi取得最大值的那个jjj ...
- 【Cf Edu #47 F】Dominant Indices(长链剖分)
要求每个点子树中节点最多的层数,一个通常的思路是树上启发式合并,对于每一个点,保留它的重儿子的贡献,暴力扫轻儿子将他们的贡献合并到重儿子里来. 参考重链剖分,由于一个点向上最多只有$log$条轻边,故 ...
随机推荐
- functional-page-navigator 组件
functional-page-navigator 组件:是一个非常强大的组件,用于跳转插件的功能页 functional-page-navigator组件的属性: version:类型 字符串 跳转 ...
- centos安装mycat(支持mysql8连接)
1.参考前文安装jdk 2.官网 http://www.mycat.io/ 或 http://dl.mycat.io/ 下载 mycat1.6.7 release 版本 3.解压安装 cd /usr/ ...
- pom.xml文件设置
一个相对完整的maven配置文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- 应用安全-web安全-WebShell整理
shellcode.aspx <%@ Page Language="C#" AutoEventWireup="true" Inherits="S ...
- python+selenium的frame表单切换
switch_to.frame() 切换frame switch_to.default_content() 切 ...
- python字典-基础
一.解释 像列表一样,“字典”是许多值的集合.但不像列表的下标,字典的索引可以 使用许多不同数据类型,不只是整数.字典的索引被称为“键”,键及其关联的值 称为“键-值”对. 二.列表创建方式 1. I ...
- DS-二叉排序树的插入、查找和删除
2019-12-02(菜鸡开始学习了...) Data Structure 之 二叉排序树 二叉排序树是给定一个节点后,接下来插入的数如果比它大就会放到它的右孩子那边,比它小就会放到它的左孩子那边. ...
- 记录将CentOS7linux系统python升级到3后yum安装是报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid syntax问题解决
前两天将CentOS7里的python版本升级到了3.5.2 今天想装个解压软件,在执行yum install -y XXX 的时候报错 然后按照提示打开这个文件 vi /usr/bin/yum yu ...
- ngnix高并发的原理实现(转)
英文原文:Inside NGINX: How We Designed for Performance & Scale 为了更好地理解设计,你需要了解NGINX是如何工作的.NGINX之所以能在 ...
- HDU 3228 题解(最小生成树)(Kruskal)(内有详细注释)
Problem Description A group of explorers has found a solitary island. They land on the island and ex ...