poj 2486 a apple tree

题意:n节点的树,从1开始走,总共v步,每个点都有一个价值,求可以获得的最大价值

分析:这个显然可以走回来,那么就加一维表示是否走回祖先
dp[u][i][j]表示从u为根节点的子树,往下走i步,j=0表示不走回来,j=1表示走回来
那么可以得到状态转移方程,不走回来的可能会影响走回来的,如果先算不会来的,那么一个节点在不走回来算一次
在走回来又算一次,所以先算走回来的就可以避免
dp[u][i][0]=max(dp[u][i][0],dp[u][i-j][1]+dp[v][j-1][0]);
dp[u][i][0]=max(dp[u][i][0],dp[u][i-j][0]+dp[v][j-2][1]);
dp[u][i][1]=max(dp[u][i][1],dp[u][i-j][1]+dp[v][j-2][1]);
#include
#include
#include
#include
#include
using namespace std;
const int maxn=505;
vector g[maxn];
int val[maxn],n,k,dp[maxn][205][2];

void dfs(int u,int f){

for(int i=0;i0;i--)
for(int j=1;j>n>>k){
memset(dp,0,sizeof(dp));
for(int i=1;i>val[i];g[i].clear();
for(int j=0;j>u>>v;
g[u].push_back(v);g[v].push_back(u);
}
dfs(1,-1);
coutpoj 2486 a apple tree

时间: 2024-10-29 10:48:16

poj 2486 a apple tree的相关文章

【POJ 2486】 Apple Tree(树型dp)

[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Accepted: 2990 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each

【POJ 2486】 Apple Tree (树形DP)

Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apple

【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)

[POJ 3321] Apple Tree (dfs重标号设区间+树状数组求和) Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21966   Accepted: 6654 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. K

POJ 题目3321 Apple Tree(线段树)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21566   Accepted: 6548 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

POJ 3321:Apple Tree(dfs序+树状数组)

题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1. 分析: 对树进行dfs,将树变为序列,记录每个点进栈出栈的时间戳作为其对应区间的左右端点,那么其子节点对应区间必在该区间内,对这段区间求和即可,用树状数组进行修改与求和. 代码: program apple; type point=^node; node=record x:longint; next:point; end; var a:arra

[Poj 2486] Apple Tree 树形DP

Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9069 Accepted: 3016 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of

POJ 2486 Apple Tree (树形dp 经典题)

Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7784   Accepted: 2603 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amoun

Apple Tree POJ - 2486

Apple Tree POJ - 2486 题目大意:一棵点带权有根树,根节点为1.从根节点出发,走k步,求能收集的最大权值和. 树形dp.复杂度可能是O(玄学),不会超过$O(nk^2)$.(反正这题不卡这个,考思想)参考 ans[i][j][0]表示i点以下共走j步,不回来,可能收集到最大的权值ans[i][j][1]表示i点以下共走j步,回来,可能收集到最大的权值 比较复杂的是,每个节点(以下称当前节点)从其子节点转移的时候,需要用一个背包: t[i][j][0]表示当前节点的前i个子节点

POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

POJ - 3321 Apple Tree Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very muc