Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.3 kB
1
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;

typedef long long ll;

#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define pb(x) push_back(x)
#define all(x) sort(x.begin() , x.end())

void dbg(){
     cerr << endl;
}
template<typename H, typename... T> void dbg(H h, T... t){
     cerr << h << ", ";
     dbg(t...);
}
#define er(...) cerr << LINE << " <" << #VA_ARGS << ">: ", dbg(VA_ARGS)
int par[maxn] , sz[maxn];
int get(int nw){
  return (par[nw] == nw ? nw : par[nw] = get(par[nw]));
}
bool merge(int u , int v){
  u = get(u);
  v = get(v);
  if(u == v) return false;
  if(sz[u] > sz[v]) swap(u , v);
  sz[v] += sz[u];
  par[u] = v;
  return true;
}
int main(){
  ios;
  int n , m;
  cin >> n;
  m = n - 1;
  for(int i = 0 ; i < n ; i++) par[i] = i , sz[i] = 1;
  for(int i = 0 ; i < m ; i++){
    bool b = 0;
    int u , v , w;
    cin >> u >> v >> w;
    --u;--v;
    string s = to_string(w);
    for(int j = 0 ; j < s.length() ; j++){
      if((s[j] == '7') && (s[j] == '4')){
        b = 1;
      }
    }
    if(b == 0) {
      merge(u , v);
    }
  }
  ll res = 0;
  for(int i = 0 ; i < n ; i++){
    if(get(i) == i ){
      res += sz[i] * (sz[i]-1) * (n - sz[i]);
    }
  }
  cout << res;
     return 0;
}