8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp
思路:想到二分答案了之后就不难啦, 对于每个答案用树形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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...
- 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 ...
- 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 ...
随机推荐
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...
- MongoDB - Introduction to MongoDB, Databases and Collections
MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases. Data ...
- Java入门系列(五)JVM内存模型
概述 根据<Java 虚拟机规范>中的说法,Java 虚拟机的内存结构可以分为公有和私有两部分. 公有指的是所有线程都共享的部分,指的是 Java 堆.方法区.常量池. 私有指的是每个线程 ...
- 浅谈ASP.net中的DataSet对象
在我们对数据库进行操作的时候,总是先把数据从数据库取出来,然后放到一个"容器"中,再通过这个"容器"取出数据显示在前台,而充当这种容器的角色中当属DataSet ...
- TED_Topic6:How to raise a black son in America
By Clint Smith As kids, we all get advice from parents and teachers that seems strange, even confusi ...
- TED_Topic4:How I fell in love with quasars, blazars and our incredible universe
By Jedidah Isler # Background about our speaker Jedidah Isler studies blazars(耀变天体) — supermassive h ...
- C++利用cin输入时检测回车的方法
今天做TJU的OJ ,其中一道题是先读入一个字符串,再读入一个整数,循环往复,直到字符串是空,也就是说回车键结束循环. 但是cin对空格和回车都不敏感,都不影响继续读入数据,所以需要一种新的方式检测回 ...
- Let Me Count The Ways(Kickstart Round H 2018)
题目链接:https://code.google.com/codejam/contest/3324486/dashboard#s=p2 题目: 思路: 代码实现如下: #include <set ...
- 【数据库】SQL经典面试题 - 数据库查询 - 子查询应用二
上节课我们通过子查询,完成了查询的最高分学生的需求,今天我们来学习子查询的分类,以及通过子查询来完成工作中经常遇到一些个性化需求. 子查询概念: 一个SELECT语句嵌套在另一个SELECT语句中,子 ...
- 初涉sqlmap
1.基本注入(这个工具kali或者bt下面有集成的,这里附加一个window免py版,提取码:3ldv) sqlmap -u http://url/xx.php?id=1 判断注入 sqlmap - ...