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)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1.0);

const int mod = (int) 1e9 + 7;

void add(int &x, int y) {
  if ((x += y) >= mod) {
    x -= mod;
  }
}

int mult(int x, int y) {
  return (long long) x * y % mod;
}

int myPower(int x, int pw) {
  int res = 1;
  for (; pw; pw >>= 1) {
    if (pw & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
  }
  return res;
}

void precalc() {
}


const int maxn = (int) 2e5 + 10;
string s;
int n;

vector<vector<int> > es, ds, mxds[2];

int read() {
  if (scanf("%d", &n) < 1) {
    return 0;
  }
  es = vector<vector<int> >(n);
  for (int i = 0; i < n - 1; ++i) {
    int s, t;
    scanf("%d%d", &s, &t);
    --s, --t;
    es[s].pb(t), es[t].pb(s);
  }
  cin >> s;
  return 1;
}

int cnt[maxn];

void dfs(int v, int p) {
  if (p != -1) {
    es[v].erase(find(es[v].begin(), es[v].end(), p));
  }
  ds[v].clear();

  cnt[v] = (s[v] == '1');
  for (int u : es[v]) {
    dfs(u, v);
    ds[v].pb(mxds[0][u].back() + 1);
    cnt[v] += cnt[u];
  }
  mxds[0][v].clear();
  mxds[0][v].pb(0);
  for (int i = 0; i < sz(es[v]); ++i) {
    mxds[0][v].pb(max(mxds[0][v].back(), ds[v][i]));
  }

  mxds[1][v].clear();
  mxds[1][v].pb(0);
  for (int i = sz(es[v]) - 1; i >= 0; --i) {
    mxds[1][v].pb(max(mxds[1][v].back(), ds[v][i]));
  }
}

int up[maxn];

long long res;

bool once;

void dfs2(int v) {
  int mxs[2] = {0, 0};
  for (int i = 0; i <= sz(es[v]); ++i) {
    int cur;
    if (i == sz(es[v])) {
      if (!v) {
        continue;
      }
      cur = up[v];
    } else {
      cur = ds[v][i];
    }
    if (mxs[1] < cur) {
      mxs[1] = cur;
      if (mxs[0] < mxs[1]) {
        swap(mxs[0], mxs[1]);
      }
    }
  }
  /*eprintf("v = %d, mxs = {%d>=%d}\n", v, mxs[0], mxs[1]);
  eprintf("up[%d] = %d\n", v, up[v]);
  for (int it = 0; it < sz(es[v]); ++it) {
    eprintf("%d(u=%d)%c", ds[v][it], es[v][it], " \n"[it == sz(es[v]) - 1]);
  }*/
  {
    int from = inf;
    if (s[v] == '1') {
      from = 0;
    } else {
      for (int it = 0; it < sz(es[v]); ++it) {
        int u = es[v][it];
        if (cnt[u]) {
          from = min(from, ds[v][it]);
        }
      }
      if (cnt[0] > cnt[v]) {
        from = min(from, up[v]);
      }
    }
    res += max(0, mxs[1] + 1 - from);
  }
  if (mxs[0] > mxs[1]) {
    bool can = s[v] == '1';
    for (int it = 0; it < sz(es[v]); ++it) {
      if (ds[v][it] < mxs[0] && cnt[es[v][it]]) {
        can = 1;
        break;
      }
    }
    if (cnt[0] > cnt[v] && up[v] < mxs[0]) {
      can = 1;
    }

    if (can) {
      if (mxs[0] == mxs[1] + 1) {
        once = 1;
      } else {
        ++res;
      }
    }
  }
  for (int it = 0; it < sz(es[v]); ++it) {
    int u = es[v][it];
    up[u] = 1 + max(up[v], max(mxds[0][v][it], mxds[1][v][sz(es[v]) - 1 - it]));
    dfs2(u);
  }
}

void solve() {
  ds.resize(n);
  mxds[0].resize(n);
  mxds[1].resize(n);
  dfs(0, -1);

  up[0] = 0;
  res = 0;
  once = 0;
  dfs2(0);
  res += once;
  printf("%lld\n", res);
  //exit(0);
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}