CF1923E.Count Paths

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

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).

给你一棵包含 nn 个顶点的树,顶点编号为 11 到 nn。每个顶点均被染成某种颜色,颜色用 11 到 nn 之间的整数表示。

树中的一条简单路径被称为优美的,当且仅当满足以下条件:

  • 该路径至少包含 22 个顶点;
  • 路径的起点与终点顶点颜色相同;
  • 路径上除起点与终点外的其余所有顶点,其颜色均不同于起点(即终点)的颜色。

请计算该树中优美简单路径的数量。注意:路径是无向的(即从 xx 到 yy 的路径与从 yy 到 xx 的路径视为同一条路径)。

输入格式

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

The first line of each testcase contains a single integer nn (2≤n≤2⋅1052 \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 (1≤ci≤n1 \le c_i \le n) — the color of each vertex.

The ii-th of the next n−1n - 1 lines contains two integers viv_i and uiu_i (1≤vi,ui≤n1 \le v_i, u_i \le n; vi≠uiv_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 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 树中顶点的数量。

第二行包含 nn 个整数 c1,c2,…,cnc_1, c_2, \dots, c_n(1≤ci≤n1 \le c_i \le n)—— 每个顶点的颜色。

接下来的 n−1n - 1 行中,第 ii 行包含两个整数 viv_i 和 uiu_i(1≤vi,ui≤n1 \le v_i, u_i \le n;vi≠uiv_i \neq u_i)—— 树的第 ii 条边。

所给的边构成一棵合法的树。所有测试用例的 nn 值之和不超过 2⋅1052 \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

输入解题思路,AI测评打分。不知道怎么写?

首页