Making UCPC
Time limit3sMemory limit1024 MB
Given a tree whose vertices are labeled U, C, or P, count pairs (a, b) whose path letters can be rearranged into (UCPC)^k for some k >= 1.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Combinatorics, Math
- Solved
- No attempts yet
Problem
A tree with vertices is given. Each vertex of the tree is numbered from 1 to and has one of the letters U, C, P written on it. Count the number of ordered pairs satisfying the condition below.
- After collecting the letters written on all vertices on the simple path from vertex to vertex , they can be rearranged to form a string of the form
UCPC. ()
Input
The first line gives the number of vertices .
The next line gives a string of length consisting only of the letters U, C, P.
The next lines give two integers and separated by a space. This means that vertices and are directly connected in the tree.
Output
Print the number of ordered pairs satisfying the condition.
Hint
The tree for sample 1 is shown below.

Figure J.1: the tree for sample 1
Using the path between vertex 1 and vertex 4, or between vertex 1 and vertex 5, a string of the form (UCPC) can be made.
In sample 2, two strings of the form (UCPC) and one string of the form (UCPCUCPC) can be made.