CF1923E.Count Paths

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree, consisting of nn vertices, numbered from 11 to nn . Every vertex is colored in some color, denoted by an integer from 11 to nn .

A simple path of the tree is called beautiful if:

  • it consists of at least 22 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 xx to yy is the same as the path from yy to xx ).

输入格式

The first line contains a single integer tt ( 1t1041 \le t \le 10^4 ) — the number of testcases.

The first line of each testcase contains a single integer nn ( 2n21052 \le n \le 2 \cdot 10^5 ) — the number of vertices in the tree.

The second line contains nn integers c1,c2,,cnc_1, c_2, \dots, c_n ( 1cin1 \le c_i \le n ) — the color of each vertex.

The ii -th of the next n1n - 1 lines contains two integers viv_i and uiu_i ( 1vi,uin1 \le v_i, u_i \le n ; viuiv_i \neq u_i ) — the ii -th edge of the tree.

The given edges form a valid tree. The sum of nn over all testcases doesn't exceed 21052 \cdot 10^5 .

输出格式

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
首页