반응형

F. Maximum White Subtree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

You have to solve the following problem for each vertex vv: 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 vv? 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 cntwcntbcntw−cntb.

Input

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

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

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

It is guaranteed that the given edges form a tree.

Output

Print nn integers res1,res2,,resnres1,res2,…,resn, where resiresi is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex ii.

 

 

<코드>

#include <stdio.h>
#include <vector>
using namespace std;

vector<int> Edges[200005];
vector<int> fromCh[200005];
bool Visit[200005];

class node
{
public:
	int bw, upMax, ans;
}a[200005];

int dfs(int pos)
{
	int thisChild;
	int k = Edges[pos].size();
	Visit[pos] = true;
	a[pos].upMax = 0;
	for (int i = 0; i < k; i++)
	{
		if (!Visit[Edges[pos][i]])
		{
			thisChild = dfs(Edges[pos][i]);
			fromCh[pos][i] = thisChild;
			if (thisChild != -1) a[pos].upMax += thisChild;
		}
	}
	if (a[pos].bw) a[pos].upMax++;
	else a[pos].upMax--;
	return a[pos].upMax;
}


int dfs2(int pos, int fP)
{
	int k = Edges[pos].size();
	Visit[pos] = true;
	if (fP != -1) a[pos].ans = a[pos].upMax + fP;
	else a[pos].ans = a[pos].upMax;
	for (int i = 0; i < k; i++)
	{
		if (!Visit[Edges[pos][i]])
		{
			if (fromCh[pos][i] == -1) dfs2(Edges[pos][i], a[pos].ans);
			else dfs2(Edges[pos][i], a[pos].ans- fromCh[pos][i]);
		}
	}
	return 0;
}



int main()
{
	long long ans;
	int n, k, p, q;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &(a[i].bw));
	for (int i = 1; i < n; i++)
	{
		scanf("%d %d", &p, &q);
		Edges[p].push_back(q);
		Edges[q].push_back(p);
		fromCh[p].push_back(0);
		fromCh[q].push_back(0);
	}
	dfs(1);
	for (int i = 1; i <= n; i++) Visit[i] = 0;
	dfs2(1, 0);
	for (int i = 1; i <= n; i++) printf("%d ", a[i].ans);
	printf("\n");

	return 0;

}

 

 

 

 

 

 

https://codeforces.com/problemset/problem/1324/F

 

Problem - 1324F - Codeforces

 

codeforces.com

 

반응형

+ Recent posts