『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 ...
 
随机推荐
- UITableView与UITableViewCell
			
转自:http://blog.sina.com.cn/s/blog_4cd14afb01014j97.html UITableView用来以表格的形式显示数据.关于UITableView,我们应该注意 ...
 - Linux open fopen fdopen
			
int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); 以 ...
 - RHEL7.5 静默安装(silent mode)oracle11gr2数据库软件
			
如果没有图形界面多可怕,或者图形界面安装总报些奇怪的错误多可怕,静默安装数据库软件了解一下 修改主机名.关闭selinux [root@localhost ~]$ sed -i '3,$d' /etc ...
 - XGBoost 完整推导过程
			
参考: 陈天奇-"XGBoost: A Scalable Tree Boosting System" Paper地址: <https://arxiv.org/abs/1603 ...
 - 一次http请求的过程
			
http协议(超文本传输协议)是属于应用层的协议,网络分层:应用层(http协议,FTP),传输层(tcp,udp),网络层(ip/ARP),链路层 我们以浏览器向百度发送请求为例: http的发送: ...
 - 设计模式小议:state【转】
			
转自:https://blog.csdn.net/goodboy1881/article/details/635963 这个模式使得软件可以在不同的state下面呈现出完全不同的特征 不同的theme ...
 - Jenkins参数化构建(七)
			
一.配置参数化构建过程 主要用来区分分支,使用传参的方式,将分支名称传入脚本中进行拉取代码. 1.1 最常用的是:字符参数.文本参数. 1.2 添加字符参数和文本参数,并配置变量名称 1.3 配置 ...
 - Rust中的闭包
			
这个功能有点高级, 暂时理解不完全, 先把代码练正确吧. use std::thread; use std::time::Duration; struct Cacher<T> where ...
 - 06点击事件    tabBar配置     拨打电话
			
1== D:\wxxm 项目的地址 2==>tabBar在全局配置中 在pages的同级目录下创建images本地图标 (最好的是在远程获取img 因为微信是有大小限制的) selectedIc ...
 - 8-剑指offer: 替换空格
			
题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 代码: c ...