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为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己).那么,就可 ...
 
随机推荐
- RabbitMQ消息中介之Python使用
			
本文介绍RabbitMQ在python下的基本使用 1. RabbitMQ安装,安装RabbitMQ需要预安装erlang语言,Windows直接下载双击安装即可 RabbitMQ下载地址:http: ...
 - 单元测试在Unity中的应用
			
项目描述:简单演示单元测试在Unity中的应用 项目地址:UnityTestRunner_Tutorial - SouthBegonia 项目版本:2020.3.20f1 项目用法:打开就用,代码都放 ...
 - 学习java的第二十一天
			
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
 - 使用Rapidxml重建xml树
			
需求 : 重建一棵xml树, 在重建过程中对原来的标签进行一定的修改. 具体修改部分就不给出了, 这里只提供重建部分的代码 code : /****************************** ...
 - shell awk命令字符串拼接
			
本节内容:awk命令实现字符串的拼接 输入文件的内容: TMALL_INVENTORY_30_GROUP my163149.cm6 3506 5683506 mysql-bin.000013 3273 ...
 - Spring Boot下使用JSP页面
			
一.创建webapp目录 在src/main下创建webapp目录,用于存放jsp文件.这就是一个普通的目录,无需执行Mark Directory As 二.创建jsp 1.指定web资源目录 在sp ...
 - java中二维数组初始化的几种方法
			
/* 第一种方式 */ int tdarr1[][] = { { 1, 3, 5 }, { 5, 9, 10 } }; /* 第二种方式 */ int tdarr2[][] = new int[][] ...
 - Java 将Word转为OFD
			
通常在工作中比较常用到的Microsoft Word是属于国外的文档内容编辑软件,其编译技术均属国外.而OFD是一种我国的自主文档格式,在某些特定行业或企业的文档存储技术上是一种更为安全的选择.下面将 ...
 - 学习 27 门编程语言的长处,提升你的 Python 代码水平
			
Python猫注:Python 语言诞生 30 年了,如今的发展势头可谓如火如荼,这很大程度上得益于其易学易用的优秀设计,而不可否认的是,Python 从其它语言中偷师了不少.本文作者是一名资深的核心 ...
 - 在Eclipse中编写jQuery代码时产生的错误(连载)
			
1.Error:启动Eclipse中的服务,显示错误,端口号被占用 解决方法: 方式一:修改对应的端口号(实际情况实际处理) 方式二:在进程中关闭Eclispe重新打开即可(截图说明) 2.Error ...