Morning Walk
InterviewTime limit3sMemory limit256 MB
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 places connected by 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 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 .
The second line gives a string of length consisting of 1s and 0s. If the -th character is 1, place is indoors; if it is 0, place is outdoors.
From the third line to the -th line, the -th line gives two integers , representing each edge of the tree. This means the -th edge connects vertex and vertex .
Output
Print the number of possible distinct walk routes.
Constraints
- The input structure is guaranteed to form a valid tree.