E - Preorder Test

思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的

最多节点, 第二次dfs求出以每个点为根的答案。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, k, a[N], dp[N], sz[N], mx[N], mx2[N];
vector<int> edge[N]; void dfs1(int u, int fa, int val) {
sz[u] = ;
mx[u] = , mx2[u] = , dp[u] = ;
for(int v : edge[u]) {
if(v == fa) continue;
dfs1(v, u, val);
sz[u] += sz[v];
if(dp[v] == sz[v]) dp[u] += dp[v];
else {
if(dp[v] >= mx[u]) mx2[u] = mx[u], mx[u] = dp[v];
else if(dp[v] > mx2[u]) mx2[u] = dp[v];
}
}
dp[u] += mx[u];
if(a[u] < val) dp[u] = ;
}
void dfs2(int u, int fa, int cnt, int val, int &ans) {
if(!dp[u]) {
for(int v : edge[u]) {
if(v == fa) continue;
dfs2(v, u, , val, ans);
}
} else {
int ret = dp[u];
if(cnt == n - sz[u]) ret = max(ret, dp[u] + cnt);
else if(cnt > mx[u]) ret = max(ret, dp[u] - mx[u] + cnt), mx2[u] = mx[u], mx[u] = cnt;
else if(cnt > mx2[u]) mx2[u] = cnt;
ans = max(ans, ret);
for(int v : edge[u]) {
if(v == fa) continue;
if(dp[v] == sz[v]) dfs2(v, u, ret-dp[v], val, ans);
else if(dp[v] == mx[u]) dfs2(v, u, ret-mx[u]+mx2[u], val, ans);
else dfs2(v, u, ret, val, ans);
}
}
} bool check(int val) {
dfs1(, , val);
int ans = ;
dfs2(, , , val, ans);
return ans >= k;
} int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
int low = , high = , ans = ;
while(low <= high) {
int mid = low + high >> ;
if(check(mid)) ans = mid, low = mid + ;
else high = mid - ;
}
printf("%d\n", ans);
return ;
} /*
*/

8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp的更多相关文章

  1. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  2. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

  4. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学

    C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...

  5. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题

    A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...

  7. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题

    http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...

  8. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card

    D - Travel Card 思路:dp,类似于单调队列优化. 其实可以写的更简单... #include<bits/stdc++.h> #define LL long long #de ...

  9. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

    A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. 转:RAC中比较replay, replayLast, and replayLazily

    A co-worker recently asked me about the difference between -replay, -replayLast, and -replayLazily i ...

  2. 安装lsb_release

    lsb_release命令用来查看当前系统的发行版信息(prints certain LSB (Linux Standard Base) and Distribution information.). ...

  3. 【转】Elastic日报 第576期 (2019-04-05)

    1.Elasticsearch性能测试实践http://t.cn/EiRzFiI2.监控Elasticsearch的插件推荐http://t.cn/EiRzFix3.支持机器数据的可扩展Elastic ...

  4. 手机中的js事件

    // 手势事件 touchstart //当手指接触屏幕时触发 touchmove //当已经接触屏幕的手指开始移动后触发 touchend //当手指离开屏幕时触发 touchcancel // 触 ...

  5. Python 豆瓣顶帖

    由于在豆瓣发了个租房帖子,发现很快就被其他的帖子淹没,但是手动顶帖实在太累,

  6. 20155210潘滢昊 2016-2017-2 《Java程序设计》第5周学习总结

    20155210 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 try with resources 关闭多个资源时用分号分隔 java.lang.Auto ...

  7. Throwable、Error、Exception、RuntimeException 区别

    1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...

  8. thinkphp 修改分页样式表

    原网页:http://blog.csdn.net/u014175572/article/details/53116546 在这里我有先把page的设置做成了一个函数getpage, 将这个方法放到Ap ...

  9. URL访问 和命名规范

    手册:http://v9.help.phpcms.cn/html/2010/structure_0928/71.html http://yourdomain.com/index.php?m=conte ...

  10. 【leetcode 简单】 第一百一十二题 重复的子字符串

    给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释 ...