题目链接

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

长链剖分。

\(O(1)\)继承重儿子的信息,再暴力合并其他轻儿子的信息,时间复杂度是线性的。

继承重儿子用指针实现,非常巧妙。

#include <cstdio>
int xjc; char ch;
inline int read(){
xjc = 0; ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9'){ xjc = xjc * 10 + ch - '0'; ch = getchar(); }
return xjc;
}
const int MAXN = 1000010;
struct Edge{
int next, to;
}e[MAXN << 1];
int head[MAXN], num, son[MAXN], len[MAXN], *f[MAXN], tmp[MAXN], *id = tmp, ans[MAXN], n;
inline void Add(int from, int to){
e[++num].to = to; e[num].next = head[from]; head[from] = num;
e[++num].to = from; e[num].next = head[to]; head[to] = num;
}
void dfs(int u, int fa){
for(int i = head[u]; i; i = e[i].next)
if(e[i].to != fa){
dfs(e[i].to, u);
if(len[e[i].to] > len[son[u]])
son[u] = e[i].to;
}
len[u] = len[son[u]] + 1;
}
void dp(int u, int fa){
f[u][0] = 1;
if(son[u]) f[son[u]] = f[u] + 1, dp(son[u], u), ans[u] = ans[son[u]] + 1;
for(int i = head[u]; i; i = e[i].next)
if(e[i].to != fa && e[i].to != son[u]){
f[e[i].to] = id; id += len[e[i].to]; dp(e[i].to, u);
for(int j = 1; j <= len[e[i].to]; ++j){
f[u][j] += f[e[i].to][j - 1];
if(f[u][j] > f[u][ans[u]] || f[u][j] == f[u][ans[u]] && j < ans[u])
ans[u] = j;
}
}
if(f[u][ans[u]] == 1) ans[u] = 0;
}
int main(){
n = read();
for(int i = 1; i < n; ++i)
Add(read(), read());
dfs(1, 0); f[1] = id; id += len[1];
dp(1, 0);
for(int i = 1; i <= n; ++i)
printf("%d\n", ans[i]);
getchar();
}

【CF1009F】 Dominant Indices (长链剖分+DP)的更多相关文章

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

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

  2. CF1009F Dominant Indices 长链剖分

    题目传送门 https://codeforces.com/contest/1009/problem/F 题解 长链剖分的板子吧. 令 \(dp[x][i]\) 表示 \(x\) 的子树中的深度为 \( ...

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

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

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

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

  5. 2019.01.08 bzoj4543: [POI2014]Hotel加强版(长链剖分+dp)

    传送门 代码: 长链剖分好题. 题意:给你一棵树,问树上选三个互不相同的节点,使得这个三个点两两之间距离相等的方案数. 思路: 先考虑dpdpdp. fi,jf_{i,j}fi,j​表示iii子树中离 ...

  6. 【BZOJ4543】[POI2014]Hotel加强版 长链剖分+DP

    [BZOJ4543][POI2014]Hotel加强版 Description 同OJ3522数据范围:n<=100000 Sample Input 7 1 2 5 7 2 5 2 3 5 6 ...

  7. P7581-「RdOI R2」路径权值【长链剖分,dp】

    正题 题目链接:https://www.luogu.com.cn/problem/P7581 题目大意 给出\(n\)个点的有边权有根树,\(m\)次询问一个节点\(x\)的所有\(k\)级儿子两两之 ...

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

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

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

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

随机推荐

  1. LintCode-372.在O(1)时间复杂度删除链表节点

    在O(1)时间复杂度删除链表节点 给定一个单链表中的一个等待被删除的节点(非表头或表尾).请在在O(1)时间复杂度删除该链表节点. 样例 给定 1->2->3->4,和节点 3,删除 ...

  2. PHPCMS登录后不是进入会员中心而是转入登录前页最新代码

    phpcms比如会员在登录前是停留在下载页面的,但是下载页面是要求会员登录后才能下载,所以会员就有这个登陆过程,但是一般的会员系统是登录进会员中心的,就会有点体验不好  这里教大家修改下 能达到登录后 ...

  3. React Components Template

    React Components Template "use strict"; /** * * @author xgqfrms * @license MIT * @copyrigh ...

  4. WPF DataGrid的使用

    构造数据: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...

  5. CF995C Leaving the Bar

    题目描述 For a vector v⃗=(x,y) \vec{v} = (x, y) v=(x,y) , define ∣v∣=x2+y2 |v| = \sqrt{x^2 + y^2} ∣v∣=x2 ...

  6. 【原创】Oracle Not In 导致有存在Null的数据被过滤

    解决方法:  WHERE  NVL(ID,)  NOT IN ('') 注:红字部分不相等就可以

  7. BZOJ1604 & 洛谷2906:[USACO2008 OPEN]Cow Neighborhoods 奶牛的邻居——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 https://www.luogu.org/problemnew/show/P2906#sub ...

  8. CF916E Jamie and Tree 解题报告

    CF916E Jamie and Tree 题意翻译 有一棵\(n\)个节点的有根树,标号为\(1-n\),你需要维护一下三种操作 1.给定一个点\(v\),将整颗树的根变为\(v\) 2.给定两个点 ...

  9. IDEA中使用Docker: 图形化 or 命令行 ,你更稀罕那个??

    Docker简介: Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化. 容器是完全使用沙箱机 ...

  10. bzoj1778: [Usaco2010 Hol]Dotp 驱逐猪猡(概率DP+高斯消元)

    深夜肝题...有害身心健康QAQ 设f[i]为到达i的概率,d[i]为i的度数. 因为无限久之后炸弹爆炸的概率是1,所以最后在i点爆炸的概率实际上就是f[i]/sigma(f[]) 列出方程组 f[i ...