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;
    }
    
  • 相关阅读:
    Dynamics CRM 2011/2013 通过Javascript给lookup字段赋值
    shell重定向(大于号,小于号,左右,2>&1,&)
    Dynamics CRM2011 同一个FORM表单同一个字段可以摆放多次
    词的向量表示
    机器翻译领域的新突破
    Dynamics CRM2011 隐藏sub-grid 新建项和添加现有项按钮
    sed常用方法与命令
    Dynamics CRM Odata QueryUrl中的SetName问题
    hive发杂数据结构的使用,struct,array,map
    maven 经常使用命令
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/9761646.html
Copyright © 2011-2022 走看看