解题:CF1009 Dominant Indices
长链剖分模板题
只能按深度统计,同时比DSU on tree难理解一些,但是复杂度少个log
对每个点抓出向下延伸最长的儿子叫做长儿子。在合并时用指针继承信息,对于长儿子O(1)继承,其他儿子暴力
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int len[N],imp[N],ans[N];
int p[N],noww[*N],goal[*N];
int mem[N],*pts[N],*fpt=mem+;
int n,t1,t2,cnt;
void Link(int f,int t)
{
noww[++cnt]=p[f];
goal[cnt]=t,p[f]=cnt;
}
void DFS(int nde,int fth,int dth)
{
for(int i=p[nde];i;i=noww[i])
if(goal[i]!=fth)
{
DFS(goal[i],nde,dth+);
if(len[goal[i]]>len[imp[nde]])
imp[nde]=goal[i];
}
len[nde]=len[imp[nde]]+;
}
void Getans(int nde,int fth)
{
pts[nde][]=;
if(imp[nde])
{
pts[imp[nde]]=pts[nde]+;
Getans(imp[nde],nde),ans[nde]=ans[imp[nde]]+;
}
for(int i=p[nde];i;i=noww[i])
if(goal[i]!=fth&&goal[i]!=imp[nde])
{
pts[goal[i]]=fpt,fpt+=len[goal[i]],Getans(goal[i],nde);
for(int j=;j<=len[goal[i]];j++)
{
pts[nde][j]+=pts[goal[i]][j-];
int b1=(j<=ans[nde]&&pts[nde][j]>=pts[nde][ans[nde]]);
int b2=(j>ans[nde]&&pts[nde][j]>pts[nde][ans[nde]]);
if(b1||b2) ans[nde]=j;
}
}
if(pts[nde][ans[nde]]==) ans[nde]=;
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&t1,&t2);
Link(t1,t2),Link(t2,t1);
}
DFS(,,),pts[]=fpt,fpt+=len[];
Getans(,);
for(int i=;i<=n;i++)
printf("%d\n",ans[i]);
return ;
}
解题:CF1009 Dominant Indices的更多相关文章
- CF1009F Dominant Indices 解题报告
CF1009F Dominant Indices 题意简述 给出一颗以\(1\)为跟的有根树,定义\(d_{i,j}\)为以\(i\)为根节点的子树中到\(i\)的距离恰好为\(j\)的点的个数,对每 ...
- 【CF1009F】Dominant Indices(长链剖分)
[CF1009F]Dominant Indices(长链剖分) 题面 洛谷 CF 翻译: 给定一棵\(n\)个点,以\(1\)号点为根的有根树. 对于每个点,回答在它子树中, 假设距离它为\(d\)的 ...
- Codeforces 1009 F - Dominant Indices
F - Dominant Indices 思路:树上启发式合并 先跑轻子树,然后清除轻子树的信息 最后跑重子树,不清除信息 然后再跑一遍轻子树,重新加回轻子树的信息 由于一个节点到根节点最多有logn ...
- Codeforces 1009 F. Dominant Indices(长链剖分/树上启发式合并)
F. Dominant Indices 题意: 给一颗无向树,根为1.对于每个节点,求其子树中,哪个距离下的节点数量最多.数量相同时,取较小的那个距离. 题目: 这类题一般的做法是树上的启发式合并,复 ...
- [CF1009F] Dominant Indices (+dsu on tree详解)
这道题用到了dsu(Disjoint Set Union) on tree,树上启发式合并. 先看了CF的官方英文题解,又看了看zwz大佬的题解,差不多理解了dsu on tree的算法. 但是时间复 ...
- CF1009F Dominant Indices(树上DSU/长链剖分)
题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...
- CF1009F Dominant Indices(启发式合并)
You are given a rooted undirected tree consisting of nn vertices. Vertex 11 is the root. Let's denot ...
- Codeforces1009F Dominant Indices
dsu on tree 题目链接 点我跳转 题目大意 给定一棵以 \(1\) 为根,\(n\) 个节点的树.设\(d(u,x)\) 为 \(u\) 子树中到 \(u\) 距离为 \(x\) 的节点数. ...
- Codeforces ECR47F Dominant Indices(线段树合并)
一个比较显然的做法:对每棵子树用线段树维护其中的深度,线段树合并即可. 本来想用这个题学一下dsu on tree,结果还是弃疗了. #include<iostream> #include ...
随机推荐
- CocoStuff—基于Deeplab训练数据的标定工具【一、翻译】(未完)
一.CocoStuff简介 CocoStuff是一款为deeplab设计的,运行在Matlab中的语义标定工具,其标定结果和结合Deeplab训练出的结果均为mat文件格式,该项目源码已在github ...
- ofo容器pass架构分享
一.我们先要了解一下,为什么企业需要一个paas平台?或者可以说paas到底能做什么? 1.1 我们先来了解一下paas到底是什么? PaaS是Platform-as-a-Service的缩写,意思是 ...
- Linux系列——安装双系统Ubuntu
作为一个穷人,电脑破得不行却没钱换,怎么办呢,不如换个Ubuntu吧,没有Windows那么多后台应用,在我这台古董上稍微流畅一点. Linux有很多发行版,比较流行和适合入门的就是Ubuntu和De ...
- Python爬虫框架Scrapy学习笔记原创
字号 scrapy [TOC] 开始 scrapy安装 首先手动安装windows版本的Twisted https://www.lfd.uci.edu/~gohlke/pythonlibs/#twi ...
- 团队博客作业week1——成员介绍
我们小组的成员由六人组成,其中包括一名七班的韩国同学. 1.玉钟焕同学 玉钟焕是七班的同学.由于老师为了让我们尽早体验与不熟悉的同学共同工作的环境而提出团队需要跨行政班.于是我们便邀请钟焕同学加入我们 ...
- Task 6.4 冲刺Two之站立会议10
今天是最后一次站立会议,对我们的软件发布进行了讨论,看如何发布软件.我主要负责编写发布时需要提供的文和资料.
- hihocoder #1388 : Periodic Signal fft
题目链接: https://hihocoder.com/problemset/problem/1388 Periodic Signal 时间限制:5000ms内存限制:256MB 问题描述 Profe ...
- MaxAlertView 强大的弹框试图
[链接]https://github.com/MrJalen/MaxAlertView MaxAlertView ) { [MaxAlertView showAlertWithTitle:@" ...
- HDU 2134 Cuts the cake
http://acm.hdu.edu.cn/showproblem.php?pid=2134 Problem Description Ice cream took a bronze medal in ...
- 增加kms计数
@echo offset skms=10.15.68.62for %%i in (. . . . . . . . . . . . . . . . . . . . . . . . . .) do cal ...