ZOJ3734 LIKE vs CANDLE
2013-11-24 21:41
316 查看
这题……
把今天在完全搞清楚题意的情况和现场被这题的题意卡的后半场不能自理的情况比较了一下,还是觉得自己的经验太少了……
一棵点带权有根树,权可正可负,每次可以以X的代价让某个子树翻转。
但在之前已经有些子树被翻转过了,再翻回来需要Y的代价而不是X。
求怎么操作能让总权值最大?
状态很简单就是f[x][0/1]代表x这个节点有没有被翻过(0或1)的最大价值。
很明显f[x][i]与他的儿子的状态f[y][j]之间有额外代价仅当i!=j,代价是X还是Y取决于y这个点的种类。
其他的看代码就好懂了。
好坑的题啊……都是泪……
把今天在完全搞清楚题意的情况和现场被这题的题意卡的后半场不能自理的情况比较了一下,还是觉得自己的经验太少了……
一棵点带权有根树,权可正可负,每次可以以X的代价让某个子树翻转。
但在之前已经有些子树被翻转过了,再翻回来需要Y的代价而不是X。
求怎么操作能让总权值最大?
状态很简单就是f[x][0/1]代表x这个节点有没有被翻过(0或1)的最大价值。
很明显f[x][i]与他的儿子的状态f[y][j]之间有额外代价仅当i!=j,代价是X还是Y取决于y这个点的种类。
其他的看代码就好懂了。
好坑的题啊……都是泪……
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 50005; int n , pre , mcnt; int val , filp , state; int X , Y , f [2]; struct edge { int x , next; }e ; void dfs(int x) { if (filp[x]) state ^= 1; if (state) val[x] = -val[x]; f[x][0] = val[x] , f[x][1] = -val[x]; for (int i = pre[x] ; ~i ; i = e[i].next) { int y = e[i].x; dfs(y); f[x][0] += max(f[y][0] , f[y][1] - (filp[y] ? Y : X)); f[x][1] += max(f[y][1] , f[y][0] - (filp[y] ? Y : X)); } if (filp[x]) state ^= 1; } void work() { int i , x , y; memset(pre , -1 , sizeof(pre)) , mcnt = 0; for (i = 1 ; i <= n ; ++ i) { scanf("%d%d%d%d",&val[i] , &x , &filp[i] , &y); if (y) val[i] = -val[i]; e[mcnt] = (edge){i , pre[x]} , pre[x] = mcnt ++; } dfs(0); if (f[0][0] < 0) puts("HAHAHAOMG"); else printf("%d\n" , f[0][0]); } int main() { while (~scanf("%d%d%d",&n,&X,&Y)) work(); return 0; }
相关文章推荐
- HDU 4799 LIKE vs CANDLE ACM/ICPC 2013 Changsha(树形dp)
- HDU 4799 LIKE vs CANDLE 树形dp
- zoj-3734-LIKE vs CANDLE
- ZOJ 3734 LIKE vs CANDLE _树形DP
- [HDOJ 4799][BNUOJ 34021] LIKE vs CANDLE [树形DP]
- zoj 3734 LIKE vs CANDLE
- ZOJ 3734: LIKE vs CANDLE
- zoj-3734 LIKE vs CANDLE 【树形dp】
- 树形DP(2)-Hdu 4799 LIKE vs CANDLE
- 2013 长沙Regional I题:LIKE vs CANDLE (树dp)
- hdu 4799 LIKE vs CANDLE
- HDU 4799 LIKE vs CANDLE 树形dp
- HDU4799 LIKE vs CANDLE(树形dp)
- hdu 4799 LIKE vs CANDLE(树形dp)
- HDU 4799 LIKE vs CANDLE 【树形dp】【阅读题】【水题】
- Mysql函数INSTR、LOCATE、POSITION VS LIKE
- Is-a vs. is-like-a relationships("是一个"与"像是一个")关系
- like vs substr?
- Mysql函数INSTR、LOCATE、POSITION VS LIKE
- charindex VS like in stored procedure on to improve the performance