您的位置:首页 > 其它

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这个点的种类。

其他的看代码就好懂了。

好坑的题啊……都是泪……

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: