zoukankan      html  css  js  c++  java
  • Codeforces Round #384 (Div. 2) D. Chloe and pleasant prizes

    D. Chloe and pleasant prizes
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.

    They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai — pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.

    The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.

    Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.

    Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of gifts.

    The next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the pleasantness of the gifts.

    The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.

    It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.

    Output

    If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer — the maximum possible sum of pleasantness they can get together.

    Otherwise print Impossible.

    Examples
    Input
    8
    0 5 -1 4 3 2 6 5
    1 2
    2 4
    2 5
    1 3
    3 6
    6 7
    6 8
    Output
    25
    Input
    4
    1 -5 1 1
    1 2
    1 4
    2 3
    Output
    2
    Input
    1
    -1
    Output
    Impossible
    /*
    树形DP
    */
    #include<bits/stdc++.h>
    #define INF 0x3f3f3f3f3f3f
    #define N 200005
    #define ll long long
    using namespace std;
    int val[N];
    ll dp[N];
    ll f[N];
    vector<int>edge[N];
    int n;
    int u,v;
    ll cur=-INF;
    void dfs(int u,int fa){
    
        //cout<<dp[u]<<endl;
        f[u]=val[u];
        dp[u]=-INF;
        ll temp=-INF;
        for(int i=0;i<edge[u].size();i++){
            int v=edge[u][i];
            //cout<<v<<endl;
            if(v==fa) continue;
            dfs(v,u);
            cur=max(cur,temp+dp[v]);
            //cout<<temp<<" "<<dp[v]<<endl;
            f[u]+=f[v];//将从这个点往下的结点权值加起来
            dp[u]=max(dp[u],dp[v]);
            temp=max(temp,dp[v]);
            //cout<<dp[u]<<endl;
        }
        dp[u]=max(dp[u],f[u]);
    }
    int main(){
        //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&val[i]);
            //cout<<val[i]<<" ";
            edge[i].clear();
        }
        //cout<<endl;
        for(int i=0;i<n-1;i++){
            scanf("%d%d",&u,&v);
            edge[u].push_back(v);
            edge[v].push_back(u);
        }//建树
        int ok=(edge[1].size()>1);//根节点必须有两个分支才能够给两个人分的
        for(int i=2;i<=n;i++){//只要有一个分差了,就能分成两个(这地方wa了无数发)
            if(edge[i].size()>2) ok=1;
        }
        cur=-INF;
        dfs(1,-1);
        if(cur==-INF||!ok) puts("Impossible");
        else printf("%lld
    ",cur);
        return 0;
    }
    /*
    2
    1
    4
    2
    5
    2
    3
    1
    6
    3
    7
    6
    8
    6
    25
    */
  • 相关阅读:
    新入行程序员考虑自己是否明白以下这8件事情
    程序猿品种总结你是哪一类?
    利用趣味漫画讲述云计算起源
    设计师遇到瓶颈时的五个解困惑建议
    可以帮助到我今后的程序员生涯的十分钟思考
    oc编程中继承的相关实例
    centos和ubuntu服务器Bash漏洞修复方法
    jQuery经典面试题精选
    phpstudy -sql服务器2008r2 -中控考勤机 -php 组合查询考勤
    bat 批处理测试局域网速度 两端电脑
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6186557.html
Copyright © 2011-2022 走看看