传送门

题目大意

给出一棵无根树,每个节点有一个权值,现在要让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. Java Platform SE binary已停止运行 Can't load AMD 64-bit.dll on a IA 32-bit platform错误

    这个我是在junit测试web项目时候遇到的问题,然后今天遇到一位网友也遇到了,报问题的是A卡了,所以今天做个记载,方便遇到问题的人,因为,之前我个人遇到这个问题,在网上找了很久都没有找到. 上面这个 ...

  2. 模仿Masonry链式编程思想

    使用masonry 也将近一年多了,它的链式编程方式一直是很吸引我的. 之前一直没空好好思考它是如何实现,直到现在正好自己有空,因此写下链式编程的基本思路. 链式基本的编程形式如 a.property ...

  3. HttpWebRequest传值

    From:发送方 class Program { static void Main(string[] args) { string strId = "zhangsan"; &quo ...

  4. LeetCode Find Duplicate File in System

    原题链接在这里:https://leetcode.com/problems/find-duplicate-file-in-system/description/ 题目: Given a list of ...

  5. UVA10674 Tangents

    题意 PDF 分析 就是圆的切线的模板. 注意精度问题,排序的时候也不能直接写,被卡了好几次. 时间复杂度\(O(T)\) 代码 #include<iostream> #include&l ...

  6. web 模板引擎

    baiduTemplate:  http://baidufe.github.io/BaiduTemplate/ artTemplate: https://github.com/aui/artTempl ...

  7. 【openCV学习笔记】【3】高斯模糊一张图片(_cvSmooth相关编译错误)

    代码如下: #include <iostream> #include <opencv/highgui.h> #include <opencv/cv.h> void ...

  8. laravel 中条件查询 function模式

    当需要条件查找时,可以使用下面的注入方法: //我要预约 yudoc_name yudoc_keshi yudoc_jibing yudoc_hospital 这是需要帅选的条件 public fun ...

  9. 安装Visual C ++进行跨平台移动开发

    Visual Studio 2015   Visual Studio文档的新家是docs.microsoft.com上的Visual Studio 2017文档 . 有关Visual Studio 2 ...

  10. TCP之一:传输控制协议(Transmission Control Protocol, TCP)

    TCP协议主为了在主机间实现高可靠性的包交换传输协议.本文将描述协议标准和实现的一些方法.因为计算机网络在现代社会中已经是不可缺少的了,TCP协议主要在网络不可靠的时候完成通信,对军方可能特别有用,但 ...