题目传送门

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 长链剖分的更多相关文章

  1. CF1009F Dominant Indices——长链剖分优化DP

    原题链接 \(EDU\)出一道长链剖分优化\(dp\)裸题? 简化版题意 问你每个点的子树中与它距离为多少的点的数量最多,如果有多解,最小化距离 思路 方法1. 用\(dsu\ on\ tree\)做 ...

  2. Codeforces 1009 F. Dominant Indices(长链剖分/树上启发式合并)

    F. Dominant Indices 题意: 给一颗无向树,根为1.对于每个节点,求其子树中,哪个距离下的节点数量最多.数量相同时,取较小的那个距离. 题目: 这类题一般的做法是树上的启发式合并,复 ...

  3. CF 1009 F Dominant Indices —— 长链剖分+指针

    题目:http://codeforces.com/contest/1009/problem/F 也可以用 dsu on tree 的做法,全局记录一个 dep,然后放进堆里,因为字典序要最小,所以再记 ...

  4. 【CF1009F】Dominant Indices(长链剖分)

    [CF1009F]Dominant Indices(长链剖分) 题面 洛谷 CF 翻译: 给定一棵\(n\)个点,以\(1\)号点为根的有根树. 对于每个点,回答在它子树中, 假设距离它为\(d\)的 ...

  5. 【CF1009F】 Dominant Indices (长链剖分+DP)

    题目链接 \(O(n^2)\)的\(DP\)很容易想,\(f[u][i]\)表示在\(u\)的子树中距离\(u\)为\(i\)的点的个数,则\(f[u][i]=\sum f[v][i-1]\) 长链剖 ...

  6. 【CF1009F】Dominant Indices(长链剖分优化DP)

    点此看题面 大致题意: 设\(d(x,y)\)表示\(x\)子树内到\(x\)距离为\(y\)的点的个数,对于每个\(x\),求满足\(d(x,y)\)最大的最小的\(y\). 暴力\(DP\) 首先 ...

  7. CF1009F Dominant Indices(树上DSU/长链剖分)

    题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...

  8. 2019.01.08 codeforces 1009F. Dominant Indices(长链剖分)

    传送门 长链剖分模板题. 题意:给出一棵树,设fi,jf_{i,j}fi,j​表示iii的子树中距离点iii距离为jjj的点的个数,现在对于每个点iii要求出使得fif_ifi​取得最大值的那个jjj ...

  9. 【Cf Edu #47 F】Dominant Indices(长链剖分)

    要求每个点子树中节点最多的层数,一个通常的思路是树上启发式合并,对于每一个点,保留它的重儿子的贡献,暴力扫轻儿子将他们的贡献合并到重儿子里来. 参考重链剖分,由于一个点向上最多只有$log$条轻边,故 ...

随机推荐

  1. android发送udp,tcp消息

    发送方创建步骤: 1.  创建一个DatagramSocket对象 DatagramSocket socket = new  DatagramSocket (4567); 2.  创建一个 InetA ...

  2. JAVA 8 :从永久区(PermGen)到元空间(Metaspace)

    你注意到了吗?JDK 8早期可访问版本已经提供下载了,java 开发人员可以使用java 8 提供的新的语言和运行特性来做一些实验.其中一个特性就是完全的移除永久代(Permanent Generat ...

  3. React-Native 之 GD (四)使用通知方式隐藏或显示TabBar

    1.GDHalfHourHot.js  发送通知 /** * 近半小时热门 */ import React, { Component } from 'react'; import { StyleShe ...

  4. str_shuffle函数

      str_shuffle() 函数打乱一个字符串,使用任何一种可能的排序方案.     <?php $str = 'hello world '; echo str_shuffle($str); ...

  5. iptables List the rules in a chain or all chains

    [root@e ~]# iptables -hiptables v1.4.21 Usage: iptables -[ACD] chain rule-specification [options] ip ...

  6. JS 引擎

    最早的 JS 引擎是纯解释器,现代 JS 引擎已经使用 JIT(Just-in-time compilation:结合预编译(ahead-of-time compilation AOT)和解释器的优点 ...

  7. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_3_绝对路径和相对路径

  8. 网络摄像头CVE

    CVE-2018-9995 rtsp未授权访问 rtsp后缀整理: Axis(安讯士) rtsp:// 192.168.200.202/axis-media/media.amp?videocodec= ...

  9. [Git] 013 远程仓库篇 第零话 使用前的一些配置

    0. 前言 本地仓库和 GitHub 上的远程仓库之间的传输是通过 "SSH" 加密的,所以使用前需要进行一些设置 这回的任务 设置"身份象征" 创建 &quo ...

  10. [Git] 008 status 与 commit 命令的补充

    本文的"剧情"承接 [Git] 007 三棵树以及向本地仓库加入第一个文件 1. 对 "status" 的补充 1.1 "status" 有 ...