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>
using namespace std;
const int N=2e5+10;
const int inf=1e9;
typedef long long ll;
int gi() {
    int x=0,o=1;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
struct edge { int to,nxt; } e[N<<1];
int tot=1,h[N];
void adde(int u,int v) {
    e[++tot]=(edge){v,h[u]};h[u]=tot;
    e[++tot]=(edge){u,h[v]};h[v]=tot;
}
int n,sum=0,siz[N],mx[N],sx[N],l[N],r[N];
char s[N];
ll ans=0;
void dfs1(int u,int fa) {
    siz[u]=s[u]-'0';
    for(int i=h[u];i;i=e[i].nxt) {
        int v=e[i].to; if(v==fa) continue;
        dfs1(v,u); siz[u]+=siz[v];
        if(mx[v]+1>mx[u]) sx[u]=mx[u],mx[u]=mx[v]+1;
        else if(mx[v]+1>sx[u]) sx[u]=mx[v]+1;
    }
}
void dfs2(int u,int fa,int len) {
    r[u]=len>mx[u]?mx[u]:max(len,sx[u]);
    l[u]=s[u]-'0'?0:(sum-siz[u]?len:inf);
    if(fa) {
        if(len-1==mx[u]) ans++;
        else if(len-1<mx[u]) ans+=siz[u]<sum;
        else ans+=siz[u]>0;
    }
    for(int i=h[u];i;i=e[i].nxt) {
        int v=e[i].to; if(v==fa) continue;
        if(siz[v]) l[u]=min(l[u],mx[v]+1);
        if(mx[u]==mx[v]+1) dfs2(v,u,max(len,sx[u])+1);
        else dfs2(v,u,max(len,mx[u])+1);
    }
}
int main() {
    cin>>n;
    for(int i=1,u,v;i<n;i++) u=gi(),v=gi(),adde(u,v);
    scanf("%s",s+1);
    for(int i=1;i<=n;i++) sum+=s[i]-'0';
    dfs1(1,0);dfs2(1,0,0);
    for(int i=1;i<=n;i++) if(l[i]<=r[i]) ans+=r[i]-l[i]+1;
    printf("%lld\n",ans);
    return 0;
}