CF1923E.Count Paths
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree, consisting of n vertices, numbered from 1 to n . Every vertex is colored in some color, denoted by an integer from 1 to n .
A simple path of the tree is called beautiful if:
- it consists of at least 2 vertices;
- the first and the last vertices of the path have the same color;
- no other vertex on the path has the same color as the first vertex.
Count the number of the beautiful simple paths of the tree. Note that paths are considered undirected (i. e. the path from x to y is the same as the path from y to x ).
输入格式
The first line contains a single integer t ( 1≤t≤104 ) — the number of testcases.
The first line of each testcase contains a single integer n ( 2≤n≤2⋅105 ) — the number of vertices in the tree.
The second line contains n integers c1,c2,…,cn ( 1≤ci≤n ) — the color of each vertex.
The i -th of the next n−1 lines contains two integers vi and ui ( 1≤vi,ui≤n ; vi=ui ) — the i -th edge of the tree.
The given edges form a valid tree. The sum of n over all testcases doesn't exceed 2⋅105 .
输出格式
For each testcase, print a single integer — the number of the beautiful simple paths of the tree.
输入输出样例
输入#1
4 3 1 2 1 1 2 2 3 5 2 1 2 1 2 1 2 1 3 3 4 4 5 5 1 2 3 4 5 1 2 1 3 3 4 4 5 4 2 2 2 2 3 1 3 2 3 4
输出#1
1 3 0 3