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: Wrong Answer)

#include<stdio.h>
#include<queue>
#include<assert.h>
#include<tuple>
#include<string>
#include<algorithm>
#include<iostream>
#include<map>
#include<string.h>
#include<vector>
#include<math.h>
#include<stdlib.h>
#include<set>
#include<ctype.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef tuple<int,int,int> t3;

const int MX = 1 << 18;
const int MM = 1000000007;

int N;
char D[MX];
vector<int> T[MX];
int lev[MX], vst[MX], d1[MX], d2[MX];
int cnt[MX], low[MX];

void bfs(int x, int d[MX]){
	queue<int> Q;
	for(int i = 0; i < MX; i++) d[i] = 0;
	Q.push(x); d[x] = 1;
	while(Q.size()){
		int p = Q.front(); Q.pop();
		for(int c : T[p]){
			if( d[c] ) continue;
			d[c] = d[p] + 1;
			Q.push(c);
		}
	}
}

int main()
{
	scanf("%d", &N);
	for(int i = 2; i <= N; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		T[a].push_back(b);
		T[b].push_back(a);
	}
	scanf("%s", D+1);
	
	bfs(1, d1);
	int c = 1, d = 1;
	for(int i = 2; i <= N; i++) if( d1[c] < d1[i] ) c = i;
	bfs(c, d1);
	for(int i = 2; i <= N; i++) if( d1[d] < d1[i] ) d = i;
	bfs(d, d2);
	
	for(int i = 1; i <= N; i++) cnt[i] = T[i].size();
	queue<int> Q;
	for(int i = 1; i <= N; i++){
		vst[i] = 0;
		if( T[i].size() == 1 ) Q.push(i), vst[i] = 1;
	}
	while(Q.size()){
		int p = Q.front(); Q.pop();
		for(int c : T[p]){
			if( vst[c] ) continue;
			lev[c] = max(lev[c], lev[p] + 1);
			if( --cnt[c] == 1 ) Q.push(c), vst[c] = 1;
		}
	}

	for(int i = 1; i <= N; i++) low[i] = 1e9;
	priority_queue<pii, vector<pii>, greater<pii>> Qu;
	for(int i = 1; i <= N; i++) if( D[i] == '1' ){
		low[i] = lev[i];
		Qu.emplace(low[i], i);
	}
	while(Qu.size()){
		int c = Qu.top().first, d = Qu.top().second; Qu.pop();
		if( low[d] != c ) continue;
		for(int e : T[d]){
			if( low[e] <= d + 1) continue;
			low[e] = d+1;
			Qu.emplace(low[e], e);
		}
	}
	for(int i = 1; i <= N; i++) if( D[i] == '1' )low[i] = 0;

	ll ans = 1;
	for(int i = 1; i <= N; i++){
		if( lev[i]+2 >= low[i] ) ans += min(max(d2[i], d1[i])-1, lev[i]-low[i]+2);
	}
	printf("%lld\n", ans);
}