[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 ...
随机推荐
- TCP ------ RST的产生
产生RST的几个原因 1.请求超时 有89.27两台主机.主机89向主机27发送了一个SYN,表示希望连接8888端口,主机27回应了主机89一个SYN表示可以连接.但是主机89莫名其妙的发送了一个R ...
- MongoDB加索引
1.登陆MongoDB, 命令行在MongoDB主目录下执行:mongo -port 27017 2.切换至需要添加索引的db 并授权 use MeetingBooking db.auth({&quo ...
- es6+最佳入门实践(9)
9.Iterator和for...of 9.1.Iterator是什么? Iterator又叫做迭代器,它是一种接口,为各种不同的数据结构提供统一的访问机制.这里说的接口可以形象的理解为USB接口,有 ...
- session超时设置+超时页面跳转
session超时设置,方法有三种: (1)在主页面或者公共页面中加入:session.setMaxInactiveInterval(600);参数600单位是秒,即在没有10分钟活动后,sessio ...
- [洛谷P1528] 切蛋糕
洛谷题目链接:切蛋糕 题目描述 Facer今天买了n块蛋糕,不料被信息组中球球等好吃懒做的家伙发现了,没办法,只好浪费一点来填他们的嘴巴.他答应给每个人留一口,然后量了量每个人口的大小.Facer有把 ...
- Educational Codeforces Round 40 A B C D E G
A. Diagonal Walking 题意 将一个序列中所有的\('RU'\)或者\('UR'\)替换成\('D'\),问最终得到的序列最短长度为多少. 思路 贪心 Code #include &l ...
- Linux虚拟地址空间布局以及进程栈和线程栈总结【转】
转自:http://www.cnblogs.com/xzzzh/p/6596982.html 原文链接:http://blog.csdn.net/freeelinux/article/details/ ...
- pycharm配置教程
http://blog.csdn.net/u013088062/article/details/50100121
- win端git连接私服仓库+上传本地项目+从服务器下载文件到win
win端git连接私服仓库: 1.win端 检查c:/Users/用户/.ssh/目录下是否有config文件(!!!没有任何后缀名).如果没有则新建config文件,然后修改添加如下内容: Host ...
- java字节流和字符流编码格式(转自姚刚)
/** * 字节流和字符流的区别, * (1)字符流带有缓冲,必须flush或close后数据才会从缓存中写入磁盘文件. * 字节流直接写文件. * (2)字符流在创建流的时候,指定编码,字节流在文件 ...