All You Can Code 2008 (Romanian Contest) A - Tree Search
思路:
经典树形dp
dp[i][0]表示i的子树中以i为端点的最大链
dp[i][1]表是整棵树中除去i的子树剩下的部分以i为端点的最大链
最后答案就是以i为端点的最大链和次大链拼起来(除了一些特殊情况,比如一条链更大,或者只有一条链)
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + ;
vector<int> g[N];
int a[N], ans[N];
int dp[N][];
void dfs1(int u, int o) {
dp[u][] = a[u];
for (int v : g[u]) {
if(v != o) {
dfs1(v, u);
dp[u][] = max(dp[u][], a[u] + dp[v][]);
}
}
}
void dfs2(int u, int o) {
if(u == ) dp[u][] = a[u];
int v1, mx1 = INT_MIN, v2, mx2 = INT_MIN;
for (int v : g[u]) {
if(v != o) {
int tmp = dp[v][] + a[u];
if(tmp > mx1) {
mx2 = mx1;
v2 = v1;
mx1 = tmp;
v1 = v;
}
else if(tmp == mx1 || tmp > mx2) {
mx2 = tmp;
v2 = v;
}
}
}
if(dp[u][] > mx1) {
mx2 = mx1;
v2 = v1;
mx1 = dp[u][];
v1 = -;
}
else if(dp[u][] == mx1 || dp[u][] > mx2){
mx2 = dp[u][];
v2 = -;
}
ans[u] = a[u];
if(mx1 != INT_MIN && mx2 != INT_MIN) ans[u] = max(ans[u], mx1 + mx2 - a[u]);
else if(mx1 != INT_MIN) ans[u] = max(ans[u], mx1);
else ans[u] = max(ans[u], mx2);
ans[u] = max(ans[u], mx1);
ans[u] = max(ans[u], mx2);
for (int v : g[u]) {
if(v != o) {
dp[v][] = a[v];
if(v == v1) {
dp[v][] = max(dp[v][], mx2 + a[v]);
}
else {
dp[v][] = max(dp[v][], mx1 + a[v]);
}
dfs2(v, u);
}
} }
int main() {
int n, m, u, v, q;
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i < n; i++) {
scanf("%d %d", &u, &v);
g[u].pb(v);
g[v].pb(u);
}
dfs1(, );
dfs2(, );
while(m--) {
scanf("%d", &q);
printf("%d\n", ans[q]);
}
return ;
}
All You Can Code 2008 (Romanian Contest) A - Tree Search的更多相关文章
- @atcoder - CODE FESTIVAL 2017 Final - J@ Tree MST
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 N 个点,第 i 点有一个点权 Xi,再给定一棵边带权的树 ...
- #Leet Code# Convert Sorted Array to Binary Search Tree
描述:递归 代码: class Solution: # @param num, a list of integers # @return a tree node def sortedArrayToBS ...
- [CODE FESTIVAL 2016]Problem on Tree
题意:给一棵树,对于一个满足以下要求的序列$v_{1\cdots m}$,求最大的$m$ 对$\forall1\leq i\lt m$,路径$(v_i,v_{i+1})$不包含$v$中除了$v_i,v ...
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树全部左右子数 ...
- Code Forces Bear and Forgotten Tree 3 639B
B. Bear and Forgotten Tree 3 time limit per test2 seconds memory limit per test256 megabytes inputst ...
- The 2019 ICPC Asia Shanghai Regional Contest H Tree Partition k、Color Graph
H题意: 给你一个n个节点n-1条无向边构成的树,每一个节点有一个权值wi,你需要把这棵树划分成k个子树,每一个子树的权值是这棵子树上所有节点权值之和. 你要输出这k棵子树的权值中那个最大的.你需要让 ...
- IOCCC(The International Obfuscated C Code Contest)
国际 C 语言混乱代码大赛(IOCCC, The International Obfuscated C Code Contest)是一项国际编程赛事,从 1984 年开始,每年举办一次(1997年.1 ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B. Code obfuscation 水题
B. Code obfuscation 题目连接: http://codeforces.com/contest/765/problem/B Description Kostya likes Codef ...
- Code obfuscatio (翻译!)
Description Kostya likes Codeforces contests very much. However, he is very disappointed that his so ...
随机推荐
- python_代码中调用java类
1. 安装jpype (python调用java class文件用) 1.1. 自动安装:pip install jpype1 1.2. 手动方式安装jpype1 安装wheel:pip instal ...
- C语言中tm结构体
struct tm { int tm_sec; /* Seconds. [0-60] (1 leap second) */ int tm_min; /* Minutes. [0-59] */ int ...
- bugfree3.0.1-BUG解决方案修改
该篇内容来自文档“masterBugFree2.pdf”,记录在这里. 1.如何将解决方案改为中文 在\Bugfree\Lang\ZH_CN_UTF-8 \_COMMON.php 文件中做如下修改/* ...
- python框架之Flask(1)-Flask初使用
Flask是一个基于 Python 开发并且依赖 jinja2 模板和 Werkzeug WSGI 服务的一个微型框架,对于 Werkzeug 本质是 Socket 服务端,其用于接收 http 请求 ...
- [js]面向对象2
delete删除属性 删除对象的属性 删除未用var定义的变量. delete返回布尔 删除不存在的属性,返回true 无法删除原形中的属性 如 delete obj.toString() resu= ...
- js $的扩展写法
(function ($, w) { $.extend({ getRenderList: function (keyword, pageIndex, pageSize, renderEle, re ...
- 编译器将"+"转换成了StringBuilder类
MapReduce map100% Reduce 66% 卡死 如果你碰到map100%,reduce 66% 然后程序就貌似停止在这里了,可能是由于在Reduce类里使用了String造成的 根据一 ...
- 如何消除img默认的间距
方案一:div{font-size:0};方案二:img{ display:block};方案三:img{vertical-align:top;}方案四:div{ margin-bottom:-3px ...
- windows----------windows查看端口是否被占用
假如我们需要确定谁占用了我们的80端口在windows命令行窗口下执行: netstat -aon|findstr "80" TCP 127.0.0.1:80 0.0.0.0:0 ...
- netframework webapi exceptionless
1.webapi项目 添加nuget exceptionless webapi 2.在exceptionless server端添加项目,注意key 3.修改api项目的webconfig &l ...