Codeforces 633F 树的直径/树形DP
题意:有两个小孩玩游戏,每个小孩可以选择一个起始点,并且下一个选择的点必须和自己选择的上一个点相邻,问两个选的点权和的最大值是多少?
思路:首先这个问题可以转化为求树上两不相交路径的点权和的最大值,对于这种问题,我们有两种想法:
1:树的直径,受之前HDU多校的那道题的启发,我们先找出树的直径,然后枚举保留直径的哪些部分,去找保留这一部分的最优解,去更新答案。
代码:
#include <bits/stdc++.h>
#define INF 1e18
#define LL long long
using namespace std;
const int maxn = 100010;
vector<int> G[maxn];
LL tot;
int now, f[maxn];
LL d[maxn], val[maxn], sum[maxn];
bool v[maxn], v1[maxn];
LL mx[maxn];
void add(int x, int y) {
G[x].push_back(y);
G[y].push_back(x);
}
void dfs(int x, int fa, LL sum) {
sum += val[x];
v1[x] = 1;
f[x] = fa;
if(sum > tot) {
now = x;
tot = sum;
}
for (auto y : G[x]) {
if(y == fa || v[y]) continue;
dfs(y, x, sum);
}
}
void dfs1(int x, int fa) {
d[x] = val[x];
LL tmp = 0;
for (auto y : G[x]) {
if(y == fa || v[y]) continue;
dfs1(y, x);
tmp = max(tmp, d[y]);
}
d[x] += tmp;
return;
}
vector<int> a;
int main() {
int n, x, y;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &val[i]);
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
}
LL ans = 0;
tot = 0, now = 0;
dfs(1, 0, 0);
tot = 0;
dfs(now, 0, 0);
for (int i = now; i; i = f[i]) {
a.push_back(i);
sum[a.size()] = sum[a.size() - 1] + val[i];
v[i] = 1;
}
for (auto y : a) {
dfs1(y, 0);
}
memset(v1, 0, sizeof(v1));
for (int i = 1; i <= n; i++) {
if(v[i] == 1 || v1[i] == 1) continue;
tot = now = 0;
dfs(i, 0, 0);
tot = 0;
dfs(now, 0, 0);
ans = max(ans, tot + sum[a.size()]);
}
mx[a[a.size() - 1]] = d[a[a.size() - 1]];
for (int i = a.size() - 2; i >= 1; i--) {
mx[a[i]] = max(mx[a[i + 1]], sum[a.size()] - sum[i] + d[a[i]] - val[a[i]]);
}
for (int i = 0; i < a.size() - 1; i++) {
ans = max(ans, d[a[i]] + sum[i] + mx[a[i + 1]]);
}
printf("%lld\n", ans);
}
思路2:树形DP,我们对于每个点保留从它到叶子节点的最长路径和次长路径,以及以它为根的子树中的最长路径。dp完之后,我们对于每个节点,枚举选哪棵子树中的节点作为一条路径,然后去寻找另一条最长路径。可以用前缀最大值和后缀最大值去优化,以及需要注意需要考虑父节点方向对答案的影响。
代码:
#include <bits/stdc++.h>
#define LL long long
#define pll pair<LL, LL>
using namespace std;
const int maxn = 100010;
vector<int> G[maxn];
LL mx_path[maxn], mx_dis[maxn];
LL lmx_path[maxn], rmx_path[maxn];
pll lmx_dis[maxn], rmx_dis[maxn];
LL val[maxn];
LL ans = 0;
void add(int x, int y) {
G[x].push_back(y);
G[y].push_back(x);
}
void dfs(int x, int fa) {
vector<LL> tmp(3);
for (auto y : G[x]) {
if(y == fa) continue;
dfs(y, x);
tmp[0] = mx_dis[y];
sort(tmp.begin(), tmp.end());
mx_path[x] = max(mx_path[y], mx_path[x]);
}
mx_dis[x] = tmp[2] + val[x];
mx_path[x] = max(mx_path[x], tmp[1] + tmp[2] + val[x]);
return;
}
int tot, a[maxn];
struct node {
int x, fa;
LL mx_p, mx_d;
};
queue<node> q;
void solve() {
q.push((node){1, 0, 0, 0});
while(q.size()) {
node tmp = q.front();
q.pop();
tot = 0;
int x = tmp.x, fa = tmp.fa;
tot = 0;
for (int i = 0; i < G[x].size(); i++) {
if(G[x][i] == fa) continue;
a[++tot] = G[x][i];
}
rmx_path[tot + 1] = rmx_path[tot + 2] = 0;
rmx_dis[tot + 1] = rmx_dis[tot + 2] = make_pair(0, 0);
for (int i = 1; i <= tot; i++) {
lmx_path[i] = max(lmx_path[i - 1], mx_path[a[i]]);
lmx_dis[i] = lmx_dis[i - 1];
if(mx_dis[a[i]] >= lmx_dis[i].first) {
lmx_dis[i].second = lmx_dis[i].first;
lmx_dis[i].first = mx_dis[a[i]];
} else if(mx_dis[a[i]] > lmx_dis[i].second) {
lmx_dis[i].second = mx_dis[a[i]];
}
}
for (int i = tot; i >= 1; i--) {
rmx_path[i] = max(rmx_path[i + 1], mx_path[a[i]]);
rmx_dis[i] = rmx_dis[i + 1];
if(mx_dis[a[i]] >= rmx_dis[i].first) {
rmx_dis[i].second = rmx_dis[i].first;
rmx_dis[i].first = mx_dis[a[i]];
} else if(mx_dis[a[i]] > rmx_dis[i].second) {
rmx_dis[i].second = mx_dis[a[i]];
}
}
LL tmp1 = 0;
// printf("%d\n", x);
// for (int i = 1; i <= tot; i++) {
// printf("%lld %lld\n", lmx_path[i], rmx_path[i]);
// }
for (int i = 1; i <= tot; i++) {
LL tmp2 = 0;
tmp2 = max(tmp2, lmx_path[i - 1]);
tmp2 = max(tmp2, rmx_path[i + 1]);
tmp2 = max(tmp2, tmp.mx_p);
tmp2 = max(tmp2, val[x] + lmx_dis[i - 1].first + tmp.mx_d);
tmp2 = max(tmp2, val[x] + rmx_dis[i + 1].first + tmp.mx_d);
tmp2 = max(tmp2, val[x] + lmx_dis[i - 1].first + lmx_dis[i - 1].second);
tmp2 = max(tmp2, val[x] + rmx_dis[i + 1].first + rmx_dis[i + 1].second);
tmp2 = max(tmp2, val[x] + lmx_dis[i - 1].first + rmx_dis[i + 1].first);
tmp2 = max(tmp2, val[x] + tmp.mx_d + max(lmx_dis[i - 1].first, rmx_dis[i + 1].first));
// printf("%lld ", tmp.mx_d);
// printf("%lld ", tmp2);
q.push((node){a[i], x, tmp2, val[x] + max(max(lmx_dis[i - 1].first, rmx_dis[i + 1].first), tmp.mx_d)});
tmp2 += mx_path[a[i]];
// printf("%lld\n", tmp2);
tmp1 = max(tmp1, tmp2);
}
// printf("\n");
ans = max(ans, tmp1);
ans = max(ans, tmp.mx_p + mx_path[x]);
}
}
int main() {
int n, x, y;
// freopen("633Fin.txt", "r" , stdin);
// freopen("out1.txt", "w", stdout);
scanf("%d" , &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &val[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
}
dfs(1, 0);
solve();
printf("%lld\n", ans);
}
Codeforces 633F 树的直径/树形DP的更多相关文章
- 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分
树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...
- 2014 Super Training #9 E Destroy --树的直径+树形DP
原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...
- [10.12模拟赛] 老大 (二分/树的直径/树形dp)
[10.12模拟赛] 老大 题目描述 因为 OB 今年拿下 4 块金牌,学校赞助扩建劳模办公室为劳模办公室群,为了体现 OI 的特色,办公室群被设计成了树形(n 个点 n − 1 条边的无向连通图), ...
- HDU4514 湫湫系列故事——设计风景线 ——树的直径/树形dp+判环
中文题面,给出一个图,问能不能成环,如果可以就输出YES.否则输出该树的直径. 这里的判环我们用路径压缩的并查集就能很快的判断出来,可以在输入的同时进行判断.这题重点就是求树的直径. 树直径的性质可以 ...
- Codeforces 633F The Chocolate Spree 树形dp
The Chocolate Spree 对拍拍了半天才知道哪里写错了.. dp[ i ][ j ][ k ]表示在 i 这棵子树中有 j 条链, 是否有链延伸上来. #include<bits/ ...
- POJ 1849 Two(树的直径--树形DP)(好题)
大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- codeforces 337D Book of Evil (树形dp)
题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...
- Codeforces 543D Road Improvement(树形DP + 乘法逆元)
题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...
随机推荐
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- CSS入门基础学习二
我们下午继续学习CSS的入门基础,搬上你的小板凳赶快进入吧! 一.背景(background) Background-color:背景颜色 background-image (背景图片) backgr ...
- 20180821-Java封装
java 封装 在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节部份包装.隐藏起来的方法. 封装可以被认为是一个保护屏障,防止该类的代码和数据被外 ...
- pytest相关问题解析
1. 如果你想查询在你的环境下有哪些pytest的active plugin可以使用: py.test --traceconfig 会得到一个扩展的头文件名显示激活的插件和他们的名字.同时也会打印出当 ...
- Linux之虚拟机里的REHL7的IP
RHEL7最小化安装之后(桥接模式),我们查看本机IP, ip addr ifconfig 我们要修改配置文件 找到目录 找到文件,用vi编辑器打开修改配置文件 保存退出后,需要重启网络服务 只有我们 ...
- zabbix 接入钉钉机器人报警
import requests import json import sys import os headers = {'Content-Type': 'application/json;charse ...
- 用Delphi从内存流中判断图片格式[转]
http://blog.163.com/tfn2008%40yeah/blog/static/110321319201222243214337/ 用Delphi从内存流中判断图片格式[转] 2012- ...
- ab工具进行压力测试
简介与安装 ab:Apache Benchmark,只要我们安装了Apache,就能够在Apache的安装目录中找到它. yum | apt 安装的Apache ab的目录一般为/usr/bin 也 ...
- Hadoop: 在Azure Cluster上使用MapReduce
Azure对于学生账户有260刀的免费试用,火急火燎地创建Hadoop Cluster!本例子是使用Hadoop MapReduce来统计一本电子书中各个单词的出现个数. Let's get hand ...
- 关于狗书《Flask web开发 基于python的Web开发应用实战》中加入用户隐私功能
目前是第二次撸狗书,在用户页面这一块我个人觉得有些问题(基于交互设计).按理来说,我作为一个权限只有User的个人用户来说,肯定不喜欢让别人看到我的真实姓名,地址之类的敏感信息.所以我应该是可以设置成 ...