「CF1039D」You Are Given a Tree
传送门
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的更多相关文章
- LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree
2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- 「BZOJ2654」tree
「BZOJ2654」tree 最小生成树+二分答案. 最开始并没有觉得可以二分答案,因为答案并不单调啊. 其实根据题意,白边的数目肯定大于need条,而最小生成树的白边数并不等于need(废话),可以 ...
- 【题解】#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT)
[题解]#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT) 之前做这道题不理解,有一点走火入魔了,甚至想要一本近世代数来看,然后通过人类智慧思考后发现, ...
- 「SPOJ10707」Count on a tree II
「SPOJ10707」Count on a tree II 传送门 树上莫队板子题. 锻炼基础,没什么好说的. 参考代码: #include <algorithm> #include &l ...
- 「SPOJ1487」Query on a tree III
「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...
- 「luogu2633」Count on a tree
「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...
- 「AGC035C」 Skolem XOR Tree
「AGC035C」 Skolem XOR Tree 感觉有那么一点点上道了? 首先对于一个 \(n\),若 \(n\equiv 3 \pmod 4\),我们很快能够构造出一个合法解如 \(n,n-1, ...
- 「AGC010F」 Tree Game
「AGC010F」 Tree Game 传送门 切了一个 AGC 的题,很有精神. 于是决定纪念一下. 首先如果任意一个人在点 \(u\),他肯定不会向点权大于等于 \(a_u\) 的点走的,因为此时 ...
- 「数据结构」Link-Cut Tree(LCT)
#1.0 简述 #1.1 动态树问题 维护一个森林,支持删除某条边,加入某条边,并保证加边.删边之后仍然是森林.我们需要维护这个森林的一些信息. 一般的操作有两点连通性,两点路径权值和等等. #1.2 ...
随机推荐
- 使用类进行面向对象编程 Class 实例化 和 ES5实例化 对比,继承
ES5 写法 function Book(title, pages, isbn) { this.title = title; this.pages = pages; this.isbn = isbn; ...
- inline-block,真的懂吗
曾几何时,display:inline-block 已经深入「大街小巷」,随处可见 「display:inline-block; *display:inline; *zoom:1; 」这样的代码.如今 ...
- ➡️➡️➡️IELTS speaking by simon
目录 p1 课程概述 p2 speaking part1, intro, warm up introduction questions then 4 questions about one topic ...
- C语言-数组与指针 字符与字符串
1 字符与字符串:char c='a'而不能写出char c="a" //字符变量用单引号'',而字符串用双引号. 2 字符数组与字符指针的初始化: char s[10]={0}, ...
- Session服务器之Memcached
材料:两台Tomcat(接Session复制一起做) 第一台Tomcat:IP为130 [root@localhost ~]# yum install libevent memcached -y ...
- ubuntu开启mysql远程连接,并开启3306端口
mysql -u root -p 修改mysql库的user表,将host项,从localhost改为%.%这里表示的是允许任意host访问,如果只允许某一个ip访问,则可改为相应的ip mysql& ...
- 开发中,GA、Beta、GA、Trial到底是什么含义
前言 用过maven的都应该知道,创建maven项目时,其版本号默认会以SNAPSHOT结尾,如下: 通过英文很容易就可以知道这是一个快照版本.但是,在开发中,或者使用别的软件的时候,我们常常会见到各 ...
- UIAutomation 测试winForm
static void Main(string[] args) { Console.WriteLine("\n开始窗口程序自动化测试\n"); //启动被测试程序 string p ...
- 用Struts2框架报错:The Struts dispatcher cannot be found
报错信息 The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the ...
- JavaWeb之过滤器
过滤器 什么是过滤器 1示意图: 过滤器的作用: 1.过滤器的作用好比一个保安.是servlet规范中的技术 2.用户在访问应用的资源之前或者之后,可以对请求做出一定的处理 编写过滤器步骤: 1.编写 ...