题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4007

https://loj.ac/problem/2111

题解

[NOI2006]网络收费,背包很显然,然后因为祖先的状态不确定对之的影响,直接枚举就可以了。

具体见 https://www.cnblogs.com/hankeke/p/bzoj1495.html。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} #define lc x << 1
#define rc x << 1 | 1 const int N = 2048 + 7;
const int INF = 0x7f7f7f7f; int n, m, mm;
int a[N], c[N], w[2][N][N], col[N], dp[N][N]; inline void dfs(int x) {
if (x >= m) {
dp[x][0] = dp[x][1] = 0;
for (int i = x >> 1; i; i >>= 1) dp[x][col[i]] += w[col[i]][x][i];
return;
}
int siz = 1 << (n - std::__lg(x));
memset(dp[x], 0, sizeof(int) * (siz + 1));
col[x] = 0, dfs(lc), dfs(rc);
for (int i = 0; i <= (siz >> 1); ++i)
for (int j = 0; j <= (siz >> 1); ++j)
smax(dp[x][i + j], dp[lc][i] + dp[rc][j]);
col[x] = 1, dfs(lc), dfs(rc);
for (int i = 0; i <= (siz >> 1); ++i)
for (int j = 0; j <= (siz >> 1); ++j)
smax(dp[x][i + j], dp[lc][i] + dp[rc][j]);
} inline void work() {
dfs(1);
int ans = 0;
for (int i = 0; i <= mm; ++i) smax(ans, dp[1][i]);
printf("%d\n", ans);
} inline void init() {
read(n), read(mm), m = 1 << --n;
for (int i = 1; i <= m; ++i) {
int x = i + m - 1;
for (int j = x >> 1; j; j >>= 1) read(w[1][x][j]);
}
for (int i = 1; i <= m; ++i) {
int x = i + m - 1;
for (int j = x >> 1; j; j >>= 1) read(w[0][x][j]);
}
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj4007 & loj2111 [JLOI2015]战争调度 复杂度分析+树上背包的更多相关文章

  1. 【BZOJ4007】[JLOI2015]战争调度(动态规划)

    [BZOJ4007][JLOI2015]战争调度(动态规划) 题面 BZOJ 洛谷 题解 神仙题,我是做不来. 一个想法是设\(f[i][j]\)表示当前考虑到\(i\)节点,其子树内有\(j\)个人 ...

  2. 【bzoj4007】[JLOI2015]战争调度 暴力+树形dp

    Description 脸哥最近来到了一个神奇的王国,王国里的公民每个公民有两个下属或者没有下属,这种 关系刚好组成一个 n 层的完全二叉树.公民 i 的下属是 2 * i 和 2 * i +1.最下 ...

  3. 【bzoj4007】[JLOI2015]战争调度 暴力+树形背包dp

    题目描述 给你一棵 $n$ 层的完全二叉树,每个节点可以染黑白两种颜色.对于每个叶子节点及其某个祖先节点,如果它们均为黑色则有一个贡献值,如果均为白色则有另一个贡献值.要求黑色的叶子节点数目不超过 $ ...

  4. bzoj1495 [NOI2006]网络收费 复杂度分析+树上背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1495 题解 通过观察可以发现,对于一个 \(lca\),如果 \(nA \leq nB\),那 ...

  5. [JLOI2015]战争调度

    [JLOI2015]战争调度 题目 解题报告 考试打了个枚举的暴力,骗了20= = $qsy$大佬的$DP$: 其实就是枚举= =,只不过枚举的比较强= = #include<iostream& ...

  6. [BZOJ4007][JLOI2015]战争调度(DP+主定理)

    第一眼DP,发现不可做,第二眼就只能$O(2^{1024})$暴搜了. 重新审视一下这个DP,f[x][i]表示在x的祖先已经全部染色之后,x的子树中共有i个参战平民的最大贡献. 设k为总结点数,对于 ...

  7. BZOJ4007 [JLOI2015]战争调度

    根本想不出来... 原来还是暴力出奇迹啊QAQ 无限ymymym中 /************************************************************** Pr ...

  8. [JLOI2015]战争调度【暴力+树形Dp】

    Online Judge:Bzoj4007,Luogu P3262 Label:暴力,树形Dp 题解 参考了这篇blog https://www.cnblogs.com/GXZlegend/p/830 ...

  9. 【题解】JLOI2015战争调度

    搜索+状压+DP. 注意到一个性质:考虑一棵以x为根的子树,在x到原树的根的路径上的点如果都已经确定了方案,那么x的左右儿子的决策就彼此独立,互不影响了.所以我们考虑状压一条路径上每一层节点的状态,求 ...

随机推荐

  1. php array_splice()函数 语法

    php array_splice()函数 语法 作用:从数组中移除选定的元素,并用新元素取代它.dd马达价格 语法:array_splice(array,start,length,array) 参数: ...

  2. BZOJ 1112: [POI2008]砖块Klo Splay + 性质分析

    Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in",& ...

  3. UE4开发PSVR游戏的常见问题

    Failed to connect to file server at xxx.xxx.xxx.xxx. RETRYING in 5s解决办法:PS4 Devkit 中 Settings->De ...

  4. 阿里云code上传代码

    1-从官网下载git,然后安装,这一步可以百度. 2-在阿里云上面创建project,如图 3-回到本地,进入本地代码文件目录,右击打开git 4-输入git init 在文件夹下面会出现.git文件 ...

  5. 【SpringBoot】 项目中运用的一些技巧,mybatis-plus 自动编译等(持续更新)

    前言 本文将总结项目中用到的一些springboot 的技巧,持续更新. Mybatis-Plus 的运用 使用原因: 主要是节省了Mapper层的编写,通过继承BaseMapper可以直接调用通用的 ...

  6. leetcode 235. 二叉搜索树的最近公共祖先(c++)

    给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x ...

  7. leetcode 118. 杨辉三角(python)

    给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...

  8. 126、TensorFlow Session的执行

    # tf.Session.run 方法是一个执行tf.Operation或者计算tf.Tensor的一个主要的机制 # 你可以传递一个或者多个tf.Operation或者tf.Tensor对象来给tf ...

  9. java sftp判断目录是否存在

    java sftp判断目录是否存在 public boolean isExistDir(String path,ChannelSftp sftp){ boolean isExist=false; tr ...

  10. 测开之路六十二:接口测试平台之公共的js、html、平台入口

    common.js //定义后台的host和端口var host = 'http://192.168.xxx.1:8000'; //'http://127.0.0.1:8000'; //用于发送htt ...