『You Are Given a Tree 整体分治 树形dp』
<更新提示>
<第一次更新>
<正文>
You Are Given a Tree
Description
A tree is an undirected graph with exactly one simple path between each pair of vertices. We call a set of simple paths k -valid if each vertex of the tree belongs to no more than one of these paths (including endpoints) and each path consists of exactly k vertices.
You are given a tree with nn vertices. For each k from 1 to nn inclusive find what is the maximum possible size of a k -valid set of simple paths.
Input Format
The first line of the input contains a single integer n ( 2≤n≤100000 ) — the number of vertices in the tree.
Then following n - 1 lines describe the tree, each of them contains two integers v , u ( 1≤v,u≤n ) — endpoints of the corresponding edge.
It is guaranteed, that the given graph is a tree.
Output Format
Output n numbers, the i -th of which is the maximum possible number of paths in an i -valid set of paths.
Sample Input
7
1 2
2 3
3 4
4 5
5 6
6 7
Sample Output
7
3
2
1
1
1
1
解析
可以先考虑\(k\)确定时的做法,不妨进行树形\(dp\)。\(f[x]\)代表以\(x\)为根的子树中最长链的长度,同时维护一下全局答案。转移方式就是能合并就合并,反之选一条最长的链向上延伸,时间复杂度\(O(n)\)。
我们发现\(k\le\sqrt n\)时答案最多只有\(\sqrt n\)种取值,\(k>\sqrt n\)时答案\(\leq\sqrt n\),也只有\(\sqrt n\)种取值,并且答案的大小具有单调性,于是就有一个很直观的想法,二分找到段边界,统一每一段的答案即可,时间复杂度\(O(n\sqrt n\log_2n)\)。
但是直接这样写常数可能比较大,换一种整体二分的写法常数更小一些,时间复杂度不变,就可以通过本题了。
\(Code:\)
#include <bits/stdc++.h>
using namespace std;
const int N = 100020;
struct edge { int ver,next; } e[N*2];
int n,t,Head[N],f[N],cnt,ans[N];
inline void insert(int x,int y) { e[++t] = (edge){y,Head[x]} , Head[x] = t; }
inline void input(void)
{
scanf("%d",&n);
for (int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
insert( x , y );
insert( y , x );
}
}
inline void dp(int x,int fa,int len)
{
int Max = 0 , sec = 0;
for (int i=Head[x];i;i=e[i].next)
{
int y = e[i].ver;
if ( y == fa ) continue;
dp( y , x , len );
if ( f[y] >= Max ) sec = Max , Max = f[y];
else if ( f[y] > sec ) sec = f[y];
}
if ( sec + Max + 1 >= len ) f[x] = 0 , cnt++;
else f[x] = Max + 1;
}
inline void divide(int st,int ed,int l,int r)
{
if ( st > ed || l > r ) return;
if ( l == r )
{
for (int i=st;i<=ed;i++) ans[i] = l;
return;
}
int mid = st + ed >> 1; cnt = 0;
dp( 1 , 0 , mid );
ans[mid] = cnt;
divide( st , mid-1 , cnt , r );
divide( mid+1 , ed , l , cnt );
}
int main(void)
{
input();
divide( 1 , n , 0 , n );
for (int i=1;i<=n;i++)
printf("%d\n",ans[i]);
return 0;
}
<后记>
『You Are Given a Tree 整体分治 树形dp』的更多相关文章
- [codeforces161D]Distance in Tree(点分治/树形dp)
题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- Codeforces 791D Bear and Tree Jump(树形DP)
题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离, ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- E. Alternating Tree 树点分治|树形DP
题意:给你一颗树,然后这颗树有n*n条路径,a->b和b->a算是一条,然后路径的权值是 vi*(-1)^(i+1) 注意是点有权值. 从上头往下考虑是点分治,从下向上考虑就是树形DP, ...
- hdu5293 Tree chain problem 树形dp+线段树
题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...
- [BZOJ2152]聪聪可可 点分治/树形dp
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MB Submit: 3602 Solved: 1858 [Submit][Status][Discu ...
- 【2019.8.20 NOIP模拟赛 T2】小B的树(tree)(树形DP)
树形\(DP\) 考虑设\(f_{i,j,k}\)表示在\(i\)的子树内,从\(i\)向下的最长链长度为\(j\),\(i\)子树内直径长度为\(k\)的概率. 然后我们就能发现这个东西直接转移是几 ...
- BZOJ 2152 / Luogu P2634 [国家集训队]聪聪可可 (点分治/树形DP)
题意 一棵树,给定边权,求满足两点之间的路径上权值和为3的倍数的点对数量. 分析 点分治板题,对每个重心求子树下面的到根的距离模3分别为0,1,2的点的个数就行了. O(3nlogn)O(3nlogn ...
随机推荐
- chrome安装react-devtools开发工具
我开始安装react-devtools的时候 百度了一波,都是写的不清不楚,官网又都是英文的 也不是完全理解,经过一番折腾出来以后,写个文档记录一下,也可避免新手首次安装走弯路 我安装react-de ...
- APS系统生产流转方式和批量算法研究
01.前言 在经济领域,生产型企业是经济的根基,有了生产型企业生产出的各种产品,才有物流.网上购物和金融融资等活动.对于生产型企业,其制造能力是其核心竞争力.如何提升制造能力一直是生产型企业面临的课题 ...
- 8种常见数据结构及其Javascript实现
摘要: 面试常问的知识点啊... 原文:常见数据结构和Javascript实现总结 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 做前端的同学不少都是自学成才或者半路出家, ...
- 13 个 NPM 快速开发技巧
摘要: 玩转npm. 作者:前端小智 原文:13 个 npm 快速开发技巧 Fundebug经授权转载,版权归原作者所有. 为了保证的可读性,本文采用意译而非直译. 每天,数以百万计的开发人员使用 n ...
- odoo10学习笔记七:国际化、报表
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189297.html 一:国际化(模块翻译) 我们开发的模块要国际化通用,就要开发出不同的语言支持. 这些 ...
- 01-MySQL 大纲介绍
MySQL 大纲介绍 1.官方定义的MySQL DBA工作内容 (1)运维DBA 初级:各版本.各平台安装搭建.升级 中级:体系结构原理.基础管理(启动关闭.初始化配置文件管理.多实例管理.用户权限管 ...
- Rust中的闭包
这个功能有点高级, 暂时理解不完全, 先把代码练正确吧. use std::thread; use std::time::Duration; struct Cacher<T> where ...
- 201871010107-公海瑜《面向对象程序设计(java)》第十六周学习总结
201871010107-公海瑜<面向对象程序设计(java)>第十六周学习总结 项目 内容 这个作业属于哪 ...
- 201871010118-唐敬博 《面向对象程序设计(java)》第十五周学习总结
博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 <https://www.cnblogs.com/nwnu-daizh/> 这个作业的要求在哪里 <https://ww ...
- 283.移动零 关于列表list与remove原理*****(简单)
题目: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 注意,该题目要求不开辟行的数组空间,在原数据上进行操作. 示例: 输入: [0,1,0,3,12 ...