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为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己).那么,就可 ...
随机推荐
- C++面试基础篇(一)
1. static关键字的作用 (1)全局静态变量 在全局变量前面加上关键字static, 全局变量就定义为一个全局静态变量 在静态存储区,在整个程序运行期间一致存在. 初始化:未初始化的全局静态变量 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(终)-配合内存管理来遍历SD卡
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- Linux学习 - 数值运算
1 declare 声明变量类型 declare [+/-] [选项] 变量名 - 给变量设定类型属性 + 取消变量的类型属性 -i 将变量声明为整数型 -x 将变量声明为环境变量(同export) ...
- Android 基础UI组件(一)
1.Toast //显示文字 Toast.makeText(this,"Toast显示文本",Toast.LENGTH_SHORT).show(); //显示图片 Toast to ...
- spring注解-扩展原理
AnnotationConfigApplicationContext(IOC容器)的有参构造方法中,在refresh()里对这些组件进行初始化 BeanPostProcessor bean后置处理器, ...
- jquery datatable真实示例
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncodin ...
- Docker 安装 Oracle12c
为选定需要pull到系统中的数据库镜像 # docker pull sath89/oracle-12c --------sath89/oracle-12c为选定需要pull到系统中的数据库镜像 doc ...
- Maven错误收集
Eclipse 创建项目时报错 Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1 ...
- 2.9 go mod 之本地仓库搭建
wikihttps://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release参考https://blog.csdn.net/be ...
- 【Spring Framework】Spring入门教程(二)基于xml配置对象容器
基于xml配置对象容器--xml 标签说明 alias标签 作用:为已配置的bean设置别名 --applicationContext.xml配置文件 <?xml version="1 ...