malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j, k, q, w, x, y, flag = 0, sum = 0;     char c;     while (true) {       c = getchar();       if (c ==   n ) break;       sum = sum * 10 + int(c -  0 );       q = sum / 2;       sum = sum % 2;     }     if (sum == 1 || q % 2 == 1)       cout <<  0 n ;     else       cout <<  4 n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 5;   int deg[N];   vector<int> adj[N];   int e[N][3];   int seen[N];   int dfs(int u) {     seen[u] = true;     if (deg[u] == 1) return u;     for (int i = 0; i < adj[u].size(); i++)       if (!seen[adj[u][i]]) return dfs(adj[u][i]);   }   int main() {     int n;     cin >> n;     if (n == 2) {       int x;       cin >> x >> x >> x;       cout <<  YES n ;       cout << 2 <<   n ;       cout <<  1 2 13 n ;       cout <<  1 2   << x - 13;       return 0;     }     if (n == 3) {       cout <<  NO n ;       return 0;     }     for (int i = 0; i < n - 1; i++) {       int u, v, val;       cin >> u >> v >> val;       u--, v--;       deg[u]++;       deg[v]++;       adj[u].push_back(v);       adj[v].push_back(u);       e[i][0] = u;       e[i][1] = v;       e[i][2] = val;     }     for (int i = 0; i < n; i++)       if (deg[i] == 2) {         cout <<  NO n ;         return 0;       }     vector<pair<int, pair<int, int>>> ans;     for (int i = 0; i < n - 1; i++) {       int u, v, val;       u = e[i][0];       v = e[i][1];       val = e[i][2];       if (deg[u] == 1 || deg[v] == 1) {         if (deg[v] == 1) swap(u, v);         int w, z;         memset(seen, 0, sizeof seen);         seen[v] = true;         vector<int> adj_temp;         for (int i = 0; adj_temp.size() < 2; i++)           if (adj[v][i] != u) adj_temp.push_back(adj[v][i]);         w = dfs(adj_temp[0]);         z = dfs(adj_temp[1]);         u++, w++, z++;         ans.push_back({u, {w, val / 2}});         ans.push_back({w, {z, -val / 2}});         ans.push_back({u, {z, val / 2}});       } else {         int x, y, w, z;         vector<int> u_adj, v_adj;         for (int i = 0; u_adj.size() < 2; i++)           if (adj[u][i] != v) u_adj.push_back(adj[u][i]);         for (int i = 0; v_adj.size() < 2; i++)           if (adj[v][i] != u) v_adj.push_back(adj[v][i]);         memset(seen, 0, sizeof seen);         seen[v] = seen[u] = true;         x = dfs(u_adj[0]);         y = dfs(u_adj[1]);         w = dfs(v_adj[0]);         z = dfs(v_adj[1]);         x++, y++, w++, z++;         ans.push_back({x, {w, val / 2}});         ans.push_back({x, {y, -val / 2}});         ans.push_back({w, {z, -val / 2}});         ans.push_back({y, {z, val / 2}});       }     }     cout <<  YES n ;     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); i++)       cout << ans[i].first <<     << ans[i].second.first <<                << ans[i].second.second <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<ll, ll>;   const ll MOD = 1e9 + 7, N = 2e5 + 10;   vector<ll> g[N];   ll vis[N];   ll n, m, k;   void bfs(ll s, vector<ll> &dist) {     for (ll i = 1; i < n + 1; i++) {       vis[i] = 0;     }     dist[s] = 0;     queue<pii> q;     q.push({s, 0});     while (!q.empty()) {       ll nd = q.front().first, st = q.front().second;       q.pop();       if (vis[nd]) continue;       vis[nd] = 1;       dist[nd] = min(dist[nd], st);       for (auto &v : g[nd]) {         q.push({v, st + 1});       }     }   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m >> k;     vector<ll> spec;     spec.assign(k, 0);     for (ll i = 0; i < k; i++) {       cin >> spec[i];     }     ll u, v;     for (ll i = 0; i < m; i++) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     vector<ll> dist_src(n + 2, INT_MAX), dist_dest(n + 2, INT_MAX);     bfs(1, dist_src);     bfs(n, dist_dest);     vector<pii> tmp;     for (auto &i : spec) {       tmp.push_back({dist_dest[i] - dist_src[i], i});     }     sort(tmp.begin(), tmp.end());     ll ans = 0;     for (ll i = 1; i < k; i++) {       ll a = tmp[i - 1].second, b = tmp[i].second;       ll path =           min(dist_src[a] + dist_dest[b] + 1, dist_src[b] + dist_dest[a] + 1);       if (path >= dist_src[n]) {         cout << dist_src[n] <<   n ;         return 0;       }       ans = max(ans, path);     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int str2int(string s) {     istringstream ss(s);     int t;     ss >> t;     return t;   }   int main() {     string s;     cin >> s;     int ans = -1;     for (int i = 1; i < s.length(); i++) {       string s1 = s.substr(0, i);       if (s1.length() > 1 && s1[0] ==  0 ) continue;       int t1 = str2int(s1);       if (t1 > 1000000) continue;       for (int j = 1; j < s.length(); j++) {         if (i + j >= s.length()) continue;         string s2 = s.substr(i, j);         string s3 = s.substr(i + j);         if (s2.length() > 1 && s2[0] ==  0 ) continue;         if (s3.length() > 1 && s3[0] ==  0 ) continue;         int t2 = str2int(s2);         int t3 = str2int(s3);         if (t2 > 1000000) continue;         if (t3 > 1000000) continue;         int temp = t1 + t2 + t3;         if (temp > ans) ans = temp;       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   inline int calc(int x, int y, char c) {     if (c ==  & ) return x & y;     if (c ==  | ) return x | y;     return x ^ y;   }   int work() {     char c;     scanf( %c , &c);     if (c ==  ( ) {       int l = work();       char op;       scanf( %c , &op);       int r = work(), res = 0;       scanf( %*c );       for (int i = 0; i < 4; i++) {         if (!(l & (1 << i))) continue;         for (int j = 0; j < 4; j++) {           if (!(r & (1 << j))) continue;           res |= 1 << calc(i, j, op);         }       }       return res;     }     if (c ==  ? ) return 6;     return c ==  0  ? 1 : 8;   }   int main() {     scanf( %*d n );     puts(work() & 6 ?  YES  :  NO );     return 0;   }
#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   const long double pi = acos(-1.0);   const long double eps = 1e-5;   const int INF = 0x3f3f3f3f;   const long long INFLL = 0x3f3f3f3f3f3f3f3f;   int a[300010];   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int T;     cin >> T;     while (T--) {       int n;       cin >> n;       map<int, int> l;       map<int, int> r;       for (int i = (1); i <= (n); i++) {         cin >> a[i];         if (!l[a[i]]) l[a[i]] = i;         r[a[i]] = i;       }       int ans = l.size();       int p = 0;       int cnt = 0;       int res = 0;       for (auto it : l) {         if (p < it.second)           cnt++;         else           cnt = 1;         p = r[it.first];         res = max(res, cnt);       }       cout << ans - res <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007LL;   long long large = 2000000000000000000LL;   int main() {     int n, m;     cin >> n >> m;     vector<vector<int> > adj(n, vector<int>());     set<pair<int, int> > s;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       if (x > y) swap(x, y);       s.insert(pair<int, int>(x, y));       adj[x].push_back(y);       adj[y].push_back(x);     }     int u = -1, v = -1;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         if (s.find(pair<int, int>(i, j)) == s.end()) {           u = i;           v = j;           break;         }       }     }     if (u == -1) {       cout <<  NO  << endl;       return 0;     }     cout <<  YES  << endl;     vector<vector<int> > adjt(n, vector<int>());     for (int i = 0; i < n; i++) {       int f = i;       if (f == v) f = u;       for (int j = 0; j < (int)adj[i].size(); j++) {         int to = adj[i][j];         if (to == v) to = u;         adjt[f].push_back(to);       }     }     queue<int> q;     q.push(u);     vector<int> dis(n, -1);     dis[u] = 0;     while (!q.empty()) {       int u = q.front();       q.pop();       for (int j = 0; j < (int)adjt[u].size(); j++) {         int v = adjt[u][j];         if (dis[v] == -1) {           dis[v] = dis[u] + 1;           q.push(v);         }       }     }     vector<pair<int, int> > e;     for (int i = 0; i < n; i++) {       if (i != v) e.push_back(pair<int, int>(dis[i], i));     }     sort(e.begin(), e.end());     vector<int> a(n, -1);     for (int i = 0; i < (int)e.size(); i++) a[e[i].second] = n - 2 - i;     a[v] = n - 1;     for (int i = 0; i < n; i++) printf( %d  , a[i] + 1);     printf(  n );     a[v] = n - 2;     for (int i = 0; i < n; i++) printf( %d  , a[i] + 1);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   typedef long long arr64[200020];   int n, tn, tS, qS = 1;   pair<int, long long> pr[200020];   arr64 pX, pY, K, B;   int main() {     long long s;     scanf( %d%I64d , &n, &s);     for (int i = 1; i <= n; i++) scanf( %d%d , &pr[i].first, &pr[i].second);     sort(pr + 1, pr + n + 1);     pr[++n] = make_pair((int)1e9, s);     for (int i = 1; i <= n; i++)       if (i == 1 || pr[i].first > pr[i - 1].first) {         while (tn > 0 && pr[i].second <= pr[tn].second) tn--;         pr[++tn] = pr[i];       }     K[tS = 1] = pr[1].first;     for (int i = 2; i <= tn; i++) {       qS = min(qS, tS);       while (qS < tS &&              (((pr[i].second - B[qS + 1]) / (K[qS + 1]) +                ((pr[i].second - B[qS + 1]) % (K[qS + 1]) != 0)) <                   ((pr[i].second - B[qS]) / (K[qS]) +                    ((pr[i].second - B[qS]) % (K[qS]) != 0)) ||               ((pr[i].second - B[qS + 1]) / (K[qS + 1]) +                ((pr[i].second - B[qS + 1]) % (K[qS + 1]) != 0)) ==                       ((pr[i].second - B[qS]) / (K[qS]) +                        ((pr[i].second - B[qS]) % (K[qS]) != 0)) &&                   (K[qS + 1] * ((pr[i].second - B[qS + 1]) / (K[qS + 1]) +                                 ((pr[i].second - B[qS + 1]) % (K[qS + 1]) != 0)) +                    B[qS + 1] - pr[i].second) >=                       (K[qS] * ((pr[i].second - B[qS]) / (K[qS]) +                                 ((pr[i].second - B[qS]) % (K[qS]) != 0)) +                        B[qS] - pr[i].second)))         qS++;       long long cX = ((pr[i].second - B[qS]) / (K[qS]) +                       ((pr[i].second - B[qS]) % (K[qS]) != 0)),                 cK = pr[i].first;       long long cY = (K[qS] * ((pr[i].second - B[qS]) / (K[qS]) +                                ((pr[i].second - B[qS]) % (K[qS]) != 0)) +                       B[qS] - pr[i].second),                 cB = cY - cK * cX;       if (i == tn) printf( %I64d n , cX);       while (tS > 1 && cK * pX[tS - 1] + cB >= pY[tS - 1]) tS--;       K[++tS] = cK, B[tS] = cB;       pX[tS - 1] = (((B[tS - 1] - B[tS])) / ((K[tS] - K[tS - 1])) +                     (((B[tS - 1] - B[tS])) % ((K[tS] - K[tS - 1])) != 0));       pY[tS - 1] = cK * pX[tS - 1] + cB;     }   }
#include <bits/stdc++.h>   using namespace std;   const long long int N = 200010;   const long long mod = 1000000007;   const long long inf = 1000000000000000000;   const long double pi = 3.14159265358979323846264338;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n;     cin >> n;     vector<long long int> a(n);     for (long long int i = 0; i < n; i++) {       cin >> a[i];     }     sort((a).begin(), (a).end());     long long int ans = 0, cnt = 1;     for (long long int i = 0; i < n; i++) {       if (a[i] >= cnt) ans++, cnt++;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   T sqr(T x) {     return x * x;   }   template <typename T>   T abs(T x) {     return x < 0 ? -x : x;   }   template <typename T>   T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   const int MOD = (int)1e+9 + 7;   int n;   vector<pair<int, int> > a;   vector<int> f;   int powmod(const int a, const int b) {     if (b == 0) {       return 1;     }     long long r = powmod(a, b >> 1);     (r *= r) %= MOD;     return b & 1 ? r * a % MOD : r;   }   int divmod(const int a, const int b) {     return a * (long long)powmod(b, MOD - 2) % MOD;   }   int C(int n, int m) { return divmod(divmod(f[n], f[m]), f[n - m]); }   int main(int argc, char **argv) {     ios_base::sync_with_stdio(false);     cin >> n;     f.resize(n + 1);     f[0] = 1;     for (int i = 1; i <= n; ++i) {       f[i] = f[i - 1] * (long long)i % MOD;     }     for (int i = 1; i <= n; ++i) {       int x;       cin >> x;       if (x) {         a.push_back(make_pair(x, i));       }     }     sort(a.begin(), a.end());     if (a.size() == 0) {       cout << powmod(2, n - 1) << endl;       return 0;     }     int ans = 0;     for (int d = -1; d <= 1; d += 2) {       int l = a[0].second + d * (a[0].first - 1), r = a[0].second;       if (l > r) {         swap(l, r);       }       if (l < 1 || n < r || (l == r && d == 1)) {         continue;       }       long long res = 1;       for (size_t i = 1; i < a.size(); ++i) {         bool f = false;         for (int j = -1; j <= 1; j += 2) {           int L = a[i].second + j * (a[i].first - 1), R = a[i].second + j;           if (L > R) {             swap(L, R);           }           if (L < 1 || n < R || R < r || l < L) {             continue;           }           (res *= C(l - L + R - r, l - L)) %= MOD;           f = true;           l = L;           r = R;           if (r < a[i].second) {             ++r;           } else {             --l;           }           break;         }         if (!f) {           res = 0;           break;         }       }       if (res) {         int L = 1, R = n;         (res *= C(l - L + R - r, l - L)) %= MOD;       }       (ans += res) %= MOD;     }     if (a[0].first > 1) {       ans = ans * (long long)powmod(2, a[0].first - 2) % MOD;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const int N = 110;   struct T {     int bits;     int x;     int k;   };   bool cmp(const T& a, const T& b) { return a.k < b.k; }   T a[N];   long long dp[(1 << 20) + 4];   int n, m;   long long b;   long long Ans() {     const long long INF = 1LL << 60;     long long res = 0;     long long ans = INF;     int top = 1 << m;     for (int i = 0; i < top; ++i) dp[i] = 1LL << 60;     dp[0] = 0;     res = a[0].k * b;     dp[a[0].bits] = a[0].x;     ans = min(ans, dp[top - 1] + res);     for (int i = 1; i < n; ++i) {       res = a[i].k * b;       int& bits = a[i].bits;       for (int j = top - 1; j >= 0; --j)         if (dp[j] != INF) {           dp[j | bits] = min(dp[j | bits], dp[j] + a[i].x);         }       ans = min(ans, dp[top - 1] + res);     }     if (ans == INF) ans = -1;     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> b;     int s, t;     for (int i = 0; i < n; ++i) {       cin >> a[i].x >> a[i].k >> s;       a[i].bits = 0;       for (int j = 0; j < s; ++j) {         cin >> t;         a[i].bits |= 1 << (t - 1);       }     }     sort(a, a + n, cmp);     cout << Ans() << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long n, a, b;   int main() {     cin >> n >> a >> b;     for (long long tx = 0; tx <= n; tx += a) {       if ((n - tx) % b == 0) {         cout <<  YES  << endl << tx / a <<     << (n - tx) / b << endl;         return 0;       }     }     cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char str[100010];   int dp[26][100010];   int maxP[260][260][260];   int len[3];   int idx[3];   char desc[3][260];   int get(int rel) {     int m = maxP[idx[0] - (rel == 0)][idx[1] - (rel == 1)][idx[2] - (rel == 2)];     if (m == 100010) return 100010;     return dp[desc[rel][idx[rel] - 1] -  a ][m];   }   int main(void) {     setbuf(stdout, 0);     int N, Q;     cin >> N >> Q;     cin >> str;     for (int i = 0; i < 26; i++) dp[i][N] = 100010;     for (int i = 0; i < N; i++) {       for (int j = 0; j < 26; j++) {         if (str[N - 1 - i] ==  a  + j)           dp[j][N - 1 - i] = N - i;         else           dp[j][N - 1 - i] = dp[j][N - i];       }     }     for (int q = 0; q < Q; q++) {       char o, c;       int i;       cin >> o >> i;       i--;       if (o ==  + ) {         cin >> c;         desc[i][len[i]++] = c;         idx[i] = len[i];         if (i == 0) {           for (int j = 0; j <= len[1]; j++) {             idx[1] = j;             for (int k = 0; k <= len[2]; k++) {               idx[2] = k;               maxP[len[0]][j][k] = get(i);               if (j) maxP[len[0]][j][k] = min(maxP[len[0]][j][k], get(1));               if (k) maxP[len[0]][j][k] = min(maxP[len[0]][j][k], get(2));             }           }         }         if (i == 1) {           for (int j = 0; j <= len[0]; j++) {             idx[0] = j;             for (int k = 0; k <= len[2]; k++) {               idx[2] = k;               maxP[j][len[1]][k] = get(i);               if (j) maxP[j][len[1]][k] = min(maxP[j][len[1]][k], get(0));               if (k) maxP[j][len[1]][k] = min(maxP[j][len[1]][k], get(2));             }           }         }         if (i == 2) {           for (int j = 0; j <= len[0]; j++) {             idx[0] = j;             for (int k = 0; k <= len[1]; k++) {               idx[1] = k;               maxP[j][k][len[2]] = get(i);               if (j) maxP[j][k][len[2]] = min(maxP[j][k][len[2]], get(0));               if (k) maxP[j][k][len[2]] = min(maxP[j][k][len[2]], get(1));             }           }         }       } else         len[i]--;       cout << (maxP[len[0]][len[1]][len[2]] < 100010 ?  YES n  :  NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void task();   int main() {     task();     return 0;   }   const int N = (int)1e5 + 10;   const int MODULO = (int)1e9 + 3;   const long long int INF = (long long int)1e18;   const int BUF_SIZE = (int)1e5 + 10;   bool used[110];   int n, k;   int a[110], b[110];   long long int cat[110][110];   void comp_cat() {     cat[0][0] = 0;     for (int i = 1; i < 101; ++i) {       cat[i][0] = cat[i][i] = 1;       for (int j = 1; j < i; ++j) {         cat[i][j] = cat[i - 1][j - 1] + cat[i - 1][j];       }     }   }   void task() {     scanf( %d %d , &n, &k);     int cnt = n / k;     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     double ansmin = 100500, ansmax = -1;     int q;     scanf( %d , &q);     while (q--) {       for (int i = 0; i < cnt; ++i) scanf( %d , &b[i]);       int sum = 0;       if (used[b[0]]) {         continue;       }       for (int i = 0; i < cnt; ++i) {         sum += a[b[i]];         used[b[i]] = 1;       }       ansmin = min(ansmin, (double)sum / cnt);       ansmax = max(ansmax, (double)sum / cnt);     }     vector<int> v;     for (int i = 1; i <= n; ++i)       if (!used[i]) v.push_back(a[i]);     sort(v.begin(), v.end());     int sz = v.size();     (void(1));     if (sz > n % k) {       int sum = 0;       for (int i = 0; i < cnt; ++i) {         sum += v[i];       }       ansmin = min(ansmin, (double)sum / cnt);       sum = 0;       for (int i = sz - 1; i >= sz - cnt; --i) {         sum += v[i];       }       ansmax = max(ansmax, (double)sum / cnt);     }     printf( %.10f %.10f , ansmin, ansmax);   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = INT_MAX, df = 3e5 + 7;   long long i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, dp[df], pos[df],       a[df];   inline long long read() {     long long x = 0, y = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) y = (ch ==  - ) ? -1 : 1, ch = getchar();     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     return x * y;   }   bool chk(long long x) {     dp[1] = 0;     for (long long i = (2); i <= (n); ++i) {       dp[i] = dp[i - 1];       if (dp[i - 1] >= pos[i] - 1) dp[i] = max(dp[i], pos[i] + x);       if (dp[i - 1] >= pos[i] - x - 1) dp[i] = max(dp[i], pos[i]);       if (dp[i - 2] >= pos[i] - x - 1 && i > 2)         dp[i] = max(dp[i], pos[i - 1] + x);     }     if (dp[n] >= m - x - 1) return 1;     dp[2] = max(pos[2], x);     for (long long i = (3); i <= (n); ++i) {       dp[i] = dp[i - 1];       if (dp[i - 1] >= pos[i] - 1) dp[i] = max(dp[i], pos[i] + x);       if (dp[i - 1] >= pos[i] - x - 1) dp[i] = max(dp[i], pos[i]);       if (dp[i - 2] >= pos[i] - x - 1) dp[i] = max(dp[i], pos[i - 1] + x);     }     if (dp[n] >= min(m - 1, m + pos[2] - x - 1)) return 1;     return 0;   }   int main() {     m = read();     n = read();     long long now = 1;     for (long long i = (1); i <= (n); ++i) a[i] = read(), a[i + n] = a[i] + m;     if (n == 1) {       return printf( %lld n , m - 1), 0;     }     for (long long i = (2); i <= (n); ++i)       if (a[now + 1] - a[now] < a[i + 1] - a[i]) now = i;     long long le = 0, rig = a[now + 1] - a[now] - 1, mid = 0, ans = 0;     for (long long i = (1); i <= (n); ++i) pos[i] = a[i + now];     long long cur = pos[1];     for (long long i = (1); i <= (n); ++i) pos[i] -= cur;     while (le <= rig) {       mid = (le + rig) >> 1;       if (chk(mid))         ans = mid, rig = mid - 1;       else         le = mid + 1;     }     return printf( %lld n , ans), 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   bool chmin(T& a, const T& b) {     return a > b ? a = b, true : false;   }   template <class T>   bool chmax(T& a, const T& b) {     return a < b ? a = b, true : false;   }   const long long mod = 1e9 + 7, N = 2e5 + 5;   inline long long msb(long long val) {     return sizeof(long long) * 8 - __builtin_clzll(val) - 1;   }   long long a[N], n, m, k;   vector<pair<long long, long long> > edges[N];   bool check(long long a) {     while (a > 0) {       if (a % 10 != 4 && a % 10 != 7) return false;       a /= 10;     }     return true;   }   struct des {     vector<long long> p, rnk, sz;     void init() {       p.assign(n + 1, 0);       rnk.assign(n + 1, 0);       sz.assign(n + 1, 1);       for (long long i = 0; i < n; i++) p[i] = i;     }     long long par(long long u) {       if (p[u] == u)         return u;       else         return p[u] = par(p[u]);     }     bool issame(long long u, long long v) { return (par(u) == par(v)); }     void merge(long long u, long long v) {       u = par(u);       v = par(v);       if (u == v) return;       if (rnk[u] > rnk[v]) {         p[v] = u;         sz[u] += sz[v];         rnk[u]++;       } else {         p[u] = v;         sz[v] += sz[u];         rnk[v]++;       }     }     long long get_size(long long u) { return sz[par(u)]; }   } d;   void solve(long long test_case) {     long long i, j;     cin >> n;     d.init();     for (i = 1; i < n; i++) {       long long u, v, w;       cin >> u >> v >> w;       if (!check(w)) d.merge(u, v);     }     long long ans = 0;     for (i = 1; i <= n; i++) {       long long t = n - d.get_size(i);       ans += t * (t - 1);     }     cout << ans <<   n ;     return;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     solve(1);     return 0;   }