• 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;
    }
    
  • 相关阅读:
    代码手动修改约束(AutoLayout)
    iOS开发中手机号码和价格金额有效性判断及特殊字符的限制
    Mac下如何显示隐藏文件/文件夹
    Exp8 Web综合 20181308
    20181308 邵壮 Exp7 网络欺诈防范
    20181308 邵壮 Exp6 MSF应用基础
    重现Vim任意代码执行漏洞(CVE-2019-12735) 20181308
    密码引擎-加密API研究 20181308邵壮
    密码引擎-加密API实现与测试 20181308邵壮
    Exp5 信息搜集与漏洞扫描 20181308邵壮
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/9761646.html
Copyright © 2020-2023  润新知