malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   stack<int> stk;   vector<pair<int, int> > edge[500010];   vector<pair<int, int> > w[500010];   int a[500010], b[500010], c[500010], fa[500010], rk[500010];   bool ans[500010];   int n, m, q;   int Find(int x) {     while (x != fa[x]) x = fa[x];     return x;   }   void Union(int x, int y, bool flag) {     x = Find(x);     y = Find(y);     if (rk[x] < rk[y]) swap(x, y);     if (flag) stk.push(y);     fa[y] = x;     if (rk[x] == rk[y]) ++rk[x];   }   void undo() {     while (!stk.empty()) {       fa[stk.top()] = stk.top();       stk.pop();     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) fa[i] = i;     for (int i = 1; i <= m; ++i) {       scanf( %d%d%d , &a[i], &b[i], &c[i]);       edge[c[i]].push_back(make_pair(a[i], b[i]));     }     scanf( %d , &q);     for (int i = 1; i <= q; ++i) {       int k;       scanf( %d , &k);       while (k--) {         int x;         scanf( %d , &x);         w[c[x]].push_back(make_pair(i, x));       }       ans[i] = true;     }     for (int i = 1; i < 500010; ++i) {       int last = -1;       for (auto &j : w[i])         if (ans[j.first]) {           int o = j.first, k = j.second;           if (o != last) undo(), last = o;           int x = a[k], y = b[k];           if (Find(x) == Find(y))             ans[o] = false;           else             Union(x, y, true);         }       undo();       for (auto &j : edge[i])         if (Find(j.first) != Find(j.second)) Union(j.first, j.second, false);     }     for (int i = 1; i <= q; ++i) puts(ans[i] ?  YES  :  NO );   }
#include <bits/stdc++.h>   using namespace std;   char board[3][3];   int cross, nought;   bool cross_win() {     if (board[0][0] ==  X  && board[0][1] ==  X  && board[0][2] ==  X )       return true;     if (board[1][0] ==  X  && board[1][1] ==  X  && board[1][2] ==  X )       return true;     if (board[2][0] ==  X  && board[2][1] ==  X  && board[2][2] ==  X )       return true;     if (board[0][0] ==  X  && board[1][0] ==  X  && board[2][0] ==  X )       return true;     if (board[0][1] ==  X  && board[1][1] ==  X  && board[2][1] ==  X )       return true;     if (board[0][2] ==  X  && board[1][2] ==  X  && board[2][2] ==  X )       return true;     if (board[0][0] ==  X  && board[1][1] ==  X  && board[2][2] ==  X )       return true;     if (board[0][2] ==  X  && board[1][1] ==  X  && board[2][0] ==  X )       return true;     return false;   }   bool nought_win() {     if (board[0][0] ==  0  && board[0][1] ==  0  && board[0][2] ==  0 )       return true;     if (board[1][0] ==  0  && board[1][1] ==  0  && board[1][2] ==  0 )       return true;     if (board[2][0] ==  0  && board[2][1] ==  0  && board[2][2] ==  0 )       return true;     if (board[0][0] ==  0  && board[1][0] ==  0  && board[2][0] ==  0 )       return true;     if (board[0][1] ==  0  && board[1][1] ==  0  && board[2][1] ==  0 )       return true;     if (board[0][2] ==  0  && board[1][2] ==  0  && board[2][2] ==  0 )       return true;     if (board[0][0] ==  0  && board[1][1] ==  0  && board[2][2] ==  0 )       return true;     if (board[0][2] ==  0  && board[1][1] ==  0  && board[2][0] ==  0 )       return true;     return false;   }   int main() {     for (int i = 0; i < 3; i++)       for (int j = 0; j < 3; j++) {         cin >> board[i][j];         if (board[i][j] ==  X ) cross++;         if (board[i][j] ==  0 ) nought++;       }     if (cross - nought > 1 || nought > cross) {       printf( illegal );       return 0;     }     if (cross_win()) {       if (nought >= cross)         printf( illegal );       else         printf( the first player won );       return 0;     }     if (nought_win()) {       if (cross > nought)         printf( illegal );       else         printf( the second player won );       return 0;     }     if (cross + nought == 9) {       printf( draw );       return 0;     }     if (cross - nought == 1) {       printf( second );       return 0;     }     if (cross == nought) {       printf( first );       return 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double a, d, l, e;   int n;   int main() {     scanf( %lf %lf , &a, &d);     scanf( %d , &n);     l = n * d + .5;     e = d;     double pos = 0, fa = 4 * a;     for (int i = 0; i < n; i++) {       pos = fmod(pos + e, fa);       if (pos <= a)         printf( %.10lf %.10lf n , pos, 0.);       else if (pos <= 2 * a)         printf( %.10lf %.10lf n , a, pos - a);       else if (pos <= 3 * a)         printf( %.10lf %.10lf n , a - (pos - 2 * a), a);       else         printf( %.10lf %.10lf n , 0., a - (pos - 3 * a));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void FAST_IO(string filein =   , string fileout =   , string fileerr =   ) {     if (fopen(filein.c_str(),  r )) {       freopen(filein.c_str(),  r , stdin);       freopen(fileout.c_str(),  w , stdout);     }     cin.tie(0), cout.tie(0)->sync_with_stdio(0);   }   void Hollwo_Pelw();   signed main() {     FAST_IO( hollwo_pelw.inp ,  hollwo_pelw.out );     int testcases = 1;     for (int test = 1; test <= testcases; test++) {       Hollwo_Pelw();     }     return 0;   }   const int N = 14, inf = 2e9;   inline bool smin(int &a, int b) { return a > b ? a = b, 1 : 0; }   int n, m, adj[N][N];   int path[N][N][1 << N], dp[1 << N];   int tr[1 << N], ps[1 << N], pe[1 << N];   void Hollwo_Pelw() {     cin >> n >> m;     for (int i = 0, u, v; i < m; i++)       cin >> u >> v, --u, --v, adj[u][v] = adj[v][u] = 1;     memset(path, -1, sizeof path);     for (int mask = 0; mask < 1 << n; mask++) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           if (mask >> i & 1) continue;           if (mask >> j & 1) continue;           if (__builtin_popcount(mask) == 1 && i == j) continue;           if ((i == j || adj[i][j]) && !mask) {             path[i][j][mask] = i;           } else {             for (int k = 0; k < n; k++) {               if ((mask >> k & 1) && path[i][k][mask ^ (1 << k)] >= 0 &&                   adj[k][j]) {                 path[i][j][mask] = k;                 break;               }             }           }         }       }     }     for (int mask = 1; mask < (1 << n); mask++) {       if (__builtin_popcount(mask) == 1) {         dp[mask] = 0;       } else {         dp[mask] = inf;         for (int sub = mask; sub; sub = (sub - 1) & mask) {           int nmask = mask ^ sub;           for (int i = 0; i < n; i++) {             for (int j = 0; j < n; j++) {               if ((nmask >> i & 1) && (nmask >> j & 1) && path[i][j][sub] >= 0) {                 if (smin(dp[mask], dp[nmask] + __builtin_popcount(sub) + 1))                   tr[mask] = sub, ps[mask] = i, pe[mask] = j;               }             }           }         }       }     }     cout << dp[(1 << n) - 1] <<   n ;     int cur = (1 << n) - 1;     while (__builtin_popcount(cur) > 1) {       int nxt = tr[cur], i = ps[cur], j = pe[cur];       cur ^= tr[cur];       do {         int k = path[i][j][nxt];         cout << k + 1 <<     << j + 1 <<   n ;         nxt ^= 1 << k, j = k;       } while (i != j);     }   }
#include <bits/stdc++.h>   using namespace std;   vector<int> adj[1000005];   char op[1000005];   bool A[1000005][2], val[1000005];   void dfs(int u) {     if (op[u] ==  I ) return;     dfs(adj[u][0]);     if (op[u] ==  N ) {       val[u] = !val[adj[u][0]];       return;     }     dfs(adj[u][1]);     if (op[u] ==  A )       val[u] = val[adj[u][0]] and val[adj[u][1]];     else if (op[u] ==  O )       val[u] = val[adj[u][0]] or val[adj[u][1]];     else       val[u] = val[adj[u][0]] xor val[adj[u][1]];   }   void dfs2(int u, int p) {     if (u == 1)       A[1][1 - val[1]] = 1 - val[1];     else {       if (op[p] ==  N )         A[u][1 - val[u]] = A[p][val[u]];       else {         int v;         if (adj[p][0] == u)           v = adj[p][1];         else           v = adj[p][0];         if (op[p] ==  A )           A[u][1 - val[u]] = A[p][val[v] and (1 - val[u])];         else if (op[p] ==  O )           A[u][1 - val[u]] = A[p][val[v] or (1 - val[u])];         else           A[u][1 - val[u]] = A[p][val[v] xor (1 - val[u])];       }     }     for (auto x : adj[u]) dfs2(x, u);   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, i, u, v;     string s;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> s;       op[i] = s[0];       if (s ==  AND  or s ==  OR  or s ==  XOR ) {         cin >> u >> v;         adj[i].push_back(u);         adj[i].push_back(v);       } else if (s ==  NOT ) {         cin >> u;         adj[i].push_back(u);       } else {         cin >> u;         val[i] = u;       }     }     dfs(1);     for (i = 1; i <= n; i++) A[i][val[i]] = val[1];     dfs2(1, 0);     for (i = 1; i <= n; i++)       if (op[i] ==  I ) cout << A[i][1 - val[i]];     return 0;   }
#include<bits/stdc++.h>   using namespace std;   int T,n,m,flag,k,a[110],b[110],ans[300];   int main()   {    scanf( %d ,&T);    while (T--) {     scanf( %d%d%d ,&k,&n,&m);     for (int i=1;i<=n;i++) scanf( %d ,&a[i]);     for (int i=1;i<=m;i++) scanf( %d ,&b[i]);     flag=1;     for (int i=1,j=1;i<=n||j<=m;) {      if ((a[i]>k||i>n)&&(b[j]>k||j>m)) {       flag=0;       break;      }      while (a[i]<=k&&i<=n) {       ans[i+j-1]=a[i];       if (a[i]==0) k++;       i++;      }      while (b[j]<=k&&j<=m) {       ans[i+j-1]=b[j];       if (b[j]==0) k++;       j++;      }     }     if (flag) {      for (int i=1;i<=n+m;i++) printf( %d  ,ans[i]);      printf(  n );     } else {      printf( -1 n );     }    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool X[1000001], vis[1000001];   int main() {     int n;     while (scanf( %d , &n) == 1) {       vector<int> V;       int x;       for (int i = 0; i < n; i++) {         scanf( %d , &x);         X[x] = true;         V.push_back(x);       }       deque<int> Q;       for (int i = 1; i < 1000001; i++)         if (!X[i] && !X[1000001 - i]) Q.push_front(i), Q.push_back(1000001 - i);       printf( %d n , n);       for (int i = 0; i < V.size(); i++) {         if (vis[V[i]]) continue;         if (!X[1000001 - V[i]])           printf( %d n , 1000001 - V[i]);         else {           printf( %d %d n , Q.front(), Q.back());           vis[1000001 - V[i]] = true;           Q.pop_front();           Q.pop_back();         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long int n, h, m, k;   vector<long long int> freight;   int workingTrains(long long int time) {     if (time - k >= 0) {       return n - (lower_bound(freight.begin(), freight.end(), time) -                   upper_bound(freight.begin(), freight.end(), time - k));     } else {       return upper_bound(freight.begin(), freight.end(),                          (time - k + m / 2) % (m / 2)) -              lower_bound(freight.begin(), freight.end(), time);     }   }   void solve(int test_index) {     cin >> n >> h >> m >> k;     freight.resize(n);     vector<long long int> raw(n);     for (int i = 0; i < n; i++) {       long long int hh, mm;       cin >> hh >> mm;       freight[i] = (hh * m + mm) % (m / 2);       raw[i] = freight[i];     }     sort(freight.begin(), freight.end());     long long int t = freight[0], works = workingTrains(freight[0]);     for (int time : freight) {       long long int w = workingTrains(time);       if (works < w) {         works = w;         t = time;       }     }     cout << n - works <<     << t <<   n ;     for (int i = 0; i < n; i++) {       if (t - k < 0) {         if (raw[i] < t || raw[i] > (t - k + m / 2) % (m / 2)) {           cout << i + 1 <<    ;         }       } else {         if (raw[i] > t - k && raw[i] < t) {           cout << i + 1 <<    ;         }       }     }     cout <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int num_tests = 1;     for (int test_index = 1; test_index <= num_tests; test_index++) {       solve(test_index);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void optimizeIO() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   int main() {     optimizeIO();     int n, t;     cin >> n >> t;     string s1, s2, s3;     cin >> s1 >> s2;     s3 = s1;     int sa = 0;     vector<int> v, v2;     for (typeof(n) i = 0; i < n; i++) {       if (s1[i] == s2[i]) {         sa++;         v.push_back(i);       } else         v2.push_back(i);     }     if (n - sa < 2 * (n - t - sa))       cout <<  -1 n ;     else {       if ((n - t) < sa) {         int i1 = 0, i2 = 0;         int dd = n - t;         for (typeof(n) i = 0; i < n; i++) {           if (v[i1] == i && dd) {             s3[i] = s1[i];             dd--;             i1++;           } else {             if (abs(s1[i] - s2[i]) > 1) {               s3[i] = min(s1[i], s2[i]) + 1;             } else {               if (max(s1[i], s2[i]) ==  z )                 s3[i] =  a ;               else                 s3[i] =  z ;             }           }         }       } else {         int i1 = 0;         int dd = n - t, c = 0;         int d1 = n - t - sa, d2 = n - t - sa;         for (typeof(n) i = 0; i < n; i++) {           if (i1 < int((v).size())) {             if (v[i1] == i) {               s3[i] = s1[i];               i1++;               continue;             }           }           if (d1) {             s3[i] = s1[i];             d1--;             continue;           }           if (d2) {             s3[i] = s2[i];             d2--;             continue;           }           if (d1 == 0 && d2 == 0) {             if (abs(s1[i] - s2[i]) > 1) {               s3[i] = min(s1[i], s2[i]) + 1;             } else {               if (max(s1[i], s2[i]) ==  z )                 s3[i] =  a ;               else                 s3[i] =  z ;             }           }         }       }       cout << s3 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   const int BIT = 17;   int n, m;   int P[N];   int pot2[33];   set<int> S[18];   set<int>::iterator it;   long long sol;   inline int find(int bit, int pos) {     if (S[bit].empty()) return 0;     it = S[bit].lower_bound(pos);     if (it == S[bit].begin()) return 0;     it--;     return *it + 1;   }   inline int find2(int bit, int pos) {     if (S[bit].empty()) return n - 1;     it = S[bit].lower_bound(pos);     if (it == S[bit].end()) return n - 1;     return *it - 1;   }   inline void load() {     pot2[0] = 1;     for (int i = 1; i <= BIT; i++) pot2[i] = (pot2[i - 1] << 1);     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i++) {       int x;       scanf( %d , &P[i]);       x = P[i];       for (int j = BIT; j >= 0; j--) {         if (x & (1 << j)) {           sol += (long long)pot2[j] * (i - find(j, i) + 1);         } else           S[j].insert(i);       }     }   }   inline void print() {     printf( SADRZAJ NA MJESTU == %d n , 1);     for (set<int>::iterator it = S[0].begin(); it != S[0].end(); it++) {       printf( %d  , *it);     }     printf(  n );     system( pause );   }   inline void solve() {     for (; m; m--) {       int pos, val;       scanf( %d %d , &pos, &val);       pos--;       for (int j = BIT; j >= 0; j--) {         if (val & (1 << j)) {           if ((P[pos] & (1 << j)) == 0) {             S[j].erase(pos);             sol += (long long)pot2[j] * (pos - find(j, pos) + 1) *                    (find2(j, pos) - pos + 1);           }         } else {           if ((P[pos] & (1 << j))) {             int gore = pos - find(j, pos) + 1;             int dole = find2(j, pos) - pos;             sol -= (long long)pot2[j] * dole * gore;             sol -= (long long)pot2[j] * gore;             S[j].insert(pos);           }         }       }       P[pos] = val;       cout << sol << endl;     }   }   int main(void) {     load();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Maxn = 65536 + 10;   int n, deg[Maxn], s[Maxn];   vector<pair<int, int> > ans;   set<pair<int, int> > mys;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d%d , &deg[i], &s[i]);       mys.insert(make_pair(deg[i], i));     }     while (!mys.empty()) {       pair<int, int> p = *mys.begin();       mys.erase(p);       if (p.first > 0) {         ans.push_back(make_pair(p.second, s[p.second]));         mys.erase(make_pair(deg[s[p.second]], s[p.second]));         deg[s[p.second]]--;         s[s[p.second]] ^= p.second;         mys.insert(make_pair(deg[s[p.second]], s[p.second]));       }     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++)       printf( %d %d  n , ans[i].first, ans[i].second);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   const double eps = 1e-10;   int main() {     int n;     scanf( %d , &n);     int feet, inches;     inches = n / 3;     if (n - inches * 3 > 1) inches++;     feet = inches / 12;     inches %= 12;     cout << feet <<     << inches << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100005, mod = 1e9 + 7, M = 100;   int n, m, k;   int q[N][6], a[N][6], r[6], f[6], e[6];   inline void inq(int id, int x) {     while (f[id] <= e[id] && a[x][id] >= a[q[e[id]][id]][id]) e[id]--;     q[++e[id]][id] = x;   }   inline void outq(int id, int x) {     if (q[f[id]][id] <= x) f[id]++;   }   inline int jug() {     int sum = 0;     for (int i = 1; i <= m; i++) {       sum += a[q[f[i]][i]][i];     }     return sum > k;   }   inline int read() {     int x = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x;   }   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         a[i][j] = read();       }     }     for (int i = 1; i <= m; i++) f[i] = 0, e[i] = -1;     int res = 0;     int pre = 1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         inq(j, i);       }       while (pre <= i && jug()) {         for (int j = 1; j <= m; j++) outq(j, pre);         pre++;       }       if (res < i - pre + 1) {         res = i - pre + 1;         for (int j = 1; j <= m; j++) r[j] = a[q[f[j]][j]][j];       }     }     for (int i = 1; i <= m; i++) {       cout << r[i] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1555;   const int dx[4] = {0, 0, 1, -1};   const int dy[4] = {1, -1, 0, 0};   int n, m;   template <typename T>   inline T mod(const T& lhs, const T& rhs) {     T _lhs = lhs;     while (_lhs < 0) {       _lhs += rhs;     }     return _lhs % rhs;   }   char gra[MAXN][MAXN];   bool used[MAXN][MAXN];   pair<int, int> visited[MAXN][MAXN];   queue<pair<int, int> > Q;   bool bfs(int x, int y) {     pair<int, int> now(x, y);     Q.push(now);     visited[now.first][now.second] = now;     used[x][y] = 1;     while (!Q.empty()) {       now = Q.front();       Q.pop();       for (int i = 0; i < 4; i++) {         int tx = dx[i] + now.first, ty = dy[i] + now.second;         int rx = mod(tx, n), ry = mod(ty, m);         if (gra[rx][ry] ==  # ) {           continue;         }         if (!used[rx][ry]) {           used[rx][ry] = 1;           Q.push(pair<int, int>(tx, ty));           visited[rx][ry] = pair<int, int>(tx, ty);         } else {           if (visited[rx][ry].first != tx || visited[rx][ry].second != ty) {             return true;           }         }       }     }     return false;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %s , gra[i]);     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (gra[i][j] ==  S ) {           if (bfs(i, j)) {             puts( Yes );           } else {             puts( No );           }           return 0;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   T MAX(T a, T b) {     return a > b ? a : b;   }   template <class T>   T MIN(T a, T b) {     return a < b ? a : b;   }   template <class T1>   void deb(T1 p) {     cout <<  Debugging:   << p << endl;   }   template <class T1, class T2>   void deb(T1 p, T2 q) {     cout <<  Debugging:   << p <<   t  << q << endl;   }   template <class T1, class T2, class T3>   void deb(T1 p, T2 q, T3 r) {     cout <<  Debugging:   << p <<   t   << q <<   t   << r << endl;   }   template <class T1, class T2, class T3, class T4>   void deb(T1 p, T2 q, T3 r, T4 s) {     cout <<  Debugging:   << p <<   t   << q <<   t   << r <<   t   << s << endl;   }   long long int POOW(int b, int p) {     if (p == 0) return 1;     return b * POOW(b, p - 1);   }   const int xx[] = {0, 0, 1, -1, -1, 1, -1, 1};   const int yy[] = {1, -1, 0, 0, 1, 1, -1, -1};   const int kx[] = {-2, -1, 1, 2, 2, 1, -1, -2};   const int ky[] = {1, 2, 2, 1, -1, -2, -2, -1};   int main() {     char mat[105][105];     int i, j, m, sum, res = 0, ar[105], n, k;     scanf( %d , &n);     getchar();     for (i = 0; i < n; i++) gets(mat[i]);     res = sum = 0;     for (i = 0; i < n; i++) {       sum = 0;       for (j = 0; j < n; j++) {         for (k = 0; k < n; k++) {           if (mat[i][k] == mat[j][k])             ;           else {             break;           }         }         if (k == n) sum++;       }       res = max(res, sum);     }     sum = 0;     for (i = 0; i < n; i++) {       for (j = k = 0; j < n; j++) {         if (mat[i][j] ==  1 ) ++k;       }       if (k == n) sum++;     }     if (res == 0) res = max(res, sum);     printf( %d n , res);     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( inline , 2)   using namespace std;   const long long N = 5005;   const long long inf = 1e18;   long long n, hd[N], nxt[N << 1], to[N << 1], w[N << 1], tot = 1;   long long sz[N << 1];   long long aa[N << 1], eg[N << 1];   void add(long long a, long long b, long long c) {     nxt[++tot] = hd[a], to[tot] = b, w[tot] = c;     hd[a] = tot;   }   long long dfs1(long long u, long long fe, long long d) {     long long re = d;     for (long long i = hd[u]; i; i = nxt[i])       if (i != fe) re += dfs1(to[i], i ^ 1, d + w[i]);     return eg[fe] = re;   }   long long dfs2(long long u, long long fe) {     long long re = 1;     for (long long i = hd[u]; i; i = nxt[i])       if (i != fe) re += dfs2(to[i], i ^ 1);     sz[fe ^ 1] = n - re;     return sz[fe] = re;   }   void solve(long long rt) {     memset(eg, 0, sizeof eg);     long long sum = dfs1(rt, 0, 0);     for (long long i = 2; i <= tot; ++i) aa[i] = min(aa[i], sum - eg[i]);   }   long long zz[N];   long long wi[N], wo[N], bb[N << 1];   void dfs3(long long u, long long fe) {     zz[u] = 1;     wi[u] = 0, wo[u] = 0;     for (long long i = hd[u]; i; i = nxt[i])       if (i != fe) {         dfs3(to[i], i ^ 1);         zz[u] += zz[to[i]];         wi[u] += wi[to[i]];         wo[u] += wo[to[i]] + zz[to[i]] * w[i];         wi[u] -= (wo[to[i]] + zz[to[i]] * w[i]) * zz[to[i]];       }     wi[u] += wo[u] * zz[u];     bb[fe] = wi[u];   }   signed main() {     memset(aa, 0x3f, sizeof aa);     cin >> n;     for (long long i = 1; i <= n - 1; ++i) {       long long x, y, z;       cin >> x >> y >> z;       add(x, y, z);       add(y, x, z);     }     for (long long i = 1; i <= n; ++i) dfs3(i, 0);     for (long long i = 1; i <= n; ++i) solve(i);     dfs2(1, 0);     long long ans = 1e18;     for (long long i = 1; i <= n - 1; ++i) {       long long r = i << 1, s = i << 1 | 1;       ans = min(ans, 1LL * w[r] * sz[r] * sz[s] + aa[r] * sz[r] + aa[s] * sz[s] +                          bb[r] + bb[s]);     }     cout << ans << endl;   }