CF633F The Chocolate Spree

Description

Alice and Bob have a tree (undirected acyclic connected graph). There are \(a_{i}\) chocolates waiting to be picked up in the \(i-th\) vertex of the tree. First, they choose two different vertices as their starting positions (Alice chooses first) and take all the chocolates contained in them.

Then, they alternate their moves, selecting one vertex at a time and collecting all chocolates from this node. To make things more interesting, they decided that one can select a vertex only if he/she selected a vertex adjacent to that one at his/her previous turn and this vertex has not been already chosen by any of them during other move.

If at any moment one of them is not able to select the node that satisfy all the rules, he/she will skip his turns and let the other person pick chocolates as long as he/she can. This goes on until both of them cannot pick chocolates any further.

Due to their greed for chocolates, they want to collect as many chocolates as possible. However, as they are friends they only care about the total number of chocolates they obtain together. What is the maximum total number of chocolates they may pick?

Solution

其实这个题有比较套路的做法, 参考OO0OO0...或者tourist的提交
但是有一个童鞋提供了一个比较正常的做法godspeedkaka's blog.

Code

// my id of codeforces is XXXXXXXXX
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
const int N = 100005;

std:: vector<int> e[N];
int val[N];

long long Res[N], AChain[N], ACFNAAC[N], LCFNTL[N];

int GetAnswer(int u, int fa) {
    Res[u] = AChain[u] = ACFNAAC[u] = LCFNTL[u] = val[u];
    long long LongestChain = 0;
    for (auto v : e[u]) {
        if (v == fa) continue;
        GetAnswer(v, u);
        Res[u] = std:: max(Res[u], Res[v]);
        Res[u] = std:: max(Res[u], AChain[u] + AChain[v]);
        Res[u] = std:: max(Res[u], ACFNAAC[u] + LCFNTL[v]);
        Res[u] = std:: max(Res[u], ACFNAAC[v] + LCFNTL[u]);

        AChain[u] = std:: max(AChain[u], AChain[v]);
        AChain[u] = std:: max(AChain[u], LCFNTL[u] + LCFNTL[v]);

        ACFNAAC[u] = std:: max(ACFNAAC[u], val[u] + ACFNAAC[v]);
        ACFNAAC[u] = std:: max(ACFNAAC[u], LCFNTL[u] + AChain[v]);
        ACFNAAC[u] = std:: max(ACFNAAC[u], LCFNTL[v] + val[u] + LongestChain);

        LongestChain = std:: max(LongestChain, AChain[v]);
        LCFNTL[u] = std:: max(LCFNTL[u], LCFNTL[v] + val[u]);
    }
}

int main () {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i += 1)
        scanf("%d", &val[i]);
    for (int i = 1; i < n; i += 1) {
        int u, v;
        scanf("%d%d", &u, &v);
        e[u].push_back(v), e[v].push_back(u);
    }
    GetAnswer(1, 0);
    printf("%I64d", Res[1]);
    return 0;
}

转载于:https://www.cnblogs.com/qdscwyy/p/9761646.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值