Codeforces 633F The Chocolate Spree 树形dp
对拍拍了半天才知道哪里写错了。。
dp[ i ][ j ][ k ]表示在 i 这棵子树中有 j 条链, 是否有链延伸上来。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ;
const double eps = 1e-;
const double PI = acos(-); int n, a[N];
LL dp[N][][]; vector<int> G[N]; inline bool chkmax(LL &a, LL b) {
return a < b ? a = b, true : false;
} void dfs(int u, int fa) {
LL tmp[][];
LL gg[][];
dp[u][][] = ;
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
tmp[i][j] = -INF;
tmp[][] = ;
for(auto& v : G[u]) {
if(v == fa) continue;
dfs(v, u);
memcpy(gg, tmp, sizeof(gg));
for(int i = ; i <= ; i++) {
for(int j = ; j <= ; j++) {
for(int x = ; x <= ; x++) {
for(int y = ; y <= ; y++) {
if(!i && j || !x && y) continue;
if(j + y > ) continue;
tmp[i + x][j + y] = max(tmp[i + x][j + y], gg[i][j] + dp[v][x][y]);
}
}
}
}
}
// dp[1][0]
chkmax(dp[u][][], tmp[][] + a[u]);
chkmax(dp[u][][], tmp[][]);
chkmax(dp[u][][], tmp[][] + a[u]);
chkmax(dp[u][][], tmp[][] + a[u]); // dp[1][1]
chkmax(dp[u][][], tmp[][] + a[u]);
chkmax(dp[u][][], tmp[][] + a[u]); //dp[2][0]
chkmax(dp[u][][], tmp[][]);
chkmax(dp[u][][], tmp[][] + a[u]);
chkmax(dp[u][][], tmp[][] + a[u]); //dp[2][1]
chkmax(dp[u][][], tmp[][] + a[u]);
chkmax(dp[u][][], tmp[][] + a[u]);
chkmax(dp[u][][], tmp[][] + a[u]);
} int main() {
// freopen("test.in", "r", stdin);
scanf("%d", &n);
for(int i = ; i <= n; i++) G[i].clear();
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for(int i = ; i <= n; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; k++)
dp[i][j][k] = -INF;
dfs(, );
LL ans = ;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
ans = max(ans, dp[][i][j]);
printf("%lld\n", ans);
return ;
} /*
*/
Codeforces 633F The Chocolate Spree 树形dp的更多相关文章
- codeforces 633F The Chocolate Spree (树形dp)
题目链接:http://codeforces.com/problemset/problem/633/F 题解:看起来很像是树形dp其实就是单纯的树上递归,就是挺难想到的. 显然要求最优解肯定是取最大的 ...
- CF 633 F. The Chocolate Spree 树形dp
题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...
- cf633F. The Chocolate Spree(树形dp)
题意 题目链接 \(n\)个节点的树,点有点权,找出互不相交的两条链,使得权值和最大 Sol 这辈子也不会写树形dp的 也就是有几种情况,可以讨论一下.. 下文的"最大值"指的是& ...
- Codeforces 633F 树的直径/树形DP
题意:有两个小孩玩游戏,每个小孩可以选择一个起始点,并且下一个选择的点必须和自己选择的上一个点相邻,问两个选的点权和的最大值是多少? 思路:首先这个问题可以转化为求树上两不相交路径的点权和的最大值,对 ...
- Codeforces 633F - The Chocolate Spree(树形 dp)
Codeforces 题目传送门 & 洛谷题目传送门 看来我这个蒟蒻现在也只配刷刷 *2600 左右的题了/dk 这里提供一个奇奇怪怪的大常数做法. 首先还是考虑分析"两条不相交路径 ...
- 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 1276D - Tree Elimination(树形 dp)
Codeforces 题面传送门 & 洛谷题面传送门 繁琐的简单树形 dp(大雾),要是现场肯定弃了去做 F 题 做了我一中午,写篇题解纪念下. 提供一种不太一样的思路. 首先碰到这样的题肯定 ...
- Codeforces 543D Road Improvement(树形DP + 乘法逆元)
题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...
随机推荐
- PHP导出MySQL数据字典 Summer-Mysql-Dic
2017年11月9日09:30:29 用 PHP 写的一个类文件, 用来导出MySQL数据字典 导出表信息; 字段信息, 索引信息 可以导出浏览器适用的样式, 也可以导出word文档 建议上线前用这个 ...
- Java并发编程的4个同步辅助类(CountDownLatch、CyclicBarrier、Semaphore、Phaser)
我在<JDK1.5引入的concurrent包>中,曾经介绍过CountDownLatch.CyclicBarrier两个类,还给出了CountDownLatch的演示案例.这里再系统总结 ...
- python-GIL、死锁递归锁及线程补充
一.GIL介绍 GIL全称 Global Interpreter Lock ,中文解释为全局解释器锁.它并不是Python的特性,而是在实现python的主流Cpython解释器时所引入的一个概念,G ...
- python-时间模块,random、os、sys、shutil、json和pickle模块
一.time与datetime模块 time模块: 时间戳:表示的是从1970年1月1日00:00:00开始按秒计算的偏移量,返回类型为float类型 格式化时间字符串(Format String) ...
- LuoGu P2420 让我们异或吧
其实......这就是个SB题,本来看到这个题,和树上路径有关 于是--我就欣喜地打了一个树剖上去,结果嘞,异或两遍等于没异或 所以这题和LCA屁关系都没有,所以这题就是个树上DFS!!!! 所以它为 ...
- Git- 命令及使用
关于Git相关介绍这里就不介绍了,可转<Git- 简介>或者查看官网信息.这篇整理一下git相关的命令. 1) 远程仓库相关命令 克隆下载仓库:$ git clone git://gi ...
- json字符串和字典的区别补充
json字符串和字典的区别:json:(JavaScript Object Notation)的首字母缩写,字面的意思是(javascript对象表示法),这里说的json指的是类似于javascri ...
- 如何在cmd中执行python文件
打开cmd终端 输入python 然后再输入要执行文件的路径 就可以把python文件运行起来 ...
- day11 函数的位置形参,位置实参,可变长位置形参,关键字形参
今天内容 函数的参数详解 形参与实参 形参及形式参数,就是在定义函数是括号中指定的参数(本质就是一个名字) 实参及实际参数,指的是在调用函数是传入的参数)(本质就是一个值) 在调用函数是就会把形参和实 ...
- BeautifulSoup的基本操作
>>> from bs4 import BeautifulSoup #导入 >>> soup = BeautifulSoup(url.content," ...