This page is still under construction.

Parts of this page are still being built. What you see may change.

Morning Walk

Interview

Time limit3sMemory limit256 MB

Summary
Given a tree where each vertex is indoor or outdoor, count ordered pairs of distinct indoor vertices whose tree path has no other indoor vertex.
Level

Medium5 of 10

Topics
Tree, DFS, Combinatorics, Graph
Solved
No attempts yet

Problem

Seohyun enjoys morning walks, and she wants to keep enjoying them after entering Seoul Science High School. She analyzed the school's layout for her walks, and found she could simplify it into a tree with NN places connected by N−1N-1 paths. Because the structure is a tree, every place can be reached from every other place by way of some paths.

A morning walk is defined by choosing a start point and an end point, then walking along the simple path on the tree from the start point to the end point (a path that does not pass through the same point more than once). The path between two points on a tree is unique, so once the start point and end point are chosen, the path is determined uniquely.

Among the NN places, some are indoors and the rest are outdoors. Seohyun does not want to exercise before the walk starts, so both the start point and the end point of the walk must be indoors. Also, because seeing an indoor place during the walk makes her want to stop walking, there must be no indoor place on the walk path other than the start point and the end point.

Seohyun wants to walk a different route every day. Let us find how many distinct walk routes there are.

Input

The first line gives the number of vertices NN.

The second line gives a string AA of length NN consisting of 1s and 0s. If the ii-th character AiA_i is 1, place ii is indoors; if it is 0, place ii is outdoors.

From the third line to the N+1N+1-th line, the i+2i+2-th line gives two integers uiu_i, viv_i representing each edge of the tree. This means the ii-th edge connects vertex uiu_i and vertex viv_i.

Output

Print the number of possible distinct walk routes.

Constraints

  • 2≤N≤2×1052 \le N \le 2 \times 10^5
  • 1≤ui,vi≤N1 \le u_i , v_i \le N
  • ui≠viu_i \neq v_i
  • The input structure is guaranteed to form a valid tree.

Examples1

  1. Example 1

    Input
    5
    10111
    1 2
    2 3
    2 4
    4 5
    
    Expected output
    8