[CF627D]Preorder Test
题目大意:
一个$n(n\le2\times10^5)$个结点的树,每个结点有一个权值$w_i$。可以任选一点为根,并选择一些结点交换其子结点的顺序,使得该树DFS序上第$m$个结点的权值最大。求最大权值。
思路:
二分答案$k$ ,树形DP检验可行性。
对于以结点$x$为根的子树,用$f[x]$表示$x$的子树经过任意变换的所有DFS序中,满足$w_i\ge k$的最长前缀长度。
若$w_x<k$,则$f[x]$显然为$0$。
若$w_x\ge k$,则对于$x$的每个子结点$y$,若$f[y]=size[y]$,将$y$的子树插入到$x$子树DFS序的最前面仍然是合法的;而对于所有满足$f[y]<size[y]$的子结点$y$,可以选择一个$f[y]$最大的加入。考虑以$x$为根的情况,则只需要在$f[x]$上加上满足$f[y]<size[y]$的$f[y]$第二大的$f[y]$。此时不需要考虑加上的会是$x$上方的点,因为加上$x$上方的点的情况肯定能在$x$上方的点处统计到。
时间复杂度$O(n)$。
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<forward_list>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=2e5+;
int w[N],f[N],k,size[N],ans,root;
std::forward_list<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_front(v);
e[v].push_front(u);
}
void dfs(const int &x,const int &par) {
f[x]=size[x]=;
int max1=,max2=;
for(register auto &y:e[x]) {
if(y==par) continue;
dfs(y,x);
size[x]+=size[y];
if(f[y]==size[y]) {
f[x]+=f[y];
} else {
if(f[y]>max1) std::swap(f[y],max1);
if(f[y]>max2) std::swap(f[y],max2);
}
}
f[x]=w[x]<k?:f[x]+max1;
ans=std::max(ans,f[x]+max2);
}
inline int calc() {
dfs(root,ans=);
return ans;
}
int main() {
const int n=getint(),m=getint();
for(register int i=;i<=n;i++) w[i]=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
root=std::min_element(&w[],&w[n]+)-w;
int l=*std::min_element(&w[],&w[n]+);
int r=*std::max_element(&w[],&w[n]+);
while(l<=r) {
k=(l+r)>>;
if(calc()>=m) {
l=k+;
} else {
r=k-;
}
}
printf("%d\n",l-);
return ;
}
[CF627D]Preorder Test的更多相关文章
- [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 ...
- Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
随机推荐
- 学习python类
类:Python 类提供了面向对象编程的所有基本特征: 允许多继承的类继承机制, 派生类可以重写它父类的任何方法, 一个方法可以调用父类中重名的方法. 对象可以包含任意数量和类型的数据成员. 作为模块 ...
- 配置Tomcat时server.xml和content.xml自动还原问题
当我们在处理中文乱码或是配置数据源时,我们要修改Tomcat下的server.xml和content.xml文件. 但是当我们修改完后重启Tomcat服务器时发现xml文件又被还原了,修改无效果. 为 ...
- 设置eclipse控制台上的信息输入到某个文件
转摘自:http://binary.duapp.com/2013/09/1511.html Run->Run Configurations->Common->File
- 前端面试:什么是css reset
HTML标签在浏览器中都有默认的样式,不同的浏览器的默认样式之间存在差别.例如ul默认带有缩进样式,在IE下,它的缩进是由margin实现的,而在Firefox下却是由padding实现的.开发时浏览 ...
- css做中划线与文字排版
html: <div class="spilt"> <span class="left"></span> < ...
- (转载)--SG函数和SG定理【详解】
在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ...
- python读写Excel文件_xlrd模块读取,xlwt模块写入
一.安装xlrd模块和xlwt模块(服务器) 1. 下载xlrd模块和xlwt模块 到python官网http://pypi.python.org/pypi/xlrd下载模块.下载的文件例如:xlrd ...
- Spring - IoC(9): @Resoure & @Autowired
@Resource 和 @Autowired 都是用来装配依赖的,它们之间有些异同. @Resoure @Resource 是 JSR-250 规范的注解. @Resource 可以标注在字段.方法上 ...
- LeetCode 192:Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 基于vlc sdk的二次开发--环境搭建、编译
前言 关于 搭建.编译VLC,不同的平台有不同的方法,可以参考wiki. 其中在windows下编译VLC有两种方式,MSYS+MinGW和CygWin.通过测试,最后决定采用MSYS+MinGW搭建 ...