CF 1009 F Dominant Indices —— 长链剖分+指针
题目:http://codeforces.com/contest/1009/problem/F
也可以用 dsu on tree 的做法,全局记录一个 dep,然后放进堆里,因为字典序要最小,所以再记一个第二关键字 dep[u];
长链剖分是 O(n) 的,因为如果 O(1) 继承重儿子(长儿子),对其他儿子枚举长度,那么每个点只会在向上第一次合并到重儿子时被枚举一次,所以总体 O(n);
然而不能开 f[1e6][1e6] 的数组,考虑到因为自己继承重儿子,所以数组有很大一部分是共用的,如果真的共用,数组总长度就是长链长度的和(每条长链只在顶端被开出来),也就是 n;
所以用指针,f[x] 指向数组 tmp 中的一个位置,对应 f[x][0] 。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const xn=1e6+;
int n,hd[xn],ct,to[xn<<],nxt[xn<<],dep[xn],d[xn],son[xn],ans[xn];
int tmp[xn],*f[xn],*id=tmp;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return f?ret:-ret;
}
void add(int x,int y){to[++ct]=y; nxt[ct]=hd[x]; hd[x]=ct;}
void dfs(int x,int fa)
{
dep[x]=d[x]=dep[fa]+;//d[x]=dep[x]
for(int i=hd[x],u;i;i=nxt[i])
{
if((u=to[i])==fa)continue;
dfs(u,x);
if(d[u]>d[x])son[x]=u,d[x]=d[u];
}
}
void dfsx(int x,int fa)
{
f[x][]=;
if(son[x])f[son[x]]=f[x]+,dfsx(son[x],x),ans[x]=ans[son[x]]+;
for(int i=hd[x],u;i;i=nxt[i])
{
if((u=to[i])==fa||u==son[x])continue;
f[u]=id; id+=d[u]-dep[u]+; dfsx(u,x);
for(int j=;j<=d[u]-dep[u];j++)
{
f[x][j+]+=f[u][j];
if(f[x][j+]>f[x][ans[x]]||(f[x][j+]==f[x][ans[x]]&&j+<ans[x]))
ans[x]=j+;//f[x][ans[x]]!!
}
}
if(f[x][ans[x]]==)ans[x]=;//cnt=1
}
int main()
{
n=rd();
for(int i=,x,y;i<n;i++)x=rd(),y=rd(),add(x,y),add(y,x);
dfs(,); f[]=id; id+=d[]; dfsx(,);//f[1]
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
return ;
}
CF 1009 F Dominant Indices —— 长链剖分+指针的更多相关文章
- Codeforces 1009 F. Dominant Indices(长链剖分/树上启发式合并)
F. Dominant Indices 题意: 给一颗无向树,根为1.对于每个节点,求其子树中,哪个距离下的节点数量最多.数量相同时,取较小的那个距离. 题目: 这类题一般的做法是树上的启发式合并,复 ...
- CF1009F Dominant Indices 长链剖分
题目传送门 https://codeforces.com/contest/1009/problem/F 题解 长链剖分的板子吧. 令 \(dp[x][i]\) 表示 \(x\) 的子树中的深度为 \( ...
- CF1009F Dominant Indices——长链剖分优化DP
原题链接 \(EDU\)出一道长链剖分优化\(dp\)裸题? 简化版题意 问你每个点的子树中与它距离为多少的点的数量最多,如果有多解,最小化距离 思路 方法1. 用\(dsu\ on\ tree\)做 ...
- Codeforces 1009 F - Dominant Indices
F - Dominant Indices 思路:树上启发式合并 先跑轻子树,然后清除轻子树的信息 最后跑重子树,不清除信息 然后再跑一遍轻子树,重新加回轻子树的信息 由于一个节点到根节点最多有logn ...
- CF 150E Freezing with Style [长链剖分,线段树]
\(sol:\) 给一种大常数 \(n \log^2 n\) 的做法 考虑二分,由于是中位数,我们就二分这个中位数,\(x>=mid\)则设为 \(1\),否则为 \(-1\) 所以我们只需要找 ...
- 【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\) 首先 ...
- dsu on tree 与长链剖分
dsu on tree 对于树进行轻重链剖分,对于节点 $x$ ,递归所有轻儿子后消除其影响,递归重儿子,不消除其影响. 然后对于所有轻儿子的子树暴力,从而得到 $x$ 的答案. 对于要消除暴力消除即 ...
- 【Cf Edu #47 F】Dominant Indices(长链剖分)
要求每个点子树中节点最多的层数,一个通常的思路是树上启发式合并,对于每一个点,保留它的重儿子的贡献,暴力扫轻儿子将他们的贡献合并到重儿子里来. 参考重链剖分,由于一个点向上最多只有$log$条轻边,故 ...
随机推荐
- Flex4_Tree组件2(添加多选框、修改树图标)
1.新建AS类,用于为Tree生成复选框,及一些选择事件. package com.th.myUtils { import flash.events.Event; import flash.event ...
- make mrproper及mrproper的含义
Linux下面去编译项目之前,一般常会用make mrproper去先删除之前编译所生成的文件和配置文件,备份文件等,其中,mrproper和distclean,clean之间的区别,Linux内核源 ...
- Leetcode_num2_Maximum Depth of Binary Tree
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- align="absmiddle" 图片的中间上下对齐
align=absmiddle表示图像的中间与同一行中最大元素的中间对齐 AbsBottom 图像的下边缘与同一行中最大元素的下边缘对齐.AbsMiddle 图像的中间与同一行中最大元素的中间对齐.B ...
- 计算IMEI号的校验位
计算IMEI号的校验位 移动设备国际识别码(IMEI:International Mobile Equipment Identification Number)是差别移动设备的标志,具有唯一性,贴在手 ...
- Sum It Up POJ 1564 HDU 杭电1258【DFS】
Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...
- # Playables API(翻译)
The Playables API provides a way to create tools, effects or other gameplay mechanisms by organi ...
- 【BZOJ1419】Red is good 期望
[BZOJ1419]Red is good Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻牌,在 ...
- 基于XML配置的Sping AOP详解
一.编写基本处理方法 package com.kang.sping.xml.aop; public class Math{ //加 public int add(int n1,int n2){ int ...
- flume topology design . tier num 分层数目
32:+:1 x:1 x<=8 https://flume.apache.org/FlumeUserGuide.html#flume-topology-design Flume topology ...