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

  1. Solution -「ARC 125F」Tree Degree Subset Sum

    \(\mathcal{Description}\)   Link.   给定含有 \(n\) 个结点的树,求非负整数对 \((x,y)\) 的数量,满足存在 \(\exist S\subseteq V ...

  2. [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 ...

  3. 【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 ...

  4. 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 ...

  5. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  6. 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 ...

  7. 动态规划法(三)子集和问题(Subset sum problem)

      继续讲故事~~   上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界.   这天,丁丁刚回到家,他 ...

  8. 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 ...

  9. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

随机推荐

  1. 1-Java继承中多态情况特性下变量,方法,静态方法的访问

    在Java继承下,多态特性下类成员访问情况 /* 在继承中,变量时静态的绑定的,非静态方法是动态的绑定的,静态方法是静态绑定的 */ class Parent{ int number = 11; pu ...

  2. Arcscene教程

    ​ ​ ​ ​ ​ ​ ​ ​ ​ ​​ ​ 筛选​ ​ ​ ​ ​ ​ ​ ​ 看不清的话可以进行如下操作:右键-->属性-->符号系统-->把高程前面的对号取消-->添加- ...

  3. C++ IDE或编辑器安装

    IDE介绍 上节课我们讲了C++编译器,可是没有好的编辑器,只用记事本打代码,这谁受得了.Linux vim至少还有代码高亮(即我作文里经常会出现的"彩色的代码"),记事本连高亮都 ...

  4. 解决更新页面版本后用户需CTRL+F5强刷才能应用最新页面

    设置文件永远不从缓存读取 第一步:在html文件设置文件不缓存 <!DOCTYPE html> <html lang="en" class="theme ...

  5. 【UE4 设计模式】单例模式 Singleton Pattern

    概述 描述 保证一个类只有一个实例 提供一个访问该实例的全局节点,可以视为一个全局变量 仅在首次请求单例对象时对其进行初始化. 套路 将默认构造函数设为私有, 防止其他对象使用单例类的 new运算符. ...

  6. JBOSS未授权访问漏洞利用

    1. 环境搭建 https://www.cnblogs.com/chengNo1/p/14297387.html 搭建好vulhub平台后 进入对应漏洞目录 cd vulhub/jboss/CVE-2 ...

  7. 力扣 - 剑指 Offer 53 - II. 0~n-1中缺失的数字

    题目 剑指 Offer 53 - II. 0-n-1中缺失的数字 思路1 排序数组找数字使用二分法 通过题目,我们可以得到一个规律: 如果数组的索引值和该位置的值相等,说明还未缺失数字 一旦不相等了, ...

  8. 记录编译QGIS(3.4+Qt5.11+VS2015)的整个过程

    编译运行整个QGIS耗时耗力,由于本人比较愚钝,来来回回花了大概两个星期最终编译成功,记录一下整个过程,一方面备忘,另一方面可能也给别人一点借鉴. 1.准备工作 参考了许多网上的教程,李民录大神的&l ...

  9. 预备知识-python核心用法常用数据分析库(上)

    1.预备知识-python核心用法常用数据分析库(上) 目录 1.预备知识-python核心用法常用数据分析库(上) 概述 实验环境 任务一:环境安装与配置 [实验目标] [实验步骤] 任务二:Pan ...

  10. Elasticsearch核心技术(三):Mapping设置

    本文从Mapping简介.Dynamic Mapping.自定义Mapping和Mapping常用参数说明4个部分介绍Elasticsearch如何设置Mapping. 3.1 Mapping简介 3 ...