D. 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
1 #include<stdio.h>
2 #include<math.h>
3 #include<queue>
4 #include<algorithm>
5 #include<string.h>
6 #include<iostream>
7 #include<stack>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 LL id[300000];
12 LL dp[300000];
13 LL pp[300000];
14 bool flag[300000];
15 vector<int>vec[300000];
16 int dfs(int n);
17 int ddf(int n);
18 int ac = 0;
19 LL maxxx = -1e18;
20 int main(void)
21 {
22 int n;
23 while(scanf("%d",&n)!=EOF)
24 {
25 int i,j;
26 for(i = 1; i <= n; i++)
27 scanf("%lld",&id[i]);
28 for(i = 0; i <= 200000; i++)
29 dp[i] = -1e16,vec[i].clear();
30 memset(flag,0,sizeof(flag));
31 for(i = 1; i < n; i++)
32 {
33 int x,y;
34 scanf("%d %d",&x,&y);
35 vec[x].push_back(y);
36 vec[y].push_back(x);
37 }
38 if(n == 1)
39 {
40 printf("Impossible\n");
41 }
42 else
43 {
44 ddf(1);
45 memset(flag,0,sizeof(flag));
46 dfs(1);
47 if(ac==0)
48 printf("Impossible\n");
49 else printf("%lld\n",maxxx);
50 }
51 }
52 return 0;
53 }
54 int ddf(int n)
55 {
56 flag[n] = true;
57 for(int i = 0; i < vec[n].size(); i++)
58 {
59 int x = vec[n][i];
60 if(!flag[x])
61 {
62 ddf(x);
63 id[n]+=id[x];
64 }
65 }
66
67 }
68 int dfs(int n)
69 {
70 flag[n] = true;
71 int cn = 0;
72 vector<int>vv;
73 dp[n] = id[n];
74 for(int i = 0; i < vec[n].size(); i++)
75 {
76 int x = vec[n][i];
77 if(!flag[x])
78 {
79 dfs(x);
80 vv.push_back(x);
81 dp[n] = max(dp[x],dp[n]);
82 }
83 }
84 for(int i = 0; i < vv.size(); i++)
85 {
86 pp[cn++] = dp[vv[i]];
87 }
88 sort(pp,pp+cn);
89 if(cn > 1)
90 {
91 maxxx = max(maxxx,pp[cn-1]+pp[cn-2]);
92 ac = 1;
93 }
94 }
D. 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 ...
- Chloe and pleasant prizes
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 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 ...
- 【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为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己).那么,就可 ...
随机推荐
- EXCEL ctrl+e 百变用法不只是你用的那么简单
Excel2013版本中,新增加了一个快捷键:Ctrl+E,可以依据字符之间的关系,实现快速填充功能.一些需要使用公式或者其他功能进行解决的问题,现在只要一个快捷键就可以实现了. 用法1:快速拆解出需 ...
- 漏洞分析:CVE-2017-17215
漏洞分析:CVE-2017-17215 华为HG532路由器的命令注入漏洞,存在于UPnP模块中. 漏洞分析 什么是UPnP? 搭建好环境(使用IoT-vulhub的docker环境),启动环境,查看 ...
- 用原生CSS编写-怦怦跳的心
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 日常Java 2021/11/17
应用程序转换成Applet 将图形化的Java应用程序(是指,使用AWT的应用程序和使用java程序启动器启动的程序)转换成嵌入在web页面里的applet是很简单的.下面是将应用程序转换成.Appl ...
- 大数据学习day11------hbase_day01----1. zk的监控机制,2动态感知服务上下线案例 3.HDFS-HA的高可用基本的工作原理 4. HDFS-HA的配置详解 5. HBASE(简介,安装,shell客户端,java客户端)
1. ZK的监控机制 1.1 监听数据的变化 (1)监听一次 public class ChangeDataWacher { public static void main(String[] arg ...
- 零基础学习java------29---------网络日志数据session案例,runtime(导出jar程序)
一. 网络日志数据session案例 部分数据 数据中的字段分别为: 访客ip地址,访客访问时间,访客请求的url及协议,网站响应码,网站返回数据量,访客的referral url,访客的客户端操作系 ...
- 零基础学习java------day4------流程控制结构
1. 顺序结构 代码从上往下依次执行 2. 选择结构 也叫分支结构,其会根据执行的结果选择不同的代码执行,有以下两种形式: if 语句 switch 语句 2.1 if 语句 2.1.1 if语 ...
- C++基本函数的调用优化(构造、拷贝构造、赋值)
合理的函数可提升时间和空间的利用率 //Test1.h #include<iostream> using namespace std; struct ST { private: int a ...
- 【STM32】WS2812介绍、使用SPI+DMA发送数据
这篇要使用到SPI+DMA,需要了解的话,可以参考我另两篇博客 时钟:https://www.cnblogs.com/PureHeart/p/11330967.html SPI+DMA通信:https ...
- 【Linux】【Services】【SaaS】Spinnaker
1. 简介 1.1. 说明: Spinnaker 是 Netflix 的开源项目,是一个持续交付平台,它定位于将产品快速且持续的部署到多种云平台上.Spinnaker 通过将发布和各个云平台解耦,来将 ...