Codeforces 1324F - Maximum White Subtree 【树形DP】

Maximum White Subtree
Time Limit: 2000 MS Memory Limit: 256MB

Problem Description

You are given a tree consisting of nn vertices. A tree is a connected undirected graph with n−1 edges. Each vertex v of this tree has a color assigned to it (av=1 if the vertex v is white and 0 if the vertex v is black).

You have to solve the following problem for each vertex v: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex v? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains cntwcntw white vertices and cntbcntb black vertices, you have to maximize cntw−cntb.

Input

he first line of the input contains one integer n ( 2 n 2 1 0 5 2≤n≤2⋅10^5 ) — the number of vertices in the tree.

The second line of the input contains nn integers a1,a2,…,an (0≤ai≤1), where ai is the color of the i-th vertex.

Each of the next n−1 lines describes an edge of the tree. Edge i is denoted by two integers ui and vi, the labels of vertices it connects (1≤ui,vi≤n,ui≠vi).

It is guaranteed that the given edges form a tree.

Output

Print n integers res1,res2,…,resn, where resi is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex i.

Examples

Input

9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9

Output

4
0 0 1 0
1 2
1 3
1 4

【题目链接】 Maximum White Subtree

【题意】

给出一颗含有n个节点的树,每个节点的颜色是白色或者黑色,你需要计算出包含节点 i i 的子图中,白色节点个数与黑色节点个数差值的最大值

【思路】

对于子图来说,这棵树把谁作为根对答案不影响,所以我们把节点1作为根。

正常的树形dp我们都是从下往上的,也就是说能处理出以 i i 为根的子图的最优解dp[i],但是对于答案显然少了 i i 上面的部分。

那么这个时候我们就要考虑从上往下了,设包含节点 i i 的父亲节点的子图的最优解为pre[i],那么答案就是dp[i]+pre[i]。

对于根节点来说,其实答案就是ans[1]=dp[1]。

那么对于根节点的儿子节点v来说,其pre[v]=max(ans[1]-max(dp[v],0),0),这里知道了pre[v]之后又可以计算ans[v]了,以此类推可以得到答案。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
const int maxn=200005;
const int INF=0x3f3f3f3f;
const ll mod=998244353;

int n;
int a[maxn];
int ans[maxn];
int pre[maxn];
int dp[maxn];
vector<int>vec[maxn];


void dfs(int u,int fa)
{
    dp[u]=a[u];
    for(int i=0;i<vec[u].size();i++)
    {
        int v=vec[u][i];
        if(v==fa) continue;
        dfs(v,u);
        dp[u]+=max(dp[v],0);
    }
}

void dfs2(int u,int fa)
{
    ans[u]=dp[u]+pre[u];
    for(int i=0;i<vec[u].size();i++)
    {
        int v=vec[u][i];
        if(v==fa) continue;
        pre[v]=max(0,ans[u]-max(0,dp[v]));
        dfs2(v,u);
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==0) a[i]=-1;
    }
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        vec[x].push_back(y);
        vec[y].push_back(x);
    }
    dfs(1,-1);
    dfs2(1,-1);
    for(int i=1;i<n;i++) printf("%d ",ans[i]);
    printf("%d\n",ans[n]);
}

发布了259 篇原创文章 · 获赞 100 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/104837234