[atARC125F]Tree Degree Subset Sum
令$a_{i}$为$i$的度数-1,那么$(x,s)$合法即等价于存在$S\subseteq [1,n],|S|=x$且$\sum_{k\in S}a_{k}=s$
引理:$(x,s)$合法的必要条件为$-z\le s-x\le z-2$
令$z$为$a_{i}$中为0的元素个数,考虑任意一个集合$S\subseteq [1,n]$,显然$-z\le \sum_{k\in S}a_{k}-|S|\le z-2$
具体的,考虑该式即为$\sum_{k\in S}(a_{k}-1)$,那么将所有$a_{k}<1$的项求和即为最小值,将所有$a_{k}\ge 1$的项求和即为最大值,不难发现前者即为$-z$,后者即为$\sum_{i=1}^{n}a_{i}-(n-z)=z-2$
不难发现,其所描述的即为该引理,也即得证
结论:令$mn(s)=\min_{(x,s)合法}x$和$mx(s)=\max_{(x,s)合法}x$,则$\forall mn(s)\le x\le mx(s),(x,s)$合法
(为了方便,若不存在$(x,s)$合法则定义$mn(s)$和$mx(S)$为$\pm\infty$,显然此时满足结论)
同样令$z$为$a_{i}$中为0的元素个数,显然$mn(s)$对应的方案必然一个0都不选,那么在其基础上再选$[0,z]$个0,即有$\forall mn(s)\le x\le mn(s)+z,(x,s)$合法
类似地,也可以得到$\forall mx(s)-z\le x\le mx(s),(x,s)$合法
由引理即有$mx(s)-mn(s)\le 2z-2$,因此两者区间相交,也即得证
由此,问题即$\forall s$求$mn(s)$和$mx(s)$,这个问题可以dp解决
具体的,(以$mn(s)$为例)令$f_{s}$为对应的答案,则$f_{s}=\min(f_{s},f_{s-a_{i}}+1)$
进一步的,将其对每一种物品(指相同的$a_{i}$)一起处理,假设有$x$个$a_{i}=k$,枚举$s$上选择的$k$个数,即可得到转移为$f_{s}=\min_{0\le i\le x}(f_{s-ik}+i)$,不难用优先队列$o(1)$求出后者
注意到物品种类数至多为$\sqrt{n}$,因此总复杂度为$o(n\sqrt{n})$,可以通过

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 200005
4 #define ll long long
5 int n,x,y,l,r,a[N],tot[N],q[N],mx[N],mn[N],f[N];
6 ll ans;
7 int main(){
8 scanf("%d",&n);
9 memset(a,-1,sizeof(a));
10 for(int i=1;i<n;i++){
11 scanf("%d%d",&x,&y);
12 a[x]++,a[y]++;
13 }
14 for(int i=1;i<=n;i++)tot[a[i]]++;
15 memset(mn,0x3f,sizeof(mn));
16 memset(mx,-0x3f,sizeof(mx));
17 mn[0]=0,mx[0]=tot[0];
18 for(int i=1;i<=n;i++){
19 if (!tot[i])continue;
20 for(int j=0;j<i;j++){
21 l=1,r=0;
22 for(int k=j;k<=n;k+=i){
23 while ((l<=r)&&(mn[k]-k/i<=mn[q[r]]-q[r]/i))r--;
24 q[++r]=k;
25 while ((l<=r)&&(q[l]/i<k/i-tot[i]))l++;
26 f[k]=mn[q[l]]+(k-q[l])/i;
27 }
28 }
29 memcpy(mn,f,sizeof(f));
30 }
31 for(int i=1;i<=n;i++){
32 if (!tot[i])continue;
33 for(int j=0;j<i;j++){
34 l=1,r=0;
35 for(int k=j;k<=n;k+=i){
36 while ((l<=r)&&(mx[k]-k/i>=mx[q[r]]-q[r]/i))r--;
37 q[++r]=k;
38 while ((l<=r)&&(q[l]/i<k/i-tot[i]))l++;
39 f[k]=mx[q[l]]+(k-q[l])/i;
40 }
41 }
42 memcpy(mx,f,sizeof(f));
43 }
44 for(int i=0;i<=n;i++)ans+=max(mx[i]-mn[i]+1,0);
45 printf("%lld\n",ans);
46 return 0;
47 }
[atARC125F]Tree Degree Subset Sum的更多相关文章
- Solution -「ARC 125F」Tree Degree Subset Sum
\(\mathcal{Description}\) Link. 给定含有 \(n\) 个结点的树,求非负整数对 \((x,y)\) 的数量,满足存在 \(\exist S\subseteq V ...
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 26. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- Subset sum problem
https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 动态规划法(三)子集和问题(Subset sum problem)
继续讲故事~~ 上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界. 这天,丁丁刚回到家,他 ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
随机推荐
- PaddlePaddle:在 Serverless 架构上十几行代码实现 OCR 能力
飞桨 (PaddlePaddle) 以百度多年的深度学习技术研究和业务应用为基础,是中国首个自主研发.功能完备. 开源开放的产业级深度学习平台,集深度学习核心训练和推理框架.基础模型库.端到端开发 ...
- FastAPI 学习之路(九)请求体有多个参数如何处理?
系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...
- 电脑日常使用bug记录
1.由于电脑太卡了,于是决定关一点服务,一不小心,电脑无线无法使用了.启动无线服务时提示"windows无法启动wlan autoconfig服务错误1068依赖服务" 启动 Ex ...
- 【UE4 设计模式】单例模式 Singleton Pattern
概述 描述 保证一个类只有一个实例 提供一个访问该实例的全局节点,可以视为一个全局变量 仅在首次请求单例对象时对其进行初始化. 套路 将默认构造函数设为私有, 防止其他对象使用单例类的 new运算符. ...
- 92.反转链表II
题目 给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right .请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 . ...
- 改善深层神经网络-week1编程题(GradientChecking)
1. Gradient Checking 你被要求搭建一个Deep Learning model来检测欺诈,每当有人付款,你想知道是否该支付可能是欺诈,例如该用户的账户可能已经被黑客掉. 但是,反向传 ...
- [技术博客]OKhttp3使用get,post,delete,patch四种请求
OKhttp3使用get,post,delete,patch四种请求 1.okhttp简介 okhttp封装了大量http操作,大大简化了安卓网络请求操作,是现在最火的安卓端轻量级网络框架.如今okh ...
- spring cache整合redis
在项目中,我们经常需要将一些常用的数据使用缓存起来,避免频繁的查询数据库造成效率低下.spring 为我们提供了一套基于注解的缓存实现,方便我们实际的开发.我们可以扩展spring的cache接口以达 ...
- Mysql的入门和连接问题
Mysql的连接问题 最近学完了mysql的基础语法,基本上是掌握了mysql的简单运用. 1.入门mysql 我是通过看<漫画sql>入门的,这个视频案例很到位,跟着2倍速学前9章就可以 ...
- Verdi Transaction Debug Mode 简单使用
转载:Verdi Transaction Debug Mode 简单使用_Holden_Liu的博客-CSDN博客 文档与源码: User Guide: Verdi_Transaction_and_P ...