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. 转载:UML学习(二)-----类图(silent)

    原文:http://www.cnblogs.com/huiy/p/8552607.html 1.什么是类图 类图(Class diagram)主要用于描述系统的结构化设计.类图也是最常用的UML图,用 ...

  2. EF数据迁移

    在项目中使用Entity Framework的Code First模式,进行数据迁移时,Migration文件夹中存放的是每一次Entity的修改如何同步到数据的操作方法,每个文件中都只有Up和Dow ...

  3. C++ sizeof()练习

    class A { int a; short b; int c; char d; }; class B { double a; short b; int c; char d; }; 在32位机器上用g ...

  4. SpringCloud的版本

    Spring Cloud 项目目前仍然是快速迭代期,版本变化很快.这里整理一下版本相关的东西,备忘一下. 大版本 版本号规则 Spring Cloud并没有熟悉的数字版本号,而是对应一个开发代号. C ...

  5. maven项目部署到Tomcat

    1.安装Tomcat,地址:http://tomcat.apache.org,我安装的版本为9.0.0.M22,安装的目录为C:\apache-tomcat,设置环境变量CATALINA_HOME为C ...

  6. Confluence 6 使用主题

    主题是被用来修改 Confluence 站点或空间的外观的. Confluence 安装了一个单一的默认主题,或者你也可以下载和安装其他的主题.你可以从 The Atlassian Marketpla ...

  7. error: js/dist/app.js from UglifyJs Unexpected token: name (Dom7)

    What you did I have installed Swiper as normal dependency in my Project and import it to my scripts ...

  8. bzoj3276磁力 两种要求下的最大值:分块or线段树+拓扑

    进阶指南上的做法是分块的.. 但是线段树搞起来也挺快,将磁石按照距离排序,建立线段树,结点维护区间质量最小值的下标 进行拓扑,每次在可行的范围内在线段树中找到质量最小的下标取出,取出后再将线段树对应的 ...

  9. hdu1198 普通的并查集

    今天开始(第三轮)并查集,,之前学的忘了一些 本题很简单直接上代码 #include<iostream> #include<cstring> #include<cstdi ...

  10. 在centos6.8上源码安装MySQL

    1.安装环境:软件包:mysql-5.6.31.tar.gz 需求相关选项: 安装基目录basedir:/mydb/mysql31数据存放目录datadir:/mydb/mysql31/data端口号 ...