当前位置:网站首页>CF1481F AB Tree
CF1481F AB Tree
2022-06-21 19:59:00 【doctorZ_】
题目大意
给定一棵 n n n 个节点的树,根为 1 1 1 ,每个节点会分配到一个字符 a 或 b。
要求整棵树中字符 a 的数量为 x x x ,字符 b 的数量为 n − x n-x n−x 。
定义节点 v v v 上的字符串:
- 若 v v v 是根节点,则 v v v 的上的字符串为根节点分配到的字符。
- 否则, v v v 上的字符串为父节点上的字符串的末尾加上 v v v 分配到的字符。
请为每个节点分配字符,在满足字符 a ,b 数量要求的前提下,使得所有节点上的字符串的种类最少。
solution
神仙构造
设最大深度为 d d d,容易发现答案的下界肯定为 d d d,具体只要每一层都染成一个颜色就行了,我们再分析一下上界,根据手玩和猜测,可以发现上界应该就是 d + 1 d+1 d+1的,考虑答案是 d + 1 d+1 d+1时应该是个什么情况,首先肯定同层非叶结点颜色必须相同,并且只有一层的部分叶子结点颜色和非叶子颜色不同,由于非叶结点至少有一个儿子,所以一层的非叶结点数一定是 ≤ ⌊ m 2 ⌋ \le \lfloor\frac{m}{2}\rfloor ≤⌊2m⌋的( m m m表示是剩下的未染色结点数),也就是说一定有办法使得每层的非叶结点都相同,然后叶子结点就先考虑染成与非叶子相同的颜色,如果不够就染完再涂别的
不难发现这样做最多有一层的叶子颜色不同,所以答案上界就是 n + 1 n+1 n+1
剩下的就用背包来判断即可,不同层结点数种类最多只有 O ( n ) O(\sqrt n) O(n)种,直接二进制优化多重背包即可,时间复杂度 O ( n ) O(\sqrt n) O(n)
代码莫得
边栏推荐
- Synchronous Boost dc/dc converter fs3400 synchronous SOT23-6 small current 500mA boost IC
- OpenJudge NOI 1.13 45:十进制到八进制
- [microservices 7] in depth analysis of bestavailablerule source code of ribbon load balancing strategy
- ARP协议及ARP攻击
- Precautions for Jerry's external radio [chapter]
- 有哪些将英文文献翻译为中文的网站或软件?
- What is EGFP, green fluorescent protein
- Qx2308 high efficiency PFM Synchronous Boost dc/dc converter
- Delaying patient self-help guide | "I have 100 excuses for not delaying, but I am not willing to take a step"
- Go language self-study series | golang standard library encoding/xml
猜你喜欢

For in JS In function

matplotlib plt. Details of subplots()

Merge two ordered arrays

Fs9935 high efficiency constant current limiting WLED drive IC

7.目标检测

Tianqi lithium passed the hearing: net profit of RMB 3.3 billion in a single quarter; jiangweiping and his wife are worth more than RMB 50billion

ACM. HJ35 蛇形矩阵 ●

#16迭代器经典案例

杰理之做蓝牙发射的时候,回链没有声音解决方法【篇】

ACM. Hj35 serpentine matrix ●
随机推荐
ARP protocol and ARP attack
Delaying patient self-help guide | "I have 100 excuses for not delaying, but I am not willing to take a step"
Jerry's SD card reuse IIC precautions [chapter]
Fs9935 high efficiency constant current limiting WLED drive IC
30 groups of outdoor travel vlog record LUTS color matching preset moody travel LUTS
30组户外旅行游玩VLOG记录LUTs调色预设Moody Travel LUTs
12.信号基础
Unity dynamically reads and plays external music
2022年焊工(高级)考试题模拟考试题库模拟考试平台操作
How to use the free and easy-to-use reference management software Zotero? Can I support both Chinese and English
J - Count the string HDU - 3336 (KMP)
Cocoapods installation (after xcode8.0, the infinite card is in the setting up cocoapods master repo)
Jerry's near end tone change problem of opening four channel call [chapter]
Scientific research cartoon | you can learn EEG by looking at the picture. Would you like to try it?
Pinduoduo 618 mobile phone brand official flag sales increased by 124% year-on-year, and 4000+ high-priced mobile phones increased by 156% year-on-year
JS中的构造函数(重点)
C语言数组与指针练习题(原题+解析+原码)
Tx9116 Synchronous Boost IC
Do280openshift command and troubleshooting -- accessing resources and resource types
提升四大性能!D-Wave发布下一代量子退火机原型