传送门

题目大意

给出一棵无根树,每个节点有一个权值,现在要让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的更多相关文章

  1. Codeforces 627D Preorder Test(二分+树形DP)

    题意:给出一棵无根树,每个节点有一个权值,现在要让dfs序的前k个结点的最小值最大,求出这个值. 考虑二分答案,把>=答案的点标记为1,<答案的点标记为0,现在的任务时使得dfs序的前k个 ...

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

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

  4. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

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

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

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

  8. LeetCode Verify Preorder Sequence in Binary Search Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 题目: Given an a ...

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

随机推荐

  1. 1076. Wifi密码 (15)

    下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用wifi,又怕耽误亲们的学习,现将wifi密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答,每两日一换.谢 ...

  2. LeetCode Next Closest Time

    原题链接在这里:https://leetcode.com/problems/next-closest-time/description/ 题目: Given a time represented in ...

  3. oracle系统表的查询

    oracle查询用户下的所有表 select * from all_tab_comments -- 查询所有用户的表,视图等select * from user_tab_comments   -- 查 ...

  4. ORACLE系统表大全

    下面全面是对Oracle系统表的一些介绍: 数据字典dict总是属于Oracle用户sys的. 1.用户: select username from dba_users; 改口令 alter user ...

  5. window下TortoiseGit的安装和使用

    一.安装git for windows 首先下载git for windows客户端https://git-for-windows.github.io/安装过程没什么特别的,下载完安装包之后,按照提示 ...

  6. AngularJS:教程

    ylbtech-AngularJS:教程 1.返回顶部 1. AngularJS 教程 AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 可以构建一个单一页面应用程序(S ...

  7. An internal error occurred during: "Map/Reducelocation status updater".java.lang.NullPointerException

    当我们运行wordcount代码时,出现报错,如下所示: An internal error occurred during: "Map/Reducelocation status upda ...

  8. mybatis foreach标签的解释 与常用之处

    情景:查询数据库中文章的相关文章   文章为一个表 字段tags为相关文章字符串中间用','逗号进行啦分割 查询完一个文章后可以把tags字段构造为一个List<String> 然后利用这 ...

  9. djano-cbv模式

    cbv,class base view 就是用类写视图 详细用法替换fbv将在后续空余时间上补齐 fbv,function base view 就是用函数写视图 创建django app01项目 ur ...

  10. verilog HDL 编码风格

    1.有意义且有效的名字. 2.同一信号在不同层次应该保持一致. 3.添加有意义的后缀,使信号的有效性更加明确. 4.模块输出寄存器化,使得输出的驱动强度和输入延时是可以预测的. 5.使用括号表明优先级 ...