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;
    }
    
  • 相关阅读:
    (转)C# 泛型委托 Action<>和Func<>
    (转)ASP.NET Core 中间件与筛选器
    react中将svg做成icon组件在其他模块调用
    react简单的tab切换 (styled-components)
    计算购物车金额总和( jquery )
    js分享功能(微信,QQ,微博,空间,豆瓣等)
    AJAX入门介绍
    作为前端,你不得不知道的搜索引擎优化
    微信小程序实现图片双滑缩放大小
    swiper实现一个好看的轮播图
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/9761646.html
Copyright © 2011-2022 走看看