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 ...
随机推荐
- 常用DNS列表(电信、网通)
电信 DNS 列表 -- 共 32 条 (按拼音排序) 电信 A安徽 202.102.192.68 202.102.199.68 电信 A澳门 202.175.3.8 202.175.3.3 ...
- 洛谷 2680 (NOIp2015) 运输计划
题目:https://www.luogu.org/problemnew/show/P2680 因为是最长的时间最短,所以二分! 离线LCA可以知道路径长度.每次只看超过二分值的路径. 原本的想法是遍历 ...
- H5浏览器播放RTMP直播流
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...
- 关于ESXI能虚拟出多少个虚拟机和CPU的关系
当你的虚拟机报如下错误的时候: esxi5.0版本最高配置: https://www.vmware.com/content/dam/digitalmarketing/vmware/zh-cn/pdf/ ...
- thinkphp中的volist标签
属性: name(必须):要输出的数据模板变量 id(必须):循环变量 offset(可选):要输出数据的offset length(可选):输出数据的长度 key(可选):循环的key变量,默认值为 ...
- DIKW模型与数据工程
DIKW 体系 DIKW体系是关于数据.信息.知识及智慧的体系,可以追溯至托马斯·斯特尔那斯·艾略特所写的诗--<岩石>.在首段,他写道:“我们在哪里丢失了知识中的智慧?又在哪里丢失了信息 ...
- Valid timeZone Values(转)
https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/timezone.html Valid time ...
- 服务器实现处理GET和POST
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...
- wireshark怎么抓包、wireshark抓包详细图文教程(转)
wireshark怎么抓包.wireshark抓包详细图文教程 wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息.使用wireshark的人必 ...
- struts2结合axis开发webservice
第一步:引入axis的依赖jar包 第二步:修改web.xml文件 <listener> <listener-class>org.apache.axis.transport ...