Maximum White Subtree(dfs)

outputstandard output
You are given a tree consisting of n 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 cntw white vertices and cntb black vertices, you have to maximize cntw−cntb.

Input
The first line of the input contains one integer n (2≤n≤2⋅105) — the number of vertices in the tree.

The second line of the input contains n 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
inputCopy
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
outputCopy
2 2 2 2 2 1 1 0 2
inputCopy
4
0 0 1 0
1 2
1 3
1 4
outputCopy
0 -1 1 -1
Note
The first example is shown below:
Maximum White Subtree(dfs)_第1张图片

The black vertices have bold borders.

In the second example, the best subtree for vertices 2,3 and 4 are vertices 2,3 and 4 correspondingly. And the best subtree for the vertex 1 is the subtree consisting of vertices 1 and 3.
思路:对这样例看了半天。看题半小时,写题十分钟。
思路其实很简单,这棵树没有根,我们假设1为根的话,可以很快找出1的最优值。同理,以2,3…为根,也可以求出来,但是那样太慢了。对于当前这个点,如果它以1为根的最优值是正数的话,那么它对父节点一定是有贡献的。我们在求这个点的最优值的时候,如果这个点本身的最优值就是正数的话,就和父节点取最大值,如果是负数的话并且父节点最优值为正数,就加上。这样取最优。
代码如下:

#include
#define ll long long
using namespace std;

const int maxx=2e5+100;
struct edge{
	int next;
	int to;
}e[maxx<<1];
int head[maxx<<1];
int vis[maxx],fa[maxx],sum[maxx],ans[maxx];
int n,tot=0;

inline void add(int u,int v)
{
	e[tot].next=head[u],e[tot].to=v,head[u]=tot++;
}
inline void dfs(int u,int f)
{
	fa[u]=f;
	sum[u]=(vis[u]==1?1:-1);
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		dfs(to,u);
		if(sum[to]>=0) sum[u]+=sum[to];
	}
}
inline void Dfs(int u,int f)
{
	if(sum[u]>=0) ans[u]=max(sum[u],ans[fa[u]]);//主体代码
	else if(ans[fa[u]]>0) ans[u]=sum[u]+ans[fa[u]];
	else ans[u]=sum[u];
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		Dfs(to,u);
	}
}
int main()
{
	scanf("%d",&n);
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++) scanf("%d",&vis[i]);
	int x,y;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	sum[0]=-1*maxx;
	dfs(1,0);
	ans[1]=sum[1];
	Dfs(1,0);
	for(int i=1;i<=n;i++) cout<<ans[i]<<" ";cout<<endl;
	return 0;
}

努力加油a啊,(o)/~

你可能感兴趣的:(Maximum White Subtree(dfs))