传送门

还是放个链接让泥萌去学一下把 orzYYB

题目中要求的\(f_{x,j}\),转移是\(f_{x,j}=\sum_{y=son_x} f_{y,j-1}\),所以这个东西可以用长链剖分优化,利用指针,每个点直接继承重儿子信息,轻儿子的话暴力合并,一边合并一边更新答案

#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register using namespace std;
const int N=1e6+10;
il int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
int to[N<<1],nt[N<<1],hd[N],dg[N],tot=1;
il void add(int x,int y)
{
++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot,++dg[x];
++tot,to[tot]=x,nt[tot]=hd[y],hd[y]=tot,++dg[y];
}
int n,m;
int ff[N],de[N],dpt[N],son[N];
int *f[N],rbq[N<<2],*uc=rbq+1,an[N];
void dfs1(int x)
{
dpt[x]=de[x];
for(int i=hd[x];i;i=nt[i])
{
int y=to[i];
if(y==ff[x]) continue;
ff[y]=x,de[y]=de[x]+1,dfs1(y),dpt[x]=max(dpt[x],dpt[y]);
if(dpt[son[x]]<dpt[y]) son[x]=y;
}
}
void dd(int x)
{
f[x][0]=1;
if(son[x]) f[son[x]]=f[x]+1,dd(son[x]),an[x]=f[son[x]][an[son[x]]]>1?an[son[x]]+1:0;
for(int i=hd[x];i;i=nt[i])
{
int y=to[i];
if(y==ff[x]||y==son[x]) continue;
f[y]=uc,uc+=(dpt[y]-de[x]+1);
dd(y);
for(int j=0;j<dpt[y]-de[x]+1;++j) f[x][j+1]+=f[y][j];
for(int j=0;j<dpt[y]-de[x]+1;++j)
if(f[x][j+1]>f[x][an[x]]||(f[x][j+1]==f[x][an[x]]&&j+1<an[x])) an[x]=j+1;
}
} int main()
{
n=rd();
for(int i=1;i<n;++i) add(rd(),rd());
de[1]=1,dfs1(1);
f[1]=uc,uc+=dpt[1]+1;
dd(1);
for(int i=1;i<=n;i++) printf("%d\n",an[i]);
return 0;
}

CF1009F Dominant Indices的更多相关文章

  1. CF1009F Dominant Indices 解题报告

    CF1009F Dominant Indices 题意简述 给出一颗以\(1\)为跟的有根树,定义\(d_{i,j}\)为以\(i\)为根节点的子树中到\(i\)的距离恰好为\(j\)的点的个数,对每 ...

  2. [CF1009F] Dominant Indices (+dsu on tree详解)

    这道题用到了dsu(Disjoint Set Union) on tree,树上启发式合并. 先看了CF的官方英文题解,又看了看zwz大佬的题解,差不多理解了dsu on tree的算法. 但是时间复 ...

  3. CF1009F Dominant Indices(启发式合并)

    You are given a rooted undirected tree consisting of nn vertices. Vertex 11 is the root. Let's denot ...

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

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

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

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

  6. CF1009F Dominant Indices 长链剖分

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

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

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

  8. Codeforces 1009 F - Dominant Indices

    F - Dominant Indices 思路:树上启发式合并 先跑轻子树,然后清除轻子树的信息 最后跑重子树,不清除信息 然后再跑一遍轻子树,重新加回轻子树的信息 由于一个节点到根节点最多有logn ...

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

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

随机推荐

  1. Java中一些jar包的作用

    axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周期管理的常用方法. jaxrpc.jar Ax ...

  2. python之文件读写和异常处理

    # 文件读取.写入和异常处理操作举例 # date:2017-07-17 file_name = "D:/file_demo.txt" with open(file_name, ' ...

  3. BZOJ1127 POI2008KUP(悬线法)

    首先显然地,如果某个格子的权值超过2k,其一定不在答案之中:如果在[k,2k]中,其自身就可以作为答案.那么现在我们只需要考虑所选权值都小于k的情况. 可以发现一个结论:若存在一个权值都小于k的矩阵其 ...

  4. LOJ #6435. 「PKUSC2018」星际穿越(倍增)

    题面 LOJ#6435. 「PKUSC2018」星际穿越 题解 参考了 这位大佬的博客 这道题好恶心啊qwq~~ 首先一定要认真阅读题目 !! 注意 \(l_i<r_i<x_i\) 这个条 ...

  5. 学习Spring Boot:(二十八)Spring Security 权限认证

    前言 主要实现 Spring Security 的安全认证,结合 RESTful API 的风格,使用无状态的环境. 主要实现是通过请求的 URL ,通过过滤器来做不同的授权策略操作,为该请求提供某个 ...

  6. source、sh、bash

    source.sh.bash../ 执行脚本的区别 1.source命令用法: source FileName 作用:在当前bash环境下读取并执行FileName中的命令.该filename文件可以 ...

  7. eclipse index 不工作 F3 不能找到头文件

    To add paths containing code to parse, follow these steps :1. Right click on the project2. Select Pr ...

  8. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)

    HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...

  9. C++11并发——多线程std::thread (一)

    https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...

  10. printf 中的 %.*s

    printf("message arrived %.*s\n", length, str); .*代表length 当 str 长度大于等于 length,打印出 str 前 le ...