Yet Another Contest 1 P2 - A Boring Problem

View as PDF

Submit solution


Points: 10 (partial)
Time limit: 1.0s
Java 2.0s
Python 2.0s
Memory limit: 256M
Java 512M
Python 512M

Authors:
Problem types

You are given a tree containing N nodes where each node is coloured black or white. The i-th edge is bidirectional and connects the nodes ui and vi. Find the number of simple paths containing at least three nodes of the same colour.

Note that traversing a path from either end counts as the same path.

Constraints

3N2×105

1ui,viN,uivi

It is guaranteed that the graph described in the input is a tree.

Subtask 1 [10%]

All nodes are black.

Subtask 2 [10%]

The graph is linear. More specifically, for 1i<N, ui=i and vi=i+1.

Subtask 3 [80%]

No additional constraints.

Input Specification

The first line of input contains a single integer, N.

The second line of input contains a string of N characters, each character being either B or W. The i-th node is black if the i-th character is B, and white otherwise.

The following N1 lines of input contain two space-separated integers ui and vi, representing that there is a bidirectional edge between ui and vi in the tree.

Output Specification

Output a single integer, representing the number of simple paths containing at least three nodes of the same colour.

Sample Input

Copy
5
BBBWB
1 2
4 2
5 2
1 3

Sample Output

Copy
4

Explanation

The simple paths in the graph with at least three nodes of the same colour are the paths between nodes:

  • 1 and 5
  • 2 and 3
  • 3 and 4
  • 3 and 5

Comments

There are no comments at the moment.