[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 ...
随机推荐
- windows中抓包命令,以及保存为多个文件的方法
本文主要介绍windows中抓包命令,以及保存为多个文件的方法 说一说保存为多个文件存储数据包这个问题的由来,一般如果长时间抓包,有可能需要等上几个小时,因为这个时候抓包的内容都是存放在内存中的,几个 ...
- 题解 2020.10.24 考试 T4 模板
题目传送门 题目大意 有一个 \(n\) 个点组成的树,有 \(m\) 次操作,每次将 \(1\to x\) 的路径上每个点都加入一个颜色为 \(c\) 的小球.但是每个点都有大小限制,即小球个数超过 ...
- CF850E Random Elections 题解
题目传送门 题目大意 没法描述,过于繁杂. 思路 果然自己是个菜鸡,只能靠读题解读题,难受极了,其实不是很难自己应该做得出来的....哎.... 不难发现可以统计 \(A\) 获胜的情况乘上 \(3\ ...
- Go语言核心36讲(Go语言进阶技术一)--学习笔记
07 | 数组和切片 我们这次主要讨论 Go 语言的数组(array)类型和切片(slice)类型. 它们的共同点是都属于集合类的类型,并且,它们的值也都可以用来存储某一种类型的值(或者说元素). 不 ...
- Pytorch——torch.nn.Sequential()详解
参考:官方文档 源码 官方文档 nn.Sequential A sequential container. Modules will be added to it in the order th ...
- 永久修改alias
永久修改alias home目录下ls -a显示隐藏文件 编辑./cshrc
- 小白自制Linux开发板 六. SPI TFT屏幕修改与移植
本文章参考:https://www.bilibili.com/read/cv9947785?spm_id_from=333.999.0.0 本篇通过SPI接口,使用ST7789V TFT焊接屏(13p ...
- JavaScript04
分离绑定事件 使用分离方式绑定元素事件可以使用页面元素与JavaScript代码完全分离,有利于代码分工和维护,是目前开发主流,分为两步: 1.获取需要绑定事件的元素 语法:根据id属性值取元素节点 ...
- python web1
***本篇中的测试均需要使用python3完成. 攻击以下面脚本运作的服务器. 针对脚本的代码逻辑,写出生成利用任意代码执行漏洞的恶意序列的脚本: 打开攻击机端口, 将生成的东西输入网页cookie: ...
- 异常大讨论-抛出异常还是返回false
iteye精华帖之异常大讨论 原帖链接http://www.iteye.com/topic/2038 Robbin的观点 观点1:Exception实际上代表了一个UseCase中的异常流的处理. 绝 ...