2014taichuan D题
2015-10-14 14:41
537 查看
题意:给定一颗有N个节点的树,一开始每个节点都是白色的,现在要对每个节点进行决策,使得每个白色节点连接着恰好K个黑色节点,如果达不到则这个白色节点变为黑色
分析:树形dp
g[u] 表示以u为根节点的子树且u节点为黑色时的最大白点数
f[u][i] 表示以u为根节点时的子树且u为白色节点时连接i个黑色节点的最大白点数,这里的转移是一个树上的背包
分析:树形dp
g[u] 表示以u为根节点的子树且u节点为黑色时的最大白点数
f[u][i] 表示以u为根节点时的子树且u为白色节点时连接i个黑色节点的最大白点数,这里的转移是一个树上的背包
#include const int N = 100 + 5; int n, m; char buffer[1000 + 5]; int g , f [10 + 5]; std::vector edges ; void dfs(int u) { g[u] = 0; f[u][0] = 1; for (int i = 1; i <= m; i ++) { f[u][i] = -1; } for (int i = 0; i < (int)edges[u].size(); i ++) { int v = edges[u][i]; dfs(v); g[u] += std::max(g[v], f[v][m - 1]); for (int j = m; j >= 0; j --) { f[u][j] = f[u][j] + f[v][m]; if (j) { f[u][j] = std::max(f[u][j], f[u][j - 1] + g[v]); } } } } int work() { if (m == 0) { return n; } dfs(0); return std::max(g[0], f[0][m]); } int main() { int t; scanf("%d", &t); while (t --) { scanf("%d%d", &n, &m); getchar(); for (int i = 0; i < n; i ++) { edges[i].clear(); gets(buffer); std::istringstream is(buffer); int x; while (is >> x && x) { edges[i].push_back(x - 1); } } printf("%d\n", work()); } return 0; }
相关文章推荐
- iredmail下安装脚本分析(一)---get_all.sh 文件所在目录为PKGS
- OCIServerAttach failed ORA-12154: TNS:could not resolve the connect identifier specified
- cannot set user id: Resource temporarily unavailable
- LR Error: Failed to deliver a p2p message from son to parent process, reason - communication error.
- LR errorFunction two_way_comm_post_message / two_way_comm_post_message_ex failed.错误
- String painter(DP综合题:区间DP(两次DP))
- http://blog.csdn.net/defonds/article/details/46042809
- Saiku3.6构建过程
- POJ 1273 Drainage Ditches 最大流
- 2015/10/14 10:02:08 play.main(:570) ERROR Cannot connected to the database : Communications link fa
- http://blog.csdn.net/liuhongwei123888/article/details/6549825
- child process failed, exited with error number 100
- NSString和block的属性时copy,而不用retain 而容器类使用retain
- 今天在我导入一些安卓项目的源码的时候会出现style.xml 和MAIN.xml 等错误, 网上的方法试了各种clean各种fix等方法都是不能解决。最后去国外论坛得到解决
- Kafka错误3_kafka中 FailedToSendMessageException解决_集群启动问题
- Monitor vs WaitHandle
- AIDL通信原理
- LeetCode -- Contains Duplicate
- iredmail下安装脚本分析(一)---get_all.sh 文件所在目录为PKGS
- Windows下Hadoop报错Failed to locate the winutils