『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 ...
随机推荐
- gsoap生成webservice调用客户端接口
1.下载gsoap2.8 2.运行 wsdl2h.exe -o XXX.h XXX.wsdl wsdl文件可以是本地文件,也可以是服务器的wsdl,比如http://192.168.0.122:333 ...
- maven 学习---NetBeans IDE集成Maven
NetBeans6.7更新版本已经内置对Maven支持.如遇以前的版本,Maven插件在插件管理器中可用.我们正在使用NetBeans在这个例子中使用6.9. 在NetBeans一些特点如下 您可以从 ...
- iOS - 常用宏定义和PCH文件知识点整理
(一)PCH文件操作步骤演示: 第一步:图文所示: 第二步:图文所示: (二)常用宏定义整理: (1)常用Log日志宏(输出日志详细可定位某个类.某个函数.某一行) //=============== ...
- itextpdf5操作表格
下面是一些对表格排版的常用方法,是在制作pdf的时候通过查看ipa和一些博客积累下来的. 包括,表格的宽度,对齐方式,表的页眉页脚,前后间距,padding: 单元格对齐方式,线条设置,段落于单元格之 ...
- SQLServer常用快捷键汇总
菜单激活键盘快捷键 操作 SQL Server 2017 SQL Server 2008 R2 移到 SQL Server Management Studio 菜单栏 Alt Alt 激活工具组件的菜 ...
- 201871010101-陈来弟《面向对象程序设计(JAVA)》 第14周学习总结
实验十二 Swing图形界面组件(一) 实验时间 2019-11-29 第一部分:基础知识 Swing和MVC设计模式 (1)设计模式(Design pattern)是设计者一种流行的 思考设计问题 ...
- shiro 基本知识测试
shiro 基本知识测试 <!--shiro核心包--> <dependency> <groupId>org.apache.shiro</groupId> ...
- USACO Dueling GPS's
洛谷 P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 洛谷传送门 JDOJ 2424: USACO 2014 Open Silver 2.Dueling GPSs JDO ...
- Xamarin.Forms移动开发系列3:项目剖析
摘要 本文主要进行Xamarin.Forms应用程序剖析. 前言 本文介绍Xamarin.Forms应用程序剖析. 由于本系列重点研究对象为Xamarin.Forms,所以对Xamarin.Andro ...
- [LeetCode] 763. Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...