zoukankan      html  css  js  c++  java
  • [CF743D] Chloe and pleasant prizes(树形dp)

    【原题】

    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 a i — 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.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of gifts.

    The next line contains n integers a 1, a 2, ..., a n ( - 109 ≤ a i ≤ 109) — the pleasantness of the gifts.

    The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers u i and v i (1 ≤ u i, v i ≤ n, u i ≠ v i) — the description of the tree's edges. It means that gifts with numbers u i and v i are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: v i hangs on u i or u i hangs on v i.

    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
    

    【题意】
    求两个不交错的子树的最大权值和。

    【思路】

    树形dp,求出当前节点下的最大子树和和次大子树和,并将最大子树和和以当前节点为根的子树和比较,这个最大值传给其父节点继续比较。

    AC代码:

    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <list>
    #include <map>
    #include <iostream>
    #include <iomanip>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <unordered_map>
    #include <vector>
    #define LL long long
    #define inf 0x3f3f3f3f
    #define INF 0x3f3f3f3f3f3f
    #define PI 3.1415926535898
    #define F first
    #define S second
    #define endl '
    '
    #define lson  rt << 1
    #define rson  rt << 1 | 1
    #define f(x, y, z) for (int LL x = (y), __ = (z); x < __; ++x)
    #define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
    using namespace std;
    
    const int maxn = 2e5 + 7;
    const int maxm = 1e9 + 7;
    const int mod = 1e9 + 7;
    LL n, a[maxn], w[maxn], mx[maxn], submx[maxn], ans = -INF;
    vector<int> G[maxn];
    
    LL build(int u, int f)
    {
    	w[u] = a[u];
    	LL tmp;
    	f(i, 0, G[u].size())
    	{
    		int v = G[u][i];
    		if (v == f) continue;
    		LL tmp = build(v, u);
    		w[u] += w[v];
    		if (tmp >= mx[u])
    		{
    			submx[u] = mx[u];
    			mx[u] = tmp;
    		}
    		else if (tmp > submx[u])
    		{
    			submx[u] = tmp;
    		}
    		if (submx[u] > -INF) ans = max(ans, mx[u] + submx[u]);
    	}
    	if (w[u] > mx[u]) mx[u] = w[u];
    	return mx[u];
    }
    
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	cin >> n;
    	_rep(i, 1, n)
    	{
    		cin >> a[i];
    		mx[i] = submx[i] = -INF;
    	}
    	int ta, tb;
    	_rep(i, 1, n - 1)
    	{
    		cin >> ta >> tb;
    		G[ta].push_back(tb);
    		G[tb].push_back(ta);
    	}
    	int yz = 0;
    	_rep(i, 2, n)
    	{
    		if (G[i].size() == 1) yz++;
    	}
    	if (yz < 2)
    	{
    		cout << "Impossible" << endl;
    		return 0;
    	}
    	build(1, -1);
    	cout << ans << endl;
    }
    

    WA代码:保存每个节点的最大子树和,第二次dfs找子节点数>1的点,计算其子节点的最大和次大子树和,忽略了最大和次大都在一个子节点之下的情况。

    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <list>
    #include <map>
    #include <iostream>
    #include <iomanip>
    #include <queue>
    #include <set>
    #include <stack>
    #include <string>
    #include <unordered_map>
    #include <vector>
    #define LL long long
    #define inf 0x3f3f3f3f
    #define INF 0x3f3f3f3f3f3f
    #define PI 3.1415926535898
    #define F first
    #define S second
    #define endl '
    '
    #define lson  rt << 1
    #define rson  rt << 1 | 1
    #define f(x, y, z) for (int LL x = (y), __ = (z); x < __; ++x)
    #define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
    using namespace std;
    
    const int maxn = 2e5 + 7;
    const int maxm = 1e9 + 7;
    const int mod = 1e9 + 7;
    LL n, a[maxn], w[maxn], p[maxn], mxx[maxn];
    vector<int> G[maxn];
    int build(int u, int f)
    {
    	w[u] = a[u];
    	f(i, 0, G[u].size())
    	{
    		int v = G[u][i];
    		if (v == f) continue;
    		build(v, u);
    		if (mxx[v] > mxx[u])
    		{
    			mxx[u] = mxx[v];
    			p[u] = p[v];
    		}
    		w[u] += w[v];
    	}
    	if (w[u] > mxx[u])
    	{
    		mxx[u] = w[u];
    		p[u] = u;
    	}
    	return 0;
    }
    LL dfs(int u, int f)
    {
    	int num = 0, son = 0;
    	LL mx = -INF, submx = -INF;
    	f(i, 0, G[u].size())
    	{
    		int v = G[u][i];
    		if (v == f) continue;
    		num++;
    		son = v;
    		if (mxx[v] >= mx)
    		{
    			submx = mx;
    			mx = mxx[v];
    		}
    		else if (mxx[v] > submx)
    		{
    			submx = w[v];
    		}
    	}
    	if (!son) return mxx[u];
    	if (num >= 2) return submx + mx;
    	else return (dfs(son, u));
    }
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	cin >> n;
    	_rep(i, 1, n)
    	{
    		cin >> a[i];
    		mxx[i] = -INF;
    	}
    	int ta, tb;
    	_rep(i, 1, n - 1)
    	{
    		cin >> ta >> tb;
    		G[ta].push_back(tb);
    		G[tb].push_back(ta);
    	}
    	int yz = 0;
    	_rep(i, 2, n)
    	{
    		if (G[i].size() == 1) yz++;
    	}
    	if (yz < 2)
    	{
    		cout << "Impossible" << endl;
    		return 0;
    	}
    	build(1, -1);
    	cout << dfs(1, -1) << endl;
    }
    
  • 相关阅读:
    jvm性能调优---jstat的用法
    flume-ng+Kafka+Storm+HDFS 实时系统搭建
    proxool
    Shell实现跳板机,为什么用跳板机
    JUC回顾之-ThreadPoolExecutor的原理和使用
    java集合之ArrayList的实现原理
    JMeter性能测试介绍学习一
    基础知识《十三》深入浅出Java回调机制
    怎样将myeclipse里默认编码设置成utf-8
    《转》怎样看待比自己强的人
  • 原文地址:https://www.cnblogs.com/hfcdyp/p/13502174.html
Copyright © 2011-2022 走看看