传送门

Luogu

解题思路

整体二分。

的确是很难看出来,但是你可以发现输出的答案都是一些可以被看作是关键字处于 \([1, n]\) 的询问,而答案的范围又很显然是 \([0, n]\),这不就刚好满足了整体二分的几个组成部分了吗。  

那么我们要如何求出 \(mid\) 位置的解呢?  

考虑 \(\text{DP}\)

我们很显然可以将子树中的点尽可能合并后再向父亲传递,所以我们对每一次DP的根节点分别记一个子树中的最大值,和一个非严格次大值,然后我们尝试合并这两个值,要是合并不了,就给答案加一,不然就把最大值上传。

正确性和NOIP2018赛道修建有异曲同工之妙

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 100000 + 2; int tot, head[_], nxt[_ << 1], ver[_ << 1];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; } int n, ans[_], dp[_]; inline void dfs(int u, int f, int x) {
dp[u] = 0;
int mx = 0, mn = 0;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
dfs(v, u, x);
if (dp[v] > mx) mn = mx, mx = dp[v];
else mn = max(mn, dp[v]);
}
if (mx + mn + 1 >= x) dp[u] = 0, ++ans[x];
else dp[u] = mx + 1;
} inline void binary(int l, int r, int L, int R) {
if (l > r || L > R) return ;
if (L == R) {
for (rg int i = l; i <= r; ++i) ans[i] = L; return ;
}
int mid = (l + r) >> 1;
ans[mid] = 0, dfs(1, 0, mid);
binary(l, mid - 1, ans[mid], R);
binary(mid + 1, r, L, ans[mid]);
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int u, v, i = 1; i < n; ++i)
read(u), read(v), Add_edge(u, v), Add_edge(v, u);
binary(1, n, 0, n);
for (rg int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
return 0;
}

完结撒花 \(qwq\)

「CF1039D」You Are Given a Tree的更多相关文章

  1. LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree

    2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  2. 「BZOJ2654」tree

    「BZOJ2654」tree 最小生成树+二分答案. 最开始并没有觉得可以二分答案,因为答案并不单调啊. 其实根据题意,白边的数目肯定大于need条,而最小生成树的白边数并不等于need(废话),可以 ...

  3. 【题解】#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT)

    [题解]#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT) 之前做这道题不理解,有一点走火入魔了,甚至想要一本近世代数来看,然后通过人类智慧思考后发现, ...

  4. 「SPOJ10707」Count on a tree II

    「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...

  5. 「SPOJ1487」Query on a tree III

    「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...

  6. 「luogu2633」Count on a tree

    「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...

  7. 「AGC035C」 Skolem XOR Tree

    「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...

  8. 「AGC010F」 Tree Game

    「AGC010F」 Tree Game 传送门 切了一个 AGC 的题,很有精神. 于是决定纪念一下. 首先如果任意一个人在点 \(u\),他肯定不会向点权大于等于 \(a_u\) 的点走的,因为此时 ...

  9. 「数据结构」Link-Cut Tree(LCT)

    #1.0 简述 #1.1 动态树问题 维护一个森林,支持删除某条边,加入某条边,并保证加边.删边之后仍然是森林.我们需要维护这个森林的一些信息. 一般的操作有两点连通性,两点路径权值和等等. #1.2 ...

随机推荐

  1. Ubuntu18 mongodb 离线安装

    环境 Ubuntu 18 + mongodb 4.0.10 1.下载版本所需库 https://www.mongodb.com/download-center/community https://re ...

  2. 研究Zookeeper的原理(二)

    阅读声明:以下内容是结合网上材料及工作内容所写的个人理解,如有不当,欢迎大家指正~~~谢谢啦 一.ZooKeeper的选举机制.FailOver机制 我们知道ZooKeeper在分布式环境中协调服务, ...

  3. 标定设备自动化-ASAP3

    欢迎关注<汽车软件技术>公众号,回复关键字获取资料. 1.ASAP3定义 下图选自INCA文档<INCA_IF_ASAM-ASAP3_EN.pdf>说明了ASAP3的用途:标定 ...

  4. python requirements.txt批量下载安装离线

    有些情况下我们需要下载N个第三方包,或者下载的包依赖其它包,一个个下载非常浪费时间.这时我们可以通过如下两种方式的命令批量下载. 方式1 pip download -d /tmp/packagesdi ...

  5. 07-华为RAID2.0+技术

    目录 07-华为RAID2.0+技术 参考 RAID2.0+原理 07-华为RAID2.0+技术

  6. Lesson 16 The modern city

    What is the author's main argument about the modern city? In the organization of industrial life the ...

  7. flask exception

    flask exception 1.1.    abort 概念:flask中的异常处理语句,功能类似于python中raise语句,只要触发abort,后面的代码不会执行,abort只能抛出符合ht ...

  8. python-定时任务-apschelduer

    python-定时任务-apschelduer 1.      apscheduler 1.1.    install pip install apscheduler 1.2.    basic co ...

  9. Can you answer these queries?-HDU4027 区间开方

    题意: 给你n个数,两个操作,0为区间开方,1为区间求和 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 思路: 如果当该区间的数都为1,我们没必要 ...

  10. 【原】php中fastcgi和php-fpm是什么东西

    fastcgi 是一个与平台无关,与语言无关,任何语言只要按照它的接口来实现,就能实现自己语言的fastcgi能力和web server 通讯. PHP-CGI就是PHP实现的自带的FastCGI管理 ...