[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 ...
随机推荐
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- lwIP RAW_API
lwIP RAW TCP/IP接口 作者: Adam Dunkels, Leon Woestenberg, Christiaan Simons lwIP为使用TCP/IP协议通信的应用程序编程提供了两 ...
- Java super和this
this this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针. this的用法在java中大体可以分为3种: 1.普通的直接引用 这种就不用讲了,this相当于是指向当前对象本 ...
- jw player学习笔记三---发布到其它网站
一.通过官网发布向导 登陆 http://www.longtailvideo.com,注册一个账号,进入你的用户管理中心,如下图 然后按提示,一步步操作,就会得到js代码了. 二.自己服务器发布 &l ...
- React 使用 fetch 请求天气
中国天气网(http://www.weather.com.cn)提供了查询天气的 API,通过传入城市 id, 可以获得当前城市的天气信息,API 相关信息如下: 规格 描述 主机地址 http:/ ...
- JVM内存模型 三
本文章节: 1.JMM简介 2.堆和栈 3.本机内存 4.防止内存泄漏 1.JMM简介 i.内存模型概述 Java平台自动集成了线程以及多处理器技术,这种集成程度比Java以前诞生的计算机语言要厉 ...
- HASHMAP 深入解析
http://blog.csdn.net/ghsau/article/details/16843543/
- 【洛谷 P3805】 【模板】manacher算法
题目链接 manacher算法:在线性时间内求一个字符串中所有/最长回文串的算法. 先来考虑一下暴力的算法,枚举每个中点,向两边扩展,时间复杂度\(O(n^2)\). 来分析下此算法的缺点. 1.因为 ...
- [bzoj4602][Sdoi2016]齿轮——dfs
题目 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x : y.即如果只考虑这两个组合齿轮,编号为u的齿轮转动x圈,编号为v的齿轮会转动y圈.传 ...
- C/C++里的const(1)
首先来看这样一段程序: #include<iostream> using namespace std; int main(){ char *s = "hello world&qu ...