题目:

Nowadays, there are more and more challenge game on TV such as 'Girls, Rush Ahead'. Now, you participate int a game like this. There are N rooms. The connection of rooms is like a tree. In other words, you can go to any other room by one and only one way. There is a gift prepared for you in Every room, and if you go the room, you can get this gift. However, there is also a trap in some rooms. After you get the gift, you may be trapped. After you go out a room, you can not go back to it any more. You can choose to start at any room ,and when you have no room to go or have been trapped for C times, game overs. Now you would like to know what is the maximum total value of gifts you can get.

Input  

The first line contains an integer T, indicating the number of testcases. 
For each testcase, the first line contains one integer N(2 <= N <= 50000), the number rooms, and another integer C(1 <= C <= 3), the number of chances to be trapped. Each of the next N lines contains two integers, which are the value of gift in the room and whether have trap in this rooom. Rooms are numbered from 0 to N-1. Each of the next N-1 lines contains two integer A and B(0 <= A,B <= N-1), representing that room A and room B is connected. 
All gifts' value are bigger than 0.

Output  

For each testcase, output the maximum total value of gifts you can get.

Sample Input

2
3 1
23 0
12 0
123 1
0 2
2 1
3 2
23 0
12 0
123 1
0 2
2 1

Sample Output

146
158

题解

大意:给定一颗树··树上节点有不同价值的礼物···有些节点有陷阱··现可以选择从任意一个节点出发按照任意路线走···但不能返回··规定碰到一定数量的陷阱时就会停止走动··问最多可以拿到多少价值总和的礼物?

写了2个多小时发现有处细节错了··就是一旦碰到规定的陷阱数C就必须停止··然而我的dp并没有注意到这一点··导致整个代码方程直接错了···

参照了网上的方法:http://blog.csdn.net/martinue/article/details/51025232

另外这道题还发现了我的一个误区:求一颗树中的最长链的相关问题··如果我们要确定每个节点上的最长链··也就是说如果问到一个节点我们就能答出它的最长链的话,我们需要按照上一篇文章Bob‘s race那样dp··但遇到类似于这道题··如果只是询问最长链而并不需要知道节点与最长链的对应关系时··我们可以按照这道题的方式进行Dp,详见代码·差别很明显

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
using namespace std;
const int N=;
const int inf=0x3f3f3f3f;
int first[N],nxt[N*],go[N*],tot,f[N][][],val[N],n,T,C,ans;
bool jud[N];
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
return f;
}
inline void comb(int a,int b)
{
nxt[++tot]=first[a],first[a]=tot,go[tot]=b;
nxt[++tot]=first[b],first[b]=tot,go[tot]=a;
}
inline void pre()
{
tot=ans=;
memset(first,,sizeof(first));memset(f,-inf,sizeof(f));
}
inline void dfs(int u,int fa)
{
f[u][jud[u]][jud[u]]=val[u];ans=max(ans,val[u]);
for(int e=first[u];e;e=nxt[e])
{
int v=go[e];if(v==fa) continue;
dfs(v,u);
for(int i=;i<=C;i++)
for(int j=;j+i<=C;j++)
{
if(j+i<=C) ans=max(ans,f[u][i][]+f[v][j][]);
if(j+i<C) ans=max(ans,f[u][i][]+f[v][j][]);
if(j!=C) ans=max(ans,f[u][i][]+f[v][j][]);
if(i!=C) ans=max(ans,f[u][i][]+f[v][j][]);
}
for(int i=;i<C;i++)
{
f[u][i+jud[u]][]=max(f[u][i+jud[u]][],f[v][i][]+val[u]);
f[u][i+jud[u]][]=max(f[u][i+jud[u]][],f[v][i][]+val[u]);
}
if(!jud[u]) f[u][C][]=max(f[u][C][],f[v][C][]+val[u]);
}
}
int main()
{
// freopen("a.in","r",stdin);
T=R();int a,b;
while(T--)
{
n=R(),C=R();pre();
for(int i=;i<=n;i++) val[i]=R(),jud[i]=R();
for(int i=;i<n;i++) a=R(),b=R(),comb(a+,b+);
dfs(,);cout<<ans<<endl;
}
return ;
}

刷题总结——game(hdu4616)的更多相关文章

  1. LeetCode刷题系列

    LeetCode 我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的.专业,覆盖语种全面. 提前说说刷题的心得: 尽量手写代码,少使用IDE的 ...

  2. ife任务刷题总结(一)-css reset与清除浮动

    本文同时发布于本人的个人网站www.yaoxiaowen.com 百度创办的前端技术学院,是一个面向大学生的前端技术学习平台.虽然只有大学生才有资格报名,提交代码进行比赛排名.但是这并不妨碍我们这些初 ...

  3. 刷题ING...

    我用codeVS刷题.. 努力准备!!

  4. XidianOJ 1020 ACMer去刷题吧

    题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...

  5. 【BZOJ-4590】自动刷题机 二分 + 判定

    4590: [Shoi2015]自动刷题机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 63[Submit][Status ...

  6. NOI题库分治算法刷题记录

    今天晚自习机房刷题,有一道题最终WA掉两组,极其不爽,晚上回家补完作业欣然搞定它,特意来写篇博文来记录下 (最想吐槽的是这个叫做分治的分类,里面的题目真的需要分治吗...) 先来说下分治法 分治法的设 ...

  7. NOI题库刷题日志 (贪心篇题解)

    这段时间在NOI题库上刷了刷题,来写点心得和题解 一.寻找平面上的极大点 2704:寻找平面上的极大点 总时间限制:  1000ms  内存限制:  65536kB 描述 在一个平面上,如果有两个点( ...

  8. 用js刷题的一些坑

    leecode可以用js刷题了,我大js越来越被认可了是吧.但是刷题中会因为忽略js的一些特性掉入坑里.我这里总结一下我掉过的坑. 坑1:js中数组对象是引用对象 js中除了object还有数组对象也 ...

  9. BZOJ4590 自动刷题机

    Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...

  10. PE刷题记录

    PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...

随机推荐

  1. noip模拟赛#45

    T1:n<=1e6,求最小的区间包含(1,m)的所有数. =>双指针扫一遍即可 #include<cstdio> #include<cstring> #includ ...

  2. 第010课_掌握ARM芯片时钟体系

    from:第010课_掌握ARM芯片时钟体系 第001节_S3C2440时钟体系结构 S3C2440是System On Chip(SOC),在芯片省不仅仅有CPU,还有一堆外设. 至于有哪些外设,可 ...

  3. 2018.5.5 phpStorm破解2017.3版本方法

    方法一 注册时,在打开的License Activation窗口中选择"License server",在输入框输入下面的网址: http://im.js.cn:8888 (新) ...

  4. ValidForm验证表单

    在做项目时,要求熟悉项目中验证表单的插件,所以学习一下validForm这个插件 http://validform.rjboy.cn/document.html#validformObject

  5. iOS 解决ipv6问题

    解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...

  6. 关于removeFromSuperview

    关于  - (void)removeFromSuperview 苹果官网API中是这么描述的: Unlinks the view from its superview and its window, ...

  7. Roman Numeral Converter-freecodecamp算法题目

    Roman Numeral Converter 1.要求 将给定的数字转换成罗马数字 所有返回的罗马数字都应该是大写形式 2.思路 分别定义个位.十位.百位.千位的对应罗马数字的数组 用Math.fl ...

  8. 【jquery】 form ajaxSubmit 问题

    常见问题 这个插件跟哪些版本的jQuery兼容? 这个插件需要jQuery v1.0.3 或 以后的版本. 这个插件需要其它插件的支持吗? 不需要. 这个插件的运行效率高吗? 是的!请到 对比页面 查 ...

  9. Ubuntu添加环境变量

    在 Ubuntu 系统中有两种设置环境变量 PATH 的方法.第一种适用于为单一用户设置 PATH,第二种是为全局设置 PATH. 第一种方法: 在用户主目录下有一个 .bashrc 文件,可以在此文 ...

  10. 《鸟哥的Linux私房菜》学习笔记(2)——Bash特性

    一.shell的基本概念:                               shell 意思是外壳,它是离用户最近的程序.shell提供用户操作系统的接口,我们通过shell将输入的命令与 ...