codeforces 743D Chloe and pleasant prizes (DFS/树形DP)
2016-12-17 22:11
519 查看
[align=center]D. Chloe and pleasant prizes[/align]
[align=center]传送门:http://codeforces.com/contest/743/problem/D[/align]
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 ton).
A gifti is characterized by integerai —
pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift1 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 withn
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, printImpossible.
Input
The first line contains a single integern (1 ≤ n ≤ 2·105) —
the number of gifts.
The next line contains n integersa1, 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 integersui
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.
Output
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.
Examples
Input
Output
Input
Output
Input
Output
题目大意:从一棵树中求出最大的两个不相交子树和,权值可能为负
解题思路:首先告诉你 n-1 条边来建一个树,不要被题目迷惑,最终树的情况是唯一的。然后从根节点开始向下遍历,依次求出当前节点为根的树的权值和 sum[ i ] ,还有其最大的两个子树和(这里的处理比较巧妙),递归实现。然后每次更新当前答案。注意 a 数组的两个含义。
AC代码:
[align=center]传送门:http://codeforces.com/contest/743/problem/D[/align]
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 ton).
A gifti is characterized by integerai —
pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift1 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 withn
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, printImpossible.
Input
The first line contains a single integern (1 ≤ n ≤ 2·105) —
the number of gifts.
The next line contains n integersa1, 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 integersui
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.
Output
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.
Examples
Input
8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8
Output
25
Input
4 1 -5 1 1 1 2 1 4 2 3
Output
2
Input
1 -1
Output
Impossible
题目大意:从一棵树中求出最大的两个不相交子树和,权值可能为负
解题思路:首先告诉你 n-1 条边来建一个树,不要被题目迷惑,最终树的情况是唯一的。然后从根节点开始向下遍历,依次求出当前节点为根的树的权值和 sum[ i ] ,还有其最大的两个子树和(这里的处理比较巧妙),递归实现。然后每次更新当前答案。注意 a 数组的两个含义。
AC代码:
#include <bits/stdc++.h> #define Fori(x) for(int i=0;i<x;i++) #define Forj(x) for(int j=0;j<x;j++) #define maxn 5005 #define inf 0x3f3f3f3f #define ONES(x) __builtin_popcount(x) using namespace std; typedef long long ll ; const double eps =1e-8; const int mod = 1000000007; typedef pair<int, int> P; const double PI = acos(-1.0); int dx[4] = {0,0,1,-1}; int dy[4] = {1,-1,0,0}; const ll INF = (ll)1e18 + 1; const int N = 300300; int n; ll ans = -INF; ll a ; ll sum ;//储存每个节点的权值加上所有子树和 vector<int> g ;//储存每个节点的 bool used ;//建树时使用,表明是否访问 void dfs(int u) { used[u] = 1; sum[u] = a[u]; a[u] = -INF; ll m1 = -INF , m2 = -INF; for(int i = 0; i<g[u].size(); i++) { int v = g[u][i]; if(used[v]) continue; dfs(v); sum[u] += sum[v]; ll val = a[v]; a[u] = max(a[u], val);//储存最大子树和 if(m1 < val) swap(m1,val);//这两行比较精彩,很简洁的找到序列中最大的两个数 if(m2 < val) swap(m2,val);//这两步是用来保留根节点最大的两个子树和 } if(m2 > -INF) ans = max(ans, m1 + m2);//如果有两个子树和 更新答案 a[u] = max(a[u], sum[u]);//更新a[i] 表示为根节点为u的树和其所有子树的中最大的和 } int main() { //freopen("test.txt","r",stdin); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); for(int i = 1; i<n; i++) { int u,v; cin>>u>>v; v--;u--; g[u].push_back(v); g[v].push_back(u); } dfs(0); if(ans==-INF) cout << "Impossible" << endl; else cout << ans << endl; return 0; } /* unsigned int 0~4294967295 int 2147483648~2147483647 unsigned long 0~4294967295 long 2147483648~2147483647 long long的最大值:9223372036854775807 long long的最小值:-9223372036854775808 unsigned long long的最大值:18446744073709551615 __int64的最大值:9223372036854775807 __int64的最小值:-9223372036854775808 unsigned __int64的最大值:18446744073709551615 */
相关文章推荐
- Codeforces 337 D Book of Evil(树形dp,两遍dfs)
- codeforces 645 D Robot Rapping Results Report 【树形dp】
- HDU 4582 DFS spanning tree 解题报告(贪心 & 树形DP)
- 【codeforces】543D. Road Improvement【树形DP】
- Codeforces 711C. Bear and Tree Jumps【树形dp好题】
- Codeforces 696B Puzzles(期望+树形dp)
- Codeforces-337D Book of Evil【树形dp】
- HDU1078(树形DP+DFS)
- CodeForces 101D Castle(树形dp)
- Codeforces 280C Game on Tree 树形期望dp
- [BZOJ4424]CodeForces 19# E Fairy(树形dp / Link-Cut-tree)
- HDU 1520 Anniversary party (树形dp,dfs+dp)
- 51nod 1405 树的距离之和 (两次dfs,树形dp)
- codeforces 61D Eternal Victory 树形DP
- 【bzoj2466】【caioj1113】树---树形DP/高斯消元+dfs
- codeforces 258B Little Elephant and Elections 数位DP+DFS
- CodeForces 161D Distance in Tree 树形DP
- CodeForces 238C World Eater Brothers 树形dp
- codeforces 461B B. Appleman and Tree(树形dp)
- hihocoder1104(Dfs预处理+树形DP)