<更新提示>

<第一次更新>


<正文>

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. 腾讯WeTest亮相—腾讯全球数字生态大会现场

    2019年5月21-23日腾讯全球数字生态大会在云南昆明滇池国际会展中心顺利召开. 此次大会上万人到场参与,大会由主峰会.分论坛.数字生态专题展会以及腾讯数字生态人物颁奖盛典四大板块构成.作为腾讯战略 ...

  2. docker仓库资源的地址修改

    docker仓库资源的地址修改 1.新增或者修改/etc/docker目录下的daemon.json文件 样例文件如下 { "registry-mirrors": ["h ...

  3. 代码审计-strpos数组绕过

    <?php $flag = "flag"; if (isset ($_GET['ctf'])) { if (@ereg ("^[1-9]+$", $_GE ...

  4. Vi编辑网卡

    Vi /etc/sysconfig/network-scripts/ifcfg-ens33  1.光标定位到BOOTPROTO=后面 2.然后按x键进行删除 3.按i键打开编辑模式,输入BOOTPRO ...

  5. SpringBoot设置支持跨域请求

    跨域:现代浏览器出全的考虑,在http/https请求时必须遵守同源策略,否则即使跨域的http/https 请求,默认情况下是被禁止的,ip(域名)不同.或者端口不同.协议不同(比如http.htt ...

  6. VIJOS-P1421 更换轮胎

    JDOJ 1506: VIJOS-P1421 更换轮胎 https://neooj.com/oldoj/problem.php?id=1506 Description 经过数周的期待,比赛终于正式开始 ...

  7. 页面配置snmp设备有问题,有时候能收到测试团体名的信息,有时候收不到

    现在走的是使用fabric远程连接zabbix服务器,这其中也会耗时间,代码中写的2s不返回数据就提示检查snmp信息失败,不合理, 目前df的server跟show在同一台机器,可以在本地直接调用, ...

  8. ASP.NET开发实战——(四)ASP.NET MVC是如何运行的?它的生命周期是什么?

    前面的文章我们使用ASP.NET MVC创建了个博客应用,那么它是如何工作的呢?我们都知道ASP.NET的程序需要部署到IIS上才能够通过浏览器来访问,那么IIS与ASP.NET MVC程序之间又是如 ...

  9. Npcap环境配置(Winpcap后继者) pcap的一种

    Npcap是基于Winpcap和Libpcap的,Winpcap已多年无人维护,其官网也推荐Windows XP之后的用户转移到Npcap上.Npcap基于WINPCAP,Winpcap基于libpc ...

  10. loj 2135 「ZJOI2015」幻想乡战略游戏 - 动态点分治

    题目传送门 传送门 题目大意 给定一棵树,初始点权都为0,要求支持: 修改点权 询问带权重心 询问带权重心就在点分树上跑一下就行了.(枚举跳哪个子树更优) 剩下都是基础点分治. 学了一下11-dime ...