<更新提示>

<第一次更新>


<正文>

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』的更多相关文章

  1. [codeforces161D]Distance in Tree(点分治/树形dp)

    题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...

  2. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  3. Codeforces 791D Bear and Tree Jump(树形DP)

    题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离, ...

  4. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  5. E. Alternating Tree 树点分治|树形DP

    题意:给你一颗树,然后这颗树有n*n条路径,a->b和b->a算是一条,然后路径的权值是 vi*(-1)^(i+1)  注意是点有权值. 从上头往下考虑是点分治,从下向上考虑就是树形DP, ...

  6. hdu5293 Tree chain problem 树形dp+线段树

    题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...

  7. [BZOJ2152]聪聪可可 点分治/树形dp

    2152: 聪聪可可 Time Limit: 3 Sec  Memory Limit: 259 MB Submit: 3602  Solved: 1858 [Submit][Status][Discu ...

  8. 【2019.8.20 NOIP模拟赛 T2】小B的树(tree)(树形DP)

    树形\(DP\) 考虑设\(f_{i,j,k}\)表示在\(i\)的子树内,从\(i\)向下的最长链长度为\(j\),\(i\)子树内直径长度为\(k\)的概率. 然后我们就能发现这个东西直接转移是几 ...

  9. BZOJ 2152 / Luogu P2634 [国家集训队]聪聪可可 (点分治/树形DP)

    题意 一棵树,给定边权,求满足两点之间的路径上权值和为3的倍数的点对数量. 分析 点分治板题,对每个重心求子树下面的到根的距离模3分别为0,1,2的点的个数就行了. O(3nlogn)O(3nlogn ...

随机推荐

  1. CTF必备技能丨Linux Pwn入门教程——利用漏洞获取libc

    Linux Pwn入门教程系列分享如约而至,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...

  2. 新手入门必看:VectorDraw 常见问题整理大全(一)

    VectorDraw Developer Framework(VDF)是一个用于应用程序可视化的图形引擎库.有了VDF提供的功能,您可以轻松地创建.编辑.管理.输出.输入和打印2D和3D图形文件.该库 ...

  3. MySQL Tools 之 mysql.server 脚本运用

    MySQL distributions on Unix and Unix-like system include a script named mysql.server, which starts t ...

  4. Rust中的Vector类型

    常用类型操作, 如python中的list,turple,dictory等, 更方常编程常用数据的处理. fn main() { let v = vec![, , , , ]; let third: ...

  5. CMS垃圾收集器深入详解

    上一次[https://www.cnblogs.com/webor2006/p/11048407.html]对安全点和安全区进行了理论化的了解,接下来继续对CMS进行其它理论的了解,还是纯理论!!坚持 ...

  6. P3731 [HAOI2017]新型城市化(tarjan+网络流)

    洛谷 题意: 给出两个最大团的补图,现在要求增加一条边,使得最大最大团个数增加至少\(1\). 思路: 我们求出团的补图,问题可以转换为:对于一个二分图,选择删掉一条边,能够增大其最大独立集的点集数. ...

  7. v8 编译 时长3小时

  8. E05 【餐厅】What kind of coffee or tea would you like?

    核心句型 What  kind  of  coffee or tea would you like? 你想喝什么咖啡或者茶? What  would  you like? 你喜欢什么?/你想要什么? ...

  9. 浅析struct device结构体

    device结构体:设备驱动模型中的基础结构体之一 struct device { /*设备所依附的父设备 大多数情况下,这样的设备是某种总线或主控制器 如果该成员变量的值为NULL,表示当前设备是一 ...

  10. github配置密钥

    我们在githob创建项目后,本地使用git 克隆代码 需要在githob配置密钥,才可以 步骤: 下载git,进行安装,安装好后.点击桌面,右键,选择>>git  bash 在弹出的黑框 ...