627D Preorder Test
题目大意
给出一棵无根树,每个节点有一个权值,现在要让dfs序的前k个结点的最小值最大,求出这个值。
分析
首先可以对这个值v进行二分然后01分数规划
现在问题转化为求出一个dfs序,使得dfs序中的至少有k个1,这一步可以用树形dp来做。
用dp[u]表示从节点u开始在子树中进行dfs最多可以经过多少个为1的结点,显然,若某一个子树中节点全为1,那么这个可以加到dp[u]中,此外还可以在不全为1的子树中挑选一个加到dp[u]上。
现在考虑我们要求的答案,对于当前的结点u,那么答案为两棵不完全子树的dp值加上所有的完全子树(父亲往上延伸的部分也认为是子树),现在考虑父亲往上延伸的部分
若父亲往上延伸的部分是一个完全子树,那么只需要加上这个值即可
问题的关键是如果父亲往上延伸的部分是一棵不完全子树该怎么做,可以这样想,从当前结点直到祖先节点中,肯定有一个结点从父亲往上延伸部分要么是一棵完全子树,要么不能往上延伸,所以对于每一个子树,我们只需要处理父亲往上延伸为一棵完整子树的情况即可,因为不完全子树这种情况肯定会在祖先节点中被处理于是我们用所有数中的最小值作为根,于是根在除了答案为最小值的情况下均为0,这样就可以避免父亲往上是一棵完整子树的情况了
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int n,k,a[],now[],siz[],Ans,all[],mid,pl;
vector<int>v[];
inline void work(int x,int fa){
siz[x]=all[x]=;
int maxn=,sen=;
for(int i=;i<v[x].size();i++)
if(v[x][i]!=fa){
work(v[x][i],x);
all[x]+=all[v[x][i]];
if(siz[v[x][i]]==all[v[x][i]])siz[x]+=siz[v[x][i]];
else {
if(siz[v[x][i]]>maxn)swap(maxn,siz[v[x][i]]);
if(siz[v[x][i]]>sen)swap(sen,siz[v[x][i]]);
}
}
siz[x]+=maxn;
if(a[x]<mid)siz[x]=;
Ans=max(Ans,siz[x]+sen);
}
inline bool ck(){
Ans=;
work(pl,);
if(Ans>=k)return ;
else return ;
}
int main(){
int i,minn=1e9+;
scanf("%d%d",&n,&k);
for(i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]<minn)minn=a[i],pl=i;
}
for(i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
int le=,ri=1e6+;
while(ri-le>){
mid=(le+ri)>>;
if(ck())le=mid;
else ri=mid;
}
cout<<le;
return ;
}
627D Preorder Test的更多相关文章
- Codeforces 627D Preorder Test(二分+树形DP)
题意:给出一棵无根树,每个节点有一个权值,现在要让dfs序的前k个结点的最小值最大,求出这个值. 考虑二分答案,把>=答案的点标记为1,<答案的点标记为0,现在的任务时使得dfs序的前k个 ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)
1. Description One way to serialize a binary tree is to use pre-order traversal. When we encounter a ...
- Leetcode 255. Verify Preorder Sequence in Binary Search Tree
验证一个list是不是一个BST的preorder traversal sequence. Given an array of numbers, verify whether it is the co ...
- LeetCode Verify Preorder Sequence in Binary Search Tree
原题链接在这里:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 题目: Given an a ...
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
随机推荐
- 因实现本地浏览器访问nginx修改配置文件后,安装vsftpd失败
解决方法如下(修改dns配置) vi /etc/resolv.conf 在此文件最后加入:nameserver 8.8.8.8 如果没有vi编辑器可用: echo "nameserver 8 ...
- sublime text 3设置浏览器快捷键
一.设置默认浏览器 1,打开sublime 依次选择 tools > build system > new build system... 2,选择你喜欢的浏览器,右键 > 属性 把 ...
- Pod Installing openssl-ios-bitcode报错
pod update --no-repo-update 问题:执行上面的操作之后,pod在安装ssl的时候会报错.部分报错信息如下: localhost:OpenSSLTest Later$ pod ...
- bzoj 1597 [Usaco2008 Mar]土地购买——斜率优化dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1597 又一道斜率优化dp.负数让我混乱.不过仔细想想还是好的. 还可以方便地把那个负号放到x ...
- Java 多个引用类型变量引用同一个对象
引用类型变量在声明后必须引用对象才能使用. 一个引用变量只能唯一指向一个对象,但同一个对象可被多个引用类型变量引用. 如:MyDate today; //将变量跟配给一个保存引用的空间(栈) toda ...
- 题目1534:数组中第K小的数字 ——二分
http://ac.jobdu.com/problem.php?pid=1534 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C.譬如A为[1,2],B为[3,4].那么由A和B中 ...
- Jenkins的用户管理
用户管理入口 Jenkins首页有一个用户,但是只能从那查看用户列表和信息,管理用户的入口在Jenkins->系统管理->管理用户 新建用户 在管理用户左侧有一个新增用户,点击后按照表单填 ...
- Angular4中利用promise异步加载gojs
GoJS是一个实现交互类图表(比如流程图,树图,关系图,力导图等等)的JS库 gojs提供了angular的基本例子,不过是离线版 https://github.com/NorthwoodsSoftw ...
- day5心得
import 模块 1.定义: 模块:用来从逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能),本质就是.py结尾的python文件(文件名test.py 模块名:test) 2导入方法 ...
- Spring Cloud Eureka 5 (服务发现与消费-简单的robbin使用)
通过上述介绍,我们已经有了服务注册中心和服务提供者 下面我们来尝试构建一个服务的消费者 它要完成两个功能,发现服务和消费服务,其中发现服务由eureka客户端完成,消费服务由ribbon完成. rib ...