F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Tree

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 836    Accepted Submission(s): 326


Problem Description
Given a tree of $n$ vertices and $n-1$ edges. Each vertex $i$ has a color $c_i$ = 'a' or 'b' or 'c'.

Please count the number of simple path between $i$ and $j$ satisfy :

* $1 \le i \le j \le n$

* The number of vertices with three different colors is equal on the path.
 

Input
The first line contains one integer $n$ ($ n \le 10^5 $).

The second line contains a string S with length of $n$ . ($S_i$ represents the color of $i$-th vertex,$S_i = \ 'a' \ or \ 'b' \ or \ 'c'$)

Each of the next $n-1$ lines contains a pair of vertex indices $u_i$ and $v_i$ ($1\le u_i,v_i \le n$)— endpoints of the corresponding edge.
 

Output
Output an integer represent the answer.
 

Sample Input
6 abbccb 1 2 1 3 1 4 1 5 4 6
 

Sample Output
5
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-19 10:20:44, Gzip enabled