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 ...
随机推荐
- python---django中权限框架设计
一:admin下的权限了解 推文:如何正确使用 Django的User Model (一)默认权限表是在自带auth模块,中permission表中 可以使用has_perm方法获取用户是否有这个权限 ...
- bayer, yuv, RGB转换方法
因为我的STVxxx USB camera输出格式是bayer格式,手头上只有YUVTOOLS这个查看工具,没法验证STVxxx在开发板上是否正常工作. 网上找了很久也没找到格式转换工具,最后放弃了, ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- JS代码判断浏览器版本,支持IE6,IE7,IE8,IE9!三种方法!
web开发的时候有时候会用到JS检测IE的版本,下面是检测Microsoft Internet Explorer版本的三种代码! 方法一: <script type="text/jav ...
- sklearn_模型遍历
# _*_ coding = utf_8 _*_ import matplotlib.pyplot as plt import seaborn as sns import pandas as pd f ...
- Dummynet模拟高时延网络场景(Windows7)
如果安装时出现:my_socket failed 2, cannot talk to kernel module 请查看是否以管理员方式运行,如果是,再判断当前操作系统是否为Win7 64位,如果是, ...
- mysql基准测试工具tpcc-mysql安装、使用、结果解读
TPCC是专门针对联机交易处理系统(OLTP系统)的规范,一般情况下我们也把这类系统称为业务处理系统,tpcc-mysql是percona基于TPC-C(下面简写成TPCC)衍生出来的产品,专用于My ...
- Apple Notification Center Service--ANCS【转】
Apple Notification Center Service 转自:http://studentdeng.github.io/blog/2014/03/22/ancs/ MAR 22ND, 20 ...
- 安装mysql5.5的时候出现Error Nr.1045
解决办法: 1.删除注册表几个键值:HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application\MySQL HKEY_L ...
- [shell]shell中if语句的使用
转自:http://lovelace.blog.51cto.com/1028430/1211353 bash中如何实现条件判断?条件测试类型: 整数测试 字符测试 文件测试 一.条件 ...