【CF1042F】Leaf Sets
【CF1042F】Leaf Sets
题面
题解
对于一个根节点\(x\),考虑其子树内的所有\(lca\)为它的叶子节点到它的距离\(d_1<d2<...<d_m\)。
那么对于中间最小的\(d_i+d_{i+1}>K\),我们可以将\(i\)之前的所有叶子节点合并成一个大点,并以深度\(d_i\)向上合并,再将\(d_{i+1}...d_m\)向上合并即可。
这样子用数据结构维护复杂度是\(O(n\log n)\)的。
然而我们发现只有\(d_i\)继续向上合并才有用,那么只要保留这个\(d_i\)就行了,复杂度\(O(n)\)。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 1e6 + 5;
struct Graph { int to, next; } e[MAX_N << 1];
int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; }
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; }
int N, K, deg[MAX_N], ans;
int dfs(int x, int fa) {
if (deg[x] == 1) return 1;
int res = 0;
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa) continue;
int tmp = dfs(v, x);
if (tmp + res > K) ans++, res = min(res, tmp);
else res = max(res, tmp);
}
return res ? res + 1 : 0;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
clearGraph();
N = gi(), K = gi();
for (int i = 1; i < N; i++) {
int u = gi(), v = gi();
Add_Edge(u, v), Add_Edge(v, u);
deg[u]++, deg[v]++;
}
for (int i = 1; i <= N; i++)
if (deg[i] != 1) return printf("%d\n", (bool)(dfs(i, 0)) + ans) & 0;
return 0;
}
【CF1042F】Leaf Sets的更多相关文章
- 「CF1042F」Leaf Sets
传送门 Luogu 解题思路 比较显然的一种做法: 我们把一个点的子树高度抠出来并排序记为 \(L_i\),找到最大的 \(i\) 使得 \(L_{i-1}+L_i\le K\). 于是我们把前 \( ...
- 【转】Python数据类型之“集合(Sets)与映射(Mapping)”
[转]Python数据类型之“集合(Sets)与映射(Mapping)” 一.集合类型(Sets) 集合对象是不同的(不可重复)hashable对象的无序集合.常见用法包括:成员关系测试.移除序列中的 ...
- 【CF932F】Escape Through Leaf 启发式合并set维护凸包
[CF932F]Escape Through Leaf 题意:给你一棵n个点的树,每个点有树形ai和bi,如果x是y的祖先,则你可以从x花费$a_x\times b_y$的费用走到y(费用可以为负). ...
- 【53.57%】【codeforces 722D】Generating Sets
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【【henuacm2016级暑期训练】动态规划专题 N】Valid Sets
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 给你一棵树. 让你统计其中子树T的数量. 这个子树T要满足最大值和最小值之差小于等于d 树形DP 可以枚举点root为子树的根. 统 ...
- 【434】COMP9024 Exercises Revision
目录: Week01 Week02 Week03 Week04 Week05 Week06 Week07 Week08 Week09 Week10 01. Week01 数字通过 #define 来定 ...
- Python高手之路【三】python基础之函数
基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...
- 【目录】Leetcode
Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) ...
随机推荐
- 使用el-dialog时,报错“Unknown custom element:<el-dialog> did you register the component correctly?...make sure to provide the 'name' option”
初学vue时,曾遇到一个无语的问题,在用el-dialog时一直显示没有导入,结果发现是因为没有把element ui引入到项目里. 需进行以下步骤: 1.执行: npm install elemen ...
- texstudio基本设置
一开始默认为英文,在上面菜单栏,“option” 1.设置中文:options->general->language->zh-cn 2.编辑和查看按钮: 3.设置默认编译器:选项-& ...
- [Leetcode] 1120. Maximum Average Subtree
Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subt ...
- Winform串口编程---接收数据demo(VSPD虚拟串口)
参考地址:https://blog.csdn.net/memgxingfeixiang/article/details/52513970 https://blog.csdn.net/kevin_io ...
- jwt认证生成后的token如何传回后端并解析的详解
jwt认证生成后的token后端解析 一.首先前端发送token token所在的位置headers {'authorization':token的值',Content-Type':applicati ...
- OpenGL 中的三维纹理操作
#define _CRT_SECURE_NO_WARNINGS #include <gl/glut.h> #include <stdio.h> #include <std ...
- React学习笔记①
三种导出方式 export let num = 1://1 let num2 = 2://2 export {num2};//2 export default {default}//3 三种导入方式 ...
- Python实现的贪婪算法
个州的听众都收听到.为此,你需要决定在哪些广播台播出.在每个广播台播出都需要支出费用,因此你力图在尽可能少的广播台播出 # 1.创建一个列表,其中包含要覆盖的州 states_needed = set ...
- nginx 405错误
nginx配置文件加上location / { try_files $uri $uri/ /index.php?$query_string; }
- 【Python】生成器
生成器是一种特殊的迭代器 # 斐波那契数列 10 def create_num(all_num): a, b = 0, 1 current_num = 0 while current_num < ...