CF109C.Lucky Tree

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

One day Petya encountered a tree with n vertexes. Besides, the tree was weighted, i. e. each edge of the tree has weight (a positive integer). An edge is lucky if its weight is a lucky number. Note that a tree with n vertexes is an undirected connected graph that has exactly n - 1 edges.

Petya wondered how many vertex triples (i, j, k) exists that on the way from i to j, as well as on the way from i to k there must be at least one lucky edge (all three vertexes are pairwise distinct). The order of numbers in the triple matters, that is, the triple (1, 2, 3) is not equal to the triple (2, 1, 3) and is not equal to the triple (1, 3, 2).

Find how many such triples of vertexes exist.

佩佳喜欢幸运数字。众所周知,幸运数字是指十进制表示中仅包含幸运数字 4 和 7 的正整数。例如,47、744、4 是幸运数,而 5、17、467 不是。

某天,佩佳遇到了一棵含 $ n $ 个顶点的树。此外,这棵树是带权的,即树的每条边都有一个权值(为正整数)。若一条边的权值是幸运数,则称该边为幸运边。注意:含 $ n $ 个顶点的树是一个无向连通图,且恰好有 $ n-1 $ 条边。

佩佳思考:有多少个顶点三元组 $ (i,,j,,k) $,满足从 $ i $ 到 $ j $ 的路径上至少有一条幸运边,且从 $ i $ 到 $ k $ 的路径上也至少有一条幸运边?(三个顶点两两互异)
三元组中数字的顺序是有意义的,即三元组 $ (1,,2,,3) $ 与 $ (2,,1,,3) $ 不同,也与 $ (1,,3,,2) $ 不同。

请计算满足条件的顶点三元组的个数。

输入格式

The first line contains the single integer n (1 ≤ n ≤ 105) — the number of tree vertexes. Next n - 1 lines contain three integers each: u__i v__i w__i (1 ≤ u__i, v__i ≤ n, 1 ≤ w__i ≤ 109) — the pair of vertexes connected by the edge and the edge's weight.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)——树的顶点数量。接下来的 n−1n-1 行每行包含三个整数:uiu_i、viv_i、wiw_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,1≤wi≤1091 \leq w_i \leq 10^9)——表示由该边连接的两个顶点及其边权。

输出格式

On the single line print the single number — the answer.

Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is recommended to use the cin, cout streams or the %I64d specificator.

在单行中输出一个整数——即答案。

请勿在 C++ 中使用 %lld 格式说明符读取或写入 64 位整数。建议使用 cin、cout 流,或使用 %I64d 格式说明符。

输入输出样例

  • 输入#1

    4
    1 2 4
    3 1 2
    1 4 7

    输出#1

    16
  • 输入#2

    4
    1 2 4
    1 3 47
    1 4 7447

    输出#2

    24

说明/提示

The 16 triples of vertexes from the first sample are: (1, 2, 4), (1, 4, 2), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 2, 4), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2).

In the second sample all the triples should be counted: 4·3·2 = 24.

第一个样例中的 16 个顶点三元组为:(1, 2, 4)(1, 2, 4)、(1, 4, 2)(1, 4, 2)、(2, 1, 3)(2, 1, 3)、(2, 1, 4)(2, 1, 4)、(2, 3, 1)(2, 3, 1)、(2, 3, 4)(2, 3, 4)、(2, 4, 1)(2, 4, 1)、(2, 4, 3)(2, 4, 3)、(3, 2, 4)(3, 2, 4)、(3, 4, 2)(3, 4, 2)、(4, 1, 2)(4, 1, 2)、(4, 1, 3)(4, 1, 3)、(4, 2, 1)(4, 2, 1)、(4, 2, 3)(4, 2, 3)、(4, 3, 1)(4, 3, 1)、(4, 3, 2)(4, 3, 2)。

在第二个样例中,所有三元组均应被计入:4⋅3⋅2 = 244·3·2 = 24。

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

首页