CF1681F.Unique Occurrences

提高+/省选-

通过率:0%

时间限制:6.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given a tree, consisting of nn vertices. Each edge has an integer value written on it.

Let f(v,u)f(v, u) be the number of values that appear exactly once on the edges of a simple path between vertices vv and uu.

Calculate the sum of f(v,u)f(v, u) over all pairs of vertices vv and uu such that 1≤v<u≤n1 \le v \lt u \le n.

给你一棵包含 nn 个顶点的树,每条边上写有一个整数。

令 f(v,u)f(v, u) 表示在顶点 vv 与 uu 之间的简单路径上的边中,恰好出现一次的数值的个数。

请计算所有满足 1≤v<u≤n1 \le v \lt u \le n 的顶点对 (v,u)(v, u) 对应的 f(v,u)f(v, u) 之和。

输入格式

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

Each of the next n−1n-1 lines contains three integers v,uv, u and xx (1≤v,u,x≤n1 \le v, u, x \le n) — the description of an edge: the vertices it connects and the value written on it.

The given edges form a tree.

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

接下来的 n−1n-1 行每行包含三个整数 vv、uu 和 xx(1≤v,u,x≤n1 \le v, u, x \le n)——一条边的描述:该边所连接的两个顶点,以及边上所写的数值。

给定的边构成一棵树。

输出格式

Print a single integer — the sum of f(v,u)f(v, u) over all pairs of vertices vv and uu such that v<uv \lt u.

输出一个整数——对所有满足 v<uv \lt u 的顶点对 (v,u)(v, u),求 f(v,u)f(v, u) 的总和。

输入输出样例

  • 输入#1

    3
    1 2 1
    1 3 2

    输出#1

    4
  • 输入#2

    3
    1 2 2
    1 3 2

    输出#2

    2
  • 输入#3

    5
    1 4 4
    1 2 3
    3 4 4
    4 5 5

    输出#3

    14
  • 输入#4

    2
    2 1 1

    输出#4

    1
  • 输入#5

    10
    10 2 3
    3 8 8
    4 8 9
    5 8 5
    3 10 7
    7 8 2
    5 6 6
    9 3 4
    1 6 3

    输出#5

    120

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

首页