【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的更多相关文章

  1. 「CF1042F」Leaf Sets

    传送门 Luogu 解题思路 比较显然的一种做法: 我们把一个点的子树高度抠出来并排序记为 \(L_i\),找到最大的 \(i\) 使得 \(L_{i-1}+L_i\le K\). 于是我们把前 \( ...

  2. 【转】Python数据类型之“集合(Sets)与映射(Mapping)”

    [转]Python数据类型之“集合(Sets)与映射(Mapping)” 一.集合类型(Sets) 集合对象是不同的(不可重复)hashable对象的无序集合.常见用法包括:成员关系测试.移除序列中的 ...

  3. 【CF932F】Escape Through Leaf 启发式合并set维护凸包

    [CF932F]Escape Through Leaf 题意:给你一棵n个点的树,每个点有树形ai和bi,如果x是y的祖先,则你可以从x花费$a_x\times b_y$的费用走到y(费用可以为负). ...

  4. 【53.57%】【codeforces 722D】Generating Sets

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  6. 【【henuacm2016级暑期训练】动态规划专题 N】Valid Sets

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 给你一棵树. 让你统计其中子树T的数量. 这个子树T要满足最大值和最小值之差小于等于d 树形DP 可以枚举点root为子树的根. 统 ...

  7. 【434】COMP9024 Exercises Revision

    目录: Week01 Week02 Week03 Week04 Week05 Week06 Week07 Week08 Week09 Week10 01. Week01 数字通过 #define 来定 ...

  8. Python高手之路【三】python基础之函数

    基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...

  9. 【目录】Leetcode

    Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) ...

随机推荐

  1. CLR学习之初识CLR

    一.什么是CLR? CLR即公共语言运行时(Common Language Runtime,简称CRL),就是微软为.net产品构建的运行环境,与java的JVM类似,通俗的讲就是.net虚拟机.CL ...

  2. 类例程_java战斗程序

    代码如下: package t11; import java.util.Random; public class Fight { String name; int life, attack, spee ...

  3. Spring Boot 的自动配置探究、自制一个starter pom

    //TODO @Conditional @Condition

  4. Schnorr签名介绍

    Schnorr签名介绍 来源 https://panzhibiao.com/2019/02/28/schnorr-sigature/ https://github.com/bitcoin/bitcoi ...

  5. 2019-08-01 jquery中常用方法

    1.attr()方法设置或返回被选元素的属性值 <html> <meta charset="utf-8"/> <head><title&g ...

  6. APP开发基础知识(转载)

    来源:https://www.cnblogs.com/wangsea/p/9413672.html 本文针对小白用户对App做一个简单的介绍,首先要了解App都有哪些类型,不同的类型适用于哪些需求,用 ...

  7. win2003下安装python3.4 + pyspider

    昨天尝试了在win2003下安装python2.7.这个是文章地址:https://www.cnblogs.com/alpiny/p/11706606.html 但是程序跑了一晚上,发现有一点问题,是 ...

  8. FFMPEG 命令行工具-ffprobe

    ffprobe 简介 ffprobe 是一个多媒体流分析工具.它从多媒体流中收集信息,并且以人类和机器可读的形式打印出来. 它可以用来检测多媒体流的容器类型,以及每一个多媒体流的格式和类型.它可以作为 ...

  9. win10 + Ubuntu18.04 双系统,UEFI+GPT,从win10切换到Ubuntu时黑屏问题

    1.现象: ①win10主系统,从win10重启,立即黑屏,之后会进入Ubuntu(还是黑屏)(为什么会知道进入了Ubuntu:按音量键可以听到Ubuntu音量加减的系统声音,数字锁定和大小写锁定均有 ...

  10. Android研发技术的进阶之路

    前言 移动研发火热不停,越来越多人开始学习android开发.但很多人感觉入门容易成长很难,对未来比较迷茫,不知道自己技能该怎么提升,到达下一阶段需要补充哪些内容.市面上也多是谈论知识图谱,缺少体系和 ...