CodeForces - 743D Chloe and pleasant prizes
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 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.
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
求两个点的的不相交的最大子树的权值和
树形dp dp[i] 表示以该点为i节点的子树当中权值最大的
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const ll INF=1e10;
const int N=+;
const ll mod=;
int head[N];
ll dp[N];
int tot;
ll a[N];
int sum[N];
ll ans;
struct node{
int to,next;
}edge[N<<];
void init(){
memset(head,-,sizeof(head));
for(int i=;i<N;i++){
dp[i]=-INF;
}
tot=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].to=u;
edge[tot].next=head[v];
head[v]=tot++;
}
void DFS(int x,int fa){
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa)continue;
DFS(v,x);
a[x]=a[x]+a[v];
if(dp[x]>-INF)ans=max(ans,dp[x]+dp[v]);
dp[x]=max(dp[x],dp[v]);
}
dp[x]=max(dp[x],a[x]);
}
int main(){
int n;
init();
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%I64d",&a[i]);
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
ans=-INF;
DFS(,);
if(ans==-INF){
cout<<"Impossible"<<endl;
return ;
}
cout<<ans<<endl;
}
CodeForces - 743D Chloe and pleasant prizes的更多相关文章
- Codeforces 743D Chloe and pleasant prizes(树型DP)
D. Chloe and pleasant prizes ...
- codeforces 743D. Chloe and pleasant prizes(树形dp)
题目链接:http://codeforces.com/contest/743/problem/D 大致思路挺简单的就是找到一个父节点然后再找到其两个字节点总值的最大值. 可以设一个dp[x]表示x节点 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces 743D:Chloe and pleasant prizes(树形DP)
http://codeforces.com/problemset/problem/743/D 题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible. 思路:之前好 ...
- [Codeforces743D][luogu CF743D]Chloe and pleasant prizes[树状DP入门][毒瘤数据]
这个题的数据真的很毒瘤,身为一个交了8遍的蒟蒻的呐喊(嘤嘤嘤) 个人认为作为一个树状DP的入门题十分合适,同时建议做完这个题之后再去做一下这个题 选课 同时在这里挂一个选取节点型树形DP的状态转移方程 ...
随机推荐
- vim下阅读代码时标签跳转设置
1.在fedora14中的 /etc/vimrc下,加入如下几行,可根据源代码工程文件的结构来定 2. 在源代码工程内,输入如下命令 ctags -R 当前目录下将生成一个tags文件 3.查看源代码 ...
- SetACL 使用方法详细参数中文解析
示例: SetACL.exe c:\nihao /dir /deny everyone /read_ex 设置E:\wxDesktop 文件夹 everyone 用户为读取和运行权限 SetACL M ...
- Centos6.6 安装nfs网络文件系统
一.介绍 nfs网络文件系统的,大部分用在内网文件共享,比如,对集群上传文件做共享,经常用在图片部分,当然数据量大了还是要做分离,做为专门的接口比较好,介绍一下基本安装环境: 1)Cnetos6.6 ...
- HDU_1079_思维题
Calendar Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- ICMP,ARP协议
ICMP ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息.控制 ...
- cocos自动图集
对于图像资源,为什么要用图集,cocos官网的解释: 1.合成图集时会去除每张图片周围的空白区域,加上可以在整体上实施各种优化算法,合成图集后可以大大减少游戏包体和内存占用2.多个 Sprite 如果 ...
- 浅谈SOCKS5代理与HTTP代理的应用区别
[1]什么是SOCKS5协议. SOCKS是一种网络传输协议,主要用于客户端与外网服务器之间通讯的中间传递.SOCKS是"Sockets”的缩写. 当防火墙后的客户端要访问外部的服务器时,就 ...
- 举枪消灭"烂代码"的实战案例
前言 之前我写过一篇如何少写PHP "烂"代码 https://segmentfault.com/a/11...感觉很多新人对此不太理解.今天以打卡功能为例,去讲解其中的奥秘.那篇 ...
- python最好用的IDE及查看源码的方法
一.PyCharm 很多语言都有比较流行的开发工具,比如JAVA 的Eclipse, C#,C++的VisualStudio,最好的Python 开发IDE就是PyCharm 可以直接调试代码,试运行 ...
- vue 组件通信传值
父子组件通信: 子组件 <template> <div> <h3 @click="alerrt"> 我是子组件一</h3> < ...