Problem:
Score : 1900 points

Problem Statement

There is a tree with N vertices.
The vertices are numbered 1 through N.
For each 1 ≤ i ≤ N - 1, the i-th edge connects vertices a_i and b_i.
The lengths of all the edges are 1.

Snuke likes some of the vertices.
The information on his favorite vertices are given to you as a string s of length N.
For each 1 ≤ i ≤ N, s_i is 1 if Snuke likes vertex i, and 0 if he does not like vertex i.

Initially, all the vertices are white.
Snuke will perform the following operation exactly once:

Select a vertex v that he likes, and a non-negative integer d. Then, paint all the vertices black whose distances from v are at most d.

Find the number of the possible combinations of colors of the vertices after the operation.

Constraints

2 ≤ N ≤ 2×10^5

1 ≤ a_i, b_i ≤ N

The given graph is a tree.

|s| = N

s consists of 0 and 1.

s contains at least one occurrence of 1.

Partial Score

In the test set worth 1300 points, s consists only of 1.

Input

The input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_{N - 1} b_{N - 1}
s

Output

Print the number of the possible combinations of colors of the vertices after the operation.

Sample Input 1

4
1 2
1 3
1 4
1100

Sample Output 1

4

The following four combinations of colors of the vertices are possible:

Sample Input 2

5
1 2
1 3
1 4
4 5
11111

Sample Output 2

11

This case satisfies the additional constraint for the partial score.

Sample Input 3

6
1 2
1 3
1 4
2 5
2 6
100011

Sample Output 3

8

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

#define MAXN 200005
#define INF 0x3f3f3f3f

typedef long long lint;

using namespace std;

struct data {
	int next, to;
	data(int next = 0, int to = 0):next(next), to(to) {}
}edge[MAXN << 1];

struct node {
	int maxd, state;
	node(int maxd = 0, int state = 0):maxd(maxd), state(state) {}

	bool operator < (const node b) const {
		return maxd < b.maxd;
	}
};

int n; 
int fa[MAXN], state[MAXN], maxd[MAXN], maxu[MAXN], se[MAXN], semaxd[MAXN];
int stated[MAXN], stateu[MAXN], target[MAXN], times[MAXN], deg[MAXN];
int head[MAXN], cnt;
lint ans = 0;

void connect(int u, int v) {
	edge[++cnt] = data(head[u], v);
	head[u] = cnt;
}

void DFS1(int u, int pre) {
	fa[u] = pre;
	times[u] = stated[u] = state[u];
	for (int i = head[u]; i; i = edge[i].next) {
		int v = edge[i].to;
		++deg[u];
		if (v == pre)
			continue;
		DFS1(v, u);
		stated[u] |= stated[v];
		if (stated[v]) {
			++times[u];
			target[u] = v;
		}
		if (maxd[v] + 1 > maxd[u]) {
			semaxd[u] = maxd[u];
			maxd[u] = maxd[v] + 1;
		}
		else if (maxd[v] + 1 > semaxd[u]) 
			semaxd[u] = maxd[v] + 1;
	}
}

void DFS2(int u, int pre) {
	if (u != 1) {
		if (maxd[fa[u]] == maxd[u] + 1)
			maxu[u] = semaxd[fa[u]] + 1;
		else 
			maxu[u] = maxd[fa[u]] + 1;
		maxu[u] = max(maxu[u], maxu[fa[u]] + 1);
		if ((stated[fa[u]] && times[fa[u]] > 1) || (stated[fa[u]] && !stated[u]) || stateu[fa[u]])
			stateu[u] = 1;
	}
	for (int i = head[u]; i; i = edge[i].next) {
		int v = edge[i].to;
		if (v == pre)
			continue;
		DFS2(v, u);
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i < n; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		connect(u, v);
		connect(v, u);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%1d", &state[i]);
	DFS1(1, 0);
	DFS2(1, 0);
	for (int u = 1; u <= n; ++u) {
		if ((maxd[u] < maxu[u] - 1 && stated[u]) || (maxd[u] > maxu[u] - 1 && stateu[u]) || (maxd[u] == maxu[u] - 1 && (stateu[u] || stated[u]))) {
			++ans;
			// cout << u << " type1\n";
		}
		int up = 0;
		if (maxu[u] > maxd[u])
			up = maxd[u];
		else 
			up = max(maxu[u], semaxd[u]);
		if (state[u]) {
			ans += up + 1;
			// cout << u << " type2\n";
		}
		else {
			int down = stateu[u] ? maxu[u] : INF;
			for (int i = head[u]; i; i = edge[i].next) {
				int v = edge[i].to;
				if (v == fa[u])
					continue;
				if (stated[v])
					down = min(down, maxd[v] + 1);
			}
			// cout << u << " type3\n";
			ans += max(0, up - down + 1);
		}
		// cout  << deg[u] << " " << maxd[u] << " " << maxu[u] << endl;
	}
	printf("%lld\n", ans);
}