You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
typedef long long LL;
|
|
|
|
|
const int N = 1e6 + 10, M = N << 1;
|
|
|
|
|
|
|
|
|
|
// 链式前向星
|
|
|
|
|
int e[M], h[N], idx, w[M], ne[M];
|
|
|
|
|
void add(int a, int b, int c = 0) {
|
|
|
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LL s[N], f[N];
|
|
|
|
|
int sz[N];
|
|
|
|
|
int st[N];
|
|
|
|
|
|
|
|
|
|
void dfs1(int u) {
|
|
|
|
|
st[u] = 1;
|
|
|
|
|
sz[u] = 1; // u节点自己加入
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int v = e[i];
|
|
|
|
|
if (st[v]) continue;
|
|
|
|
|
// 先执行噢
|
|
|
|
|
dfs1(v);
|
|
|
|
|
// 统计u子树中节点数量
|
|
|
|
|
sz[u] += sz[v];
|
|
|
|
|
// 幸运边
|
|
|
|
|
if (w[i])
|
|
|
|
|
s[u] += sz[v]; // v子树中所有节点,都可以为s[u]贡献力量
|
|
|
|
|
else
|
|
|
|
|
s[u] += s[v]; // v这个点是指望不上的,它的子树中的贡献力量
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dfs2(int u) {
|
|
|
|
|
st[u] = 1;
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int v = e[i];
|
|
|
|
|
if (st[v]) continue;
|
|
|
|
|
if (w[i]) // 幸运边
|
|
|
|
|
f[v] = sz[1] - sz[v]; // 容斥原理
|
|
|
|
|
else
|
|
|
|
|
f[v] = f[u] + s[u] - s[v]; // 还是容斥原理吧~
|
|
|
|
|
// 最后执行噢
|
|
|
|
|
dfs2(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 幸运数字是由 4 和 7 组成的正整数
|
|
|
|
|
int check(int n) {
|
|
|
|
|
while (n) {
|
|
|
|
|
if (n % 10 != 4 && n % 10 != 7) return 0;
|
|
|
|
|
n /= 10;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
memset(h, -1, sizeof h);
|
|
|
|
|
int n;
|
|
|
|
|
cin >> n;
|
|
|
|
|
for (int i = 1; i < n; i++) { // n-1条边
|
|
|
|
|
int a, b, c;
|
|
|
|
|
cin >> a >> b >> c;
|
|
|
|
|
c = check(c); // 如果一条边的权值是一个幸运数字,那么我们就说这条边是一条幸运边
|
|
|
|
|
add(a, b, c), add(b, a, c);
|
|
|
|
|
}
|
|
|
|
|
memset(st, 0, sizeof st);
|
|
|
|
|
dfs1(1);
|
|
|
|
|
memset(st, 0, sizeof st);
|
|
|
|
|
dfs2(1);
|
|
|
|
|
|
|
|
|
|
LL ans = 0;
|
|
|
|
|
for (int i = 1; i <= n; i++) ans += s[i] * (s[i] - 1) + f[i] * (f[i] - 1) + s[i] * f[i] * 2;
|
|
|
|
|
printf("%lld\n", ans);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|