zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    dubbo+zookeeper报错:com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method
    CoreException: Could not get the value for parameter compilerId for plugin execution default-compile: PluginResolutionException: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1
    Spring容器加载配置文件
    Java基础(一)字符串之如何删除指定的字符
    Java基础(二)数组之如何排序数组并插入元素
    Java基础(一)字符串之如何比较字符串
    Spring的事务管理
    Java使用split()截取字符串
    JavaWeb过滤器实现页面跳转至登录页面
    jQuery获取session中的值
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/9761646.html
Copyright © 2011-2022 走看看