CF109C.Lucky Tree

普及/提高-

通过率:0%

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 nn 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 nn vertexes is an undirected connected graph that has exactly n1n-1 edges.

Petya wondered how many vertex triples (i,j,k)(i,j,k) exists that on the way from ii to jj , as well as on the way from ii to kk 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)(1,2,3) is not equal to the triple (2,1,3)(2,1,3) and is not equal to the triple (1,3,2)(1,3,2) .

Find how many such triples of vertexes exist.

输入格式

The first line contains the single integer nn ( 1<=n<=1051<=n<=10^{5} ) — the number of tree vertexes. Next n1n-1 lines contain three integers each: uiu_{i} viv_{i} wiw_{i} ( 1<=ui,vi<=n,1<=wi<=1091<=u_{i},v_{i}<=n,1<=w_{i}<=10^{9} ) — the pair of vertexes connected by the edge and the edge's weight.

输出格式

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.

输入输出样例

  • 输入#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 1616 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)(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: 432=244·3·2=24 .

首页