The Chocolate Spree

对拍拍了半天才知道哪里写错了。。

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的更多相关文章

  1. codeforces 633F The Chocolate Spree (树形dp)

    题目链接:http://codeforces.com/problemset/problem/633/F 题解:看起来很像是树形dp其实就是单纯的树上递归,就是挺难想到的. 显然要求最优解肯定是取最大的 ...

  2. CF 633 F. The Chocolate Spree 树形dp

    题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...

  3. cf633F. The Chocolate Spree(树形dp)

    题意 题目链接 \(n\)个节点的树,点有点权,找出互不相交的两条链,使得权值和最大 Sol 这辈子也不会写树形dp的 也就是有几种情况,可以讨论一下.. 下文的"最大值"指的是& ...

  4. Codeforces 633F 树的直径/树形DP

    题意:有两个小孩玩游戏,每个小孩可以选择一个起始点,并且下一个选择的点必须和自己选择的上一个点相邻,问两个选的点权和的最大值是多少? 思路:首先这个问题可以转化为求树上两不相交路径的点权和的最大值,对 ...

  5. Codeforces 633F - The Chocolate Spree(树形 dp)

    Codeforces 题目传送门 & 洛谷题目传送门 看来我这个蒟蒻现在也只配刷刷 *2600 左右的题了/dk 这里提供一个奇奇怪怪的大常数做法. 首先还是考虑分析"两条不相交路径 ...

  6. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  7. codeforces 337D Book of Evil (树形dp)

    题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...

  8. Codeforces 1276D - Tree Elimination(树形 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 繁琐的简单树形 dp(大雾),要是现场肯定弃了去做 F 题 做了我一中午,写篇题解纪念下. 提供一种不太一样的思路. 首先碰到这样的题肯定 ...

  9. Codeforces 543D Road Improvement(树形DP + 乘法逆元)

    题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...

随机推荐

  1. Linux mem/swap/buffers/cached 区别

    Free free 命令相对于top 提供了更简洁的查看系统内存使用情况: $ free total used free shared buffers cached Mem: 255268 23833 ...

  2. mysql 5.7 Group Replication

    MySQL 组复制实现了基于复制协议的多主更新(单主模式). 复制组由多个 server成员构成,并且组中的每个 server 成员可以独立地执行事务.但所有读写(RW)事务只有在冲突检测成功后才会提 ...

  3. mybatis 按in 函数参数顺序排序

    使用 FIELD()函数 SELECT *  FROM   user  WHERE id IN (72, 80, 69)  ORDER BY FIELD(id, 72, 80, 69)

  4. Confluence 6 配置服务器基础地址备注

    使用不同 URL.如果你配置了不同的基础 URL 地址或者你站点的访问者使用了不同的 URL 地址来访问你的 Confluence 地址,你有很大概率可能会受到错误信息. 修改上下文地址.如果你修改了 ...

  5. vuejs中使用echarts

    <style scoped> .content { /*自行添加样式即可*/ } #main { /*需要制定具体高度,以px为单位*/ height: 400px; } </sty ...

  6. 【java】转:Windows系统下面多个jdk版本切换

    转自:https://blog.csdn.net/iamcaochong/article/details/56008545 1.系统-高级系统设置-环境变量 里面的Path值最前面的C:\Progra ...

  7. 新建maven项目错误处理

    1.如果新建maven 项目确省maven dependencies,则在存在问题工程的.classpath末尾 加上 <classpathentry kind="con"  ...

  8. CentOS 7 部署 Spring Boot

    Spring Boot 内嵌了tomcat .我们可以将Boot打成 jar 包丢到服务器上运行才行. Spring Boot已经帮我们打理好了这一切,如果项目是继承自 spring-boot-sta ...

  9. MySQL----数据库练习

    一.多对多的正反向查询 class Class(models.Model): name = models.CharField(max_length=32,verbose_name="班级名& ...

  10. Python字符串编码转换

    使用encode()方法编码 str.encode([encoding="utf-8"][,errors="strict"]) str:表示需要转换的字符串 e ...