Petya loves lucky numbers. We all know that lucky numbers is the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers , 744, 4 is lucky and 5, and 467 is 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 it weight is a lucky number. Note that A tree with n vertexes is an undirected connected graph the has exactly n -1 edges.
Petya wondered how many vertex triples (i, J, K) exists this on the the-the- i To J, as-well as on the the-on-the-the-same-from I-to- k There must is at least one lucky edge (All three vertexes is pairwise distinct). The order of numbers in the triple matters, which is, the triple (1, 2, 3) isn't equal to the triple (2, 1, 3) a nd is not a equal to the triple (1, 3, 2).
Find How many such triples of vertexes exist.
Input
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.
Output
On the single line print the single number-the answer.
%lld Specificator to read or write 64-bit numbers inс++. It is recommended to use the CIN, cout streams or the%i64d specificator.
Sample Test (s)input
4
1 2 4
3 1 2
1 4 7
Output
16
input
4
1 2 4
1 3 47
1 4 7447
Output
24
Note
The triples of vertexes from the first sample is:(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 =.
Test instructions
An edge, if the weight of this edge is only composed of digital 4,7, we say this edge is a lucky side, otherwise it is not
Now there is a tree, the edge of the right value, ask:
How many points does this tree have to (i,j,k) satisfy:
I!=j!=k,
and the path I to J passes at least one lucky edge, path I to K also passes at least one lucky edge
Attention:
Point Pairs (I,j,k) and (J,i,k) and (i,k,j) are different point pairs
This problem, the direct calculation of the point of satisfaction seems a bit troublesome, it is difficult to reverse, we first calculate the non-conforming point pair
Matching point pairs = Total point pairs-non-conforming point pairs
Total point to =n* (n-1) * (n-2)
Now consider non-conforming point pairs
Non-compliant points are divided into 2 types:
1.i to J and I to K2 paths do not contain lucky edges
2.2 Paths, one of which passed through the lucky side, the other without a lucky edge
This tree, according to the lucky side we can be divided into a number of Unicom block, here can be used and check set to achieve
The FIND_FA () function maintains the array sum at the same time
Sum[i] Indicates the number of nodes of the Unicom block where I resides
Then we enumerate the Unicom blocks
For each connected block:
2 paths have not been lucky side number: sum[i]* (sum[i]-1) * (sum[i]-2)
There is only one lucky edge: 2*sum[i]* (sum[i]-1) * (N-sum[i])
Add up and get all the non-conforming points.
Attention:
1. And check the set remember path compression
2. Each connected block is counted only once, do not repeat the calculation
Code:
#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>#defineLL Long Longusing namespacestd;Const intmaxn=1e5+5;intFA[MAXN]; LL SUM[MAXN];voidsolve ();intMain () {solve (); return 0;}//InitializevoidInitintN) { for(intI=1; i<=n;i++) {Fa[i]=i; Sum[i]=1; }}//Judging is not the lucky sideBOOLOkintW) { while(w) {intcur=w%Ten; if(cur!=4&& cur!=7) return false; W/=Ten; } return true;}//and check the set, note to path compression, note the update of the sum arrayintFIND_FA (intx) { if(fa[x]==x)returnx; intCur=Find_fa (fa[x]); Sum[cur]+=Sum[x]; SUM[X]=0; FA[X]=cur; returncur;}voidsolve () {intN; scanf ("%d",&N); Init (n); for(intI=1; i<n;i++){ intu,v,w; scanf (" %d%d%d",&u,&v,&W); if(!OK (w)) { intfau=find_fa (U); intfav=Find_fa (v); if(fau!=fav) {Fa[fau]=Fav; SUM[FAV]+=Sum[fau]; SUM[FAU]=0; } } } if(n<3) {printf ("0\n"); return ; } LL ans=0; for(intI=1; i<=n;i++){ if(FIND_FA (i) = =i) { if(sum[i]>2) ans+ = (sum[i]* (sum[i]-1) * (sum[i]-2)); Ans+=2ll*sum[i]* (sum[i]-1) * (nSum[i]); }} ans=n* (N-1LL) * (N-2LL)-ans; printf ("%i64d\n", ans); return ;}
CF109 C. Lucky Tree and check set