题目大意:
  一个$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的更多相关文章

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

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

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

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

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

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

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

  7. LeetCode Verify Preorder Sequence in Binary Search Tree

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

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

  9. Binary Tree Preorder Traversal

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

随机推荐

  1. ng双向数据绑定

    http://blog.csdn.net/callmekongkong/article/details/54601585

  2. Prepare and Deploy Windows Server 2016 Active Directory Federation Services

    https://docs.microsoft.com/en-us/windows/security/identity-protection/hello-for-business/hello-key-t ...

  3. jquery遍历之后代

    向下遍历dom树的jquery方法 children()方法返回被选元素的所有直接子元素,只会对向下一级对dom树进行遍历. 例子 代码: $(document).ready(function(){ ...

  4. Substrings(hdu 4455)

    题意: 给定一个序列ai,个数为n.再给出一系列w:对于每个w,求序列中,所有长度为w的连续子串中的权值和,子串权值为子串中不同数的个数. /* dp[i]表示长度为i的序列不同元素个数之和. 考虑从 ...

  5. Linux echo命令打印带有颜色的字

    一.命令格式如下: echo -e "\033[字背景颜色;文字颜色m字符串\033[0m"            例如:                      echo -e ...

  6. svn备份

    公司的svn体量很大,要是一不小心误删了SVN版本库,就要哭了,所以有了下面的备份脚本 #每个版本库完全备份 #!/bin/bash SOUR_SVN="/var/www/svn" ...

  7. Python学习笔记 - day2 - PyCharm的基本使用

    什么是IDE 开始学习的小白同学,一看到这三个字母应该是懵逼的,那么我们一点一点来说. 既然学习Python语言我们就需要写代码,那么代码写在哪里呢? 在记事本里写 在word文档里写 在sublim ...

  8. 【MT8382/8121】使用绝对路径编译模块会导致recourse_overlay无法应用的问题

    之前为了方便mm模块编译,写了个脚本,实现了在任意模块其子目录下执行脚本即可编译的功能. 其实原理就是一层一层目录地往上寻找Android.mk文件,找到存放Android.mk目录后,就把该目录当作 ...

  9. pandas 读写sql数据库

    如何从数据库中读取数据到DataFrame中? 使用pandas.io.sql模块中的sql.read_sql_query(sql_str,conn)和sql.read_sql_table(table ...

  10. dict的setdefault(学习bottle源代码)

    在bottle的源代码中,看到如下一段话: sys.modules.setdefault('bottle', sys.modules['__main__']) 这段话,实际上就是dict类型的setd ...