Maximum White Subtree
Problem Description
You are given a tree consisting of n n n vertices. A tree is a connected undirected graph with n − 1 n−1 n−1 edges. Each vertex v of this tree has a color assigned to it ( a v = 1 a_v=1 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 cntw white vertices and cntb black vertices, you have to maximize c n t w − c n t b cntw−cntb cntw−cntb.
Input
The first line of the input contains one integer n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2≤n≤2⋅10^5 2≤n≤2⋅105) — the number of vertices in the tree.
The second line of the input contains n integers a 1 a_1 a1, a 2 a_2 a2,…, a n a_n an ( 0 ≤ a i ≤ 1 0≤a_i≤1 0≤ai≤1), where a i a_i ai is the color of the i-th vertex.
Each of the next n − 1 n−1 n−1 lines describes an edge of the tree. Edge i is denoted by two integers u i ui ui and v i vi vi, the labels of vertices it connects ( 1 ≤ u i , v i ≤ n , u i ≠ v i 1≤u_i,v_i≤n,u_i≠v_i 1≤ui,vi≤n,ui=vi).
It is guaranteed that the given edges form a tree.
Output
Print n integers r e s 1 res_1 res1, r e s 2 res_2 res2,…, r e s n res_n resn, where r e s i res_i resi is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex i.
Sample 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
Sample Output
2 2 2 2 2 1 1 0 2
题意
有一个无根树,树上的边无向,树上的每个点都有颜色,黑或白。设cntw 为白点数量,cntb为黑点数量,求对每个i,找出包含点i的连通块使cntw-cntb最大。
题解:
动态规划。首先随意指定一个根,然后DFS,初始若
a
[
u
]
=
=
1
a[u] == 1
a[u]==1 则
d
p
1
[
u
]
=
1
dp1[u] = 1
dp1[u]=1;否则
d
p
1
[
u
]
=
0
dp1[u] = 0
dp1[u]=0。然后遍历访问其所有子节点,
d
p
1
[
u
]
+
=
m
a
x
(
0
,
d
p
1
[
s
o
n
[
u
]
[
i
]
]
)
dp1[u] += max(0,dp1[son[u][i]])
dp1[u]+=max(0,dp1[son[u][i]])。显然dp1只考虑节点及其子树内的部分,被没有考虑其父节点的部分。
所以DFS第二遍,当
d
p
1
[
u
]
≥
0
dp1[u]\ge0
dp1[u]≥0,则它的父节点计算
d
p
1
[
f
a
[
u
]
]
dp1[fa[u]]
dp1[fa[u]]时已经将他考虑进去了,所以不能加上dp1[u],但其父节点可能只有-1,所以可以能不加上其父节点。当
d
p
1
[
u
]
<
0
dp1[u]<0
dp1[u]<0,则取 本身、本身+父节点 的最大值即可。
所以递推式如下:
d
p
1
[
u
]
≥
0
dp1[u] \ge 0
dp1[u]≥0,则
d
p
2
[
u
]
=
m
a
x
(
d
p
1
[
u
]
,
d
p
2
[
f
a
[
u
]
]
)
dp2[u] = max(dp1[u],dp2[fa[u]])
dp2[u]=max(dp1[u],dp2[fa[u]]);
d
p
1
[
u
]
<
0
dp1[u]<0
dp1[u]<0,则
d
p
2
[
u
]
=
m
a
x
(
d
p
1
[
u
]
,
d
p
1
[
u
]
+
d
p
2
[
f
a
[
u
]
]
)
dp2[u] = max(dp1[u], dp1[u]+dp2[fa[u]])
dp2[u]=max(dp1[u],dp1[u]+dp2[fa[u]])。
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<map>
#include<vector>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const int maxn = 200100;
const int mod = 1000000000;
int dp1[maxn], dp2[maxn], a[maxn], fa[maxn];
vector<int> g[maxn];
void dfs(int u);
void dfs2(int u);
int main()
{
int n, i, j, k;
scanf("%d", &n);
for(i=1;i<=n;i++)
scanf("%d", &a[i]);
for(i=1;i<n;i++){
scanf("%d %d", &j, &k);
g[j].push_back(k);
g[k].push_back(j);
}
fa[1] = 0;
dfs(1);
dfs2(1);
for(i=1;i<n;i++)
printf("%d ", dp2[i]);
printf("%d\n", dp2[n]);
return 0;
}
void dfs(int u)
{
if(a[u] == 0)dp1[u] = -1;
else dp1[u] = 1;
for(int i=0;i<g[u].size();i++)
if(g[u][i] != fa[u]){
fa[g[u][i]] = u;
dfs(g[u][i]);
dp1[u] = max(dp1[u], dp1[u]+dp1[g[u][i]]);
}
}
void dfs2(int u)
{
if(u == 1)dp2[u] = dp1[u];
else if(dp1[u] >= 0)dp2[u] = max(dp1[u], dp2[fa[u]]);
else dp2[u] = max(dp1[u], dp1[u]+dp2[fa[u]]);
for(int i=0;i<g[u].size();i++)
{
if(g[u][i] != fa[u])
dfs2(g[u][i]);
}
}