Chloe and pleasant prizes
2 seconds
256 megabytes
standard input
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 integerai — 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.
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.
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.
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
25
4
1 -5 1 1
1 2
1 4
2 3
2
1
-1
Impossible
分析:题意是找两个不存在包含关系的权值的最大和;
只需dfs找出最大值和次大值即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define intxt freopen("in.txt","r",stdin)
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
const int dis[][]={,,,,-,,,-};
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t;
ll ans,ma,p[maxn];
vi e[maxn];
void dfs(int now,int pre)
{
for(int x:e[now])
{
if(x==pre)continue;
dfs(x,now);
p[now]+=p[x];
}
}
void dfs1(int now,int pre)
{
for(int x:e[now])
{
if(x==pre)continue;
if(ma!=-(1LL<<))ans=max(ans,ma+p[x]);
dfs1(x,now);
}
ma=max(ma,p[now]);
}
int main()
{
int i,j;
ans=ma=-(1LL<<);
scanf("%d",&n);
rep(i,,n)p[i]=read();
rep(i,,n-)scanf("%d%d",&j,&k),e[j].pb(k),e[k].pb(j);
dfs(,);
dfs1(,);
if(ans!=-(1LL<<))printf("%lld\n",ans);
else puts("Impossible");
//system("Pause");
return ;
}
Chloe and pleasant prizes的更多相关文章
- coderforces #384 D Chloe and pleasant prizes(DP)
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp
D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...
- Codeforces 743D Chloe and pleasant prizes(树型DP)
D. Chloe and pleasant prizes ...
- CodeForces - 743D Chloe and pleasant prizes
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- D. Chloe and pleasant prizes
D. Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 【27.85%】【codeforces 743D】Chloe and pleasant prizes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Codeforces743D][luogu CF743D]Chloe and pleasant prizes[树状DP入门][毒瘤数据]
这个题的数据真的很毒瘤,身为一个交了8遍的蒟蒻的呐喊(嘤嘤嘤) 个人认为作为一个树状DP的入门题十分合适,同时建议做完这个题之后再去做一下这个题 选课 同时在这里挂一个选取节点型树形DP的状态转移方程 ...
- Codeforces 743D:Chloe and pleasant prizes(树形DP)
http://codeforces.com/problemset/problem/743/D 题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible. 思路:之前好 ...
- D. Chloe and pleasant prizes 树上dp + dfs
http://codeforces.com/contest/743/problem/D 如果我们知道mx[1]表示以1为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己).那么,就可 ...
随机推荐
- Kendo UI开发教程(9): Kendo UI Validator 概述
Kendo UI Validator 支持了客户端校验的便捷方法,它基于HTML 5 的表单校验功能,支持很多内置的校验规则,同时也提供了自定义规则的便捷方法. 完整的Kendo UI 的Valida ...
- java基础知识拾遗(三)
1.类加载 bootstrap classloader -引导(也称为原始)类加载器,它负责加载Java的核心类. extension classloader -扩展类加载器,它负责加载JRE的扩展目 ...
- In-Memory:内存优化数据的持久化和还原
数据持久化是还原的前提,没有数据的持久化,就无法还原内存优化表的数据,SQL Server In-Memory OLTP的内存数据能够持久化存储,这意味着内存数据能够在SQL Server实例重启之后 ...
- Android基础知识-1
1.Android的Activity任务栈 在Android的系统中,在APP运行时每个APP都会默认有一个任务栈,任务栈的名称以APP的包名命名.任务栈是一中先进后出的结构,APP中每一个调用的Ac ...
- JAVA多线程实现的两种方式
java多线程实现方式主要有两种:继承Thread类.实现Runnable接口 1.继承Thread类实现多线程 继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了 ...
- idea导入web项目的部署
前几天 参考 http://zyjustin9.iteye.com/blog/2172712 这篇文章的部署,一直没有问题,今天又部署了一个项目,按照这个步骤,死活却不能部署成功.最后发现,原来是在部 ...
- 国内的阿里云MAVEN仓库,速度很快
配置很简单,修改conf文件夹下的settings.xml文件,添加如下镜像配置: <mirrors> <mirror> <id>alimaven</id&g ...
- java基础面向对象之类与对象
java基础面向对象之类与对象 2017-01-14 1.面向对象的基本概念 以一种组建化的形式进行代码设计 1)在面向对象程序设计中包含有如下几种特性 •封装性:保护内部结构的安全性 •继承性:在已 ...
- JavaScript进阶(三)
现在来说说判断语句(if)if语句是基于条件成立才执行相应代码时使用的语句.语法:if(条件){条件成立时执行代码}注意:if小写,大写字母(IF)会出错!假设你应聘web前端技术开发岗位,如果你会h ...
- MATLAB中的多项式运算
作者:长沙理工大学 交通运输工程学院 王航臣 1.多项式求根 在MATLAB中求取多项式的根用roots函数. 函数:roots 功能:一元高次方程求解. 语法:roots(c) 说明:返回一个列向量 ...