malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long n, m, s, t, l, rest;   long long d[1010], d2[1010], w[10010];   bool zeredg[10010];   pair<long long, long long> edg[10010];   vector<vector<long long> > edglist;   void dijkstra(long long dist[], bool sec) {     set<pair<long long, long long> > najbl;     dist[s] = 0;     najbl.insert({0, s});     for (long long i = 0; i < n; ++i) {       if (s != i) {         dist[i] = 1e18;         najbl.insert({dist[i], i});       }     }     while (najbl.size()) {       long long v = najbl.begin()->second;       najbl.erase(najbl.begin());       for (auto i : edglist[v]) {         long long u = edg[i].first + edg[i].second - v;         if (sec and dist[u] > dist[v] + w[i] and zeredg[i] and             w[i] < d[u] + rest - dist[v]) {           w[i] = d[u] + rest - dist[v];         }         if (dist[u] > dist[v] + w[i]) {           najbl.erase({dist[u], u});           dist[u] = dist[v] + w[i];           najbl.insert({dist[u], u});         }       }     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m >> l >> s >> t;     vector<long long> vi;     edglist.resize(n, vi);     for (long long i = 0; i < m; ++i) {       long long u, v, e;       cin >> u >> v >> e;       edglist[u].push_back(i);       edglist[v].push_back(i);       edg[i] = {u, v};       if (e) {         w[i] = e;       } else {         w[i] = 1;         zeredg[i] = 1;       }     }     dijkstra(d, 0);     if (d[t] > l) {       cout <<  NO ;       return 0;     } else if (d[t] == l) {       cout <<  YES n ;       for (long long i = 0; i < m; ++i) {         cout << edg[i].first <<     << edg[i].second <<     << w[i] <<   n ;       }       return 0;     }     rest = l - d[t];     dijkstra(d2, 1);     if (d2[t] != l) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     for (long long i = 0; i < m; ++i) {       cout << edg[i].first <<     << edg[i].second <<     << w[i] <<   n ;     }   }
#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   int a[200007];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     sort(a, a + n);     int c = n / 2 - 1;     int l = 0, r = n - 1;     int mn = 1e9 + 77;     for (int i = 0; i <= c; i++) {       mn = min(mn, a[r - c + i] - a[l + i]);     }     cout << mn;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct edge {     int target;     int weight;     int o;   };   struct vertice {     long long time;     int city;     int edge_ID;     int edge_W;   };   struct mysort {     bool operator()(const vertice &l, const vertice &r) {       if (l.time == r.time)         return l.edge_W < r.edge_W;       else         return l.time < r.time;     }   };   bool sort_pair(const pair<int, long long> &l, const pair<int, long long> &r) {     return l.first < r.first;   }   int main() {     int n, m;     cin >> n >> m;     vector<vector<edge> > E(n + 1);     for (int i = 0; i < m; i++) {       edge a, b;       cin >> a.target >> b.target >> a.weight;       b.weight = a.weight;       a.o = b.o = i + 1;       E[a.target].push_back(b);       E[b.target].push_back(a);     }     vertice v;     cin >> v.city;     v.time = 0;     v.edge_ID = 0;     v.edge_W = 0;     vector<pair<int, long long> > used_edges;     vector<int> visited(n + 1, 0);     int counter = 0;     multiset<vertice, mysort> S;     S.insert(v);     while (counter < n) {       multiset<vertice, mysort>::iterator it = S.begin();       if (visited[it->city] == 0) {         visited[it->city] = 1;         counter++;         used_edges.push_back(pair<int, long long>(it->edge_ID, it->edge_W));         for (int i = 0; i < E[it->city].size(); i++) {           vertice c;           c.time = it->time + E[it->city][i].weight;           c.city = E[it->city][i].target;           c.edge_ID = E[it->city][i].o;           c.edge_W = E[it->city][i].weight;           S.insert(c);         }       }       S.erase(it);     }     sort(used_edges.begin(), used_edges.end(), sort_pair);     long long ans = 0;     for (int i = 1; i < used_edges.size(); i++) ans += used_edges[i].second;     cout << ans << endl;     for (int i = 1; i < used_edges.size(); i++)       cout << used_edges[i].first <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   const int MAX_N = (int)3e5 + 123;   const long long inf = (long long)1e18 + 123;   using namespace std;   char ans[2][30];   string second;   pair<long long, long long> do_it(int l, int r) {     int len = r - l;     int last = 13 - (len + 1) / 2;     pair<long long, long long> res = make_pair(last, (len % 2));     swap(res.first, res.second);     last--;     for (int i = 1, id = last, down = 1; i <= len; i++) {       if (down) {         if (id + 1 < 13)           id++;         else           down = 0, id = 12;       } else         id--;       ans[down][id] = second[l + i - 1];     }     return res;   }   int main() {     cin >> second;     for (int i = 0; i + 1 < int((second).size()); i++)       if (second[i] == second[i + 1]) {         cout <<  Impossible ;         return 0;       }     int l = -1, r = -1;     for (int i = 0; i < int((second).size()); i++)       for (int j = i + 1; j < int((second).size()); j++)         if (second[i] == second[j]) l = i, r = j;     pair<long long, long long> hhey = do_it(l, r);     for (int len = l - 1, id = hhey.second, down = 1; len >= 0; len--) {       if (down) {         if (id)           id--;         else           id = 0, down = 0;       } else         id++;       ans[down][id] = second[len];     }     for (int len = r + 1, id = (hhey.second + hhey.first), up = 1;          len < int((second).size()); len++) {       if (up) {         if (id)           id--;         else           id = 0, up = 0;       } else         id++;       ans[up ^ 1][id] = second[len];     }     for (int i = 0; i < 2; i++) {       for (int j = 0; j < 13; j++) cout << ans[i][j];       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline long long min2(long long a, long long b) {     return (a) < (b) ? (a) : (b);   }   inline long long max2(long long a, long long b) {     return (a) > (b) ? (a) : (b);   }   inline long long max3(long long a, long long b, long long c) {     return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));   }   inline long long min3(long long a, long long b, long long c) {     return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));   }   pair<long long int, long long int> fsi(pair<long long int, long long int> a,                                          pair<long long int, long long int> b) {     pair<long long int, long long int> x;     if (max(a.first, b.first) > min(a.second, b.second)) {       x.first = 10000000000002;       x.second = 10000000000002;     } else {       x.first = max(a.first, b.first);       x.second = min(a.second, b.second);     }     return x;   }   void process() {     long long int n, m, l, h, pt, ct;     pair<long long int, long long int> p, curr;     string ans;     ans =  YES ;     bool and_found = false;     pt = 0;     cin >> n >> m;     p.first = m;     p.second = m;     for (long long int i = 0; i < n; i++) {       cin >> ct >> curr.first >> curr.second;       if (!and_found) {         p.first -= ct - pt;         p.second += ct - pt;         p = fsi(p, curr);         if (p.first == 10000000000002) {           and_found = true;           ans =  NO ;         }         pt = ct;       }     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int t;     cin >> t;     ;     while (t--) process();   }
#include <bits/stdc++.h>      using namespace std;      typedef long long ll;      int main()   {    int T;    scanf( %d , &T);    while (T--) {     ll n, m, ans = 0;     scanf( %lld%lld , &n, &m);     for (ll k = 1; k <= n / k; k++) {      ans += max(0ll, min(n / k - 1, m) - k);     }     printf( %lld n , ans);    }    return 0;   }   /*   1   50 3   */
#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n + 1];       int sum = 0;       for (int i = 1; i <= n; i++) {         cin >> a[i];         sum += a[i];       }       if (sum % n != 0)         cout << -1 << endl;       else {         vector<tuple<int, int, int>> ans;         int avg = sum / n;         for (int i = 2; i <= n; i++) {           int p = a[i];           a[i] %= i;           if (a[i] > 0) {             ans.push_back({1, i, i - a[i]});             p += (i - a[i]);           }           ans.push_back({i, 1, p / i});           a[i] = 0;         }         a[1] = sum;         for (int i = 2; i <= n; i++) {           int x = avg - a[i];           if (x > 0) {             ans.push_back({1, i, x});             a[1] -= x;           }         }         cout << ans.size() << endl;         for (auto aa : ans) {           cout << get<0>(aa) <<     << get<1>(aa) <<     << get<2>(aa) << endl;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     int G[n + 1][m + 1];     memset(G, 0, sizeof(G));     for (int l = 0; l < k; l++) {       int i, j;       cin >> i >> j;       G[i][j] = 1;       if ((i > 1 && j > 1 && G[i - 1][j] && G[i][j - 1] && G[i - 1][j - 1]) ||           (i <= n - 1 && j <= m - 1 && G[i + 1][j] && G[i][j + 1] &&            G[i + 1][j + 1]) ||           (i > 1 && j <= m - 1 && G[i - 1][j] && G[i][j + 1] &&            G[i - 1][j + 1]) ||           (i <= n - 1 && j > 1 && G[i + 1][j] && G[i][j - 1] &&            G[i + 1][j - 1])) {         cout << l + 1 << endl;         return 0;       }     }     cout << 0 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int d[][2] = {{1, -1}, {1, 1}, {-1, -1}, {-1, 1}};   const char* ds[] = { DL ,  DR ,  UL ,  UR };   int gdi(const char* s) {     int i = -1;     while (++i < 4) {       if (!strcmp(s, ds[i])) return i;     }   }   bool sv[4] = {false};   int fdi(const int dr, const int dc) {     int i = -1;     while (++i < 4 && !(dr == d[i][0] && dc == d[i][1]))       ;     return i;   }   void pr1(map<pair<int, int>, bool>& mp) {     map<pair<int, int>, bool>::iterator it = mp.begin();     while (it != mp.end()) {       printf( %d, %d n , it->first.first, it->first.second);       ++it;     }     printf( ------------ n );   }   int main() {     int n, m, xs, ys, di;     char drs[3];     scanf( %d%d%d%d%s , &n, &m, &xs, &ys, drs);     const int n1 = n - 1, m1 = m - 1;     --xs, --ys;     const int xy = ((xs + ys) & 1);     di = gdi(drs);     int dr = d[di][0], dc = d[di][1], cr = xs, cc = ys;     if (n == m && n > 2) {       if ((((!xs && !ys) || (xs == n1 && ys == n1)) && dr * dc > 0) ||           (((!xs && ys == m1) || (xs == n1 && !ys)) && dr * dc < 0)) {         printf( -1 n );         return 0;       }     }     map<pair<int, int>, bool> mp;     int i = -1;     while (++i < n) {       pair<int, int> p1(i, 0), p2(i, m1);       if (((p1.first + p1.second) & 1) == xy) mp[p1] = false;       if (((p2.first + p2.second) & 1) == xy) mp[p2] = false;     }     i = -1;     while (++i < m) {       pair<int, int> p1(0, i), p2(n1, i);       if (((p1.first + p1.second) & 1) == xy) mp[p1] = false;       if (((p2.first + p2.second) & 1) == xy) mp[p2] = false;     }     int nm = mp.size();     long long pn = 1;     pair<int, int> pc(xs, ys);     map<pair<int, int>, bool>::iterator it = mp.find(pc);     if (it != mp.end()) {       it->second = true;       --nm;     }     while (nm) {       int d1, d2;       if (dr < 0) {         d1 = cr;         if (dc > 0) {           d2 = m1 - cc;         } else {           d2 = cc;         }       } else {         d1 = n1 - cr;         if (dc > 0) {           d2 = m1 - cc;         } else {           d2 = cc;         }       }       if (d1 > d2) {         d1 = d2;         cr += d1 * dr;         cc += d1 * dc;         dc = -dc;       } else if (d1 < d2) {         cr += d1 * dr;         cc += d1 * dc;         dr = -dr;       } else {         cr += d1 * dr;         cc += d2 * dc;         dr = -dr;         dc = -dc;       }       pn += d1;       pc.first = cr, pc.second = cc;       it = mp.find(pc);       if (it != mp.end() && !it->second) {         --nm;         it->second = true;       }       if (nm && cr == xs && cc == ys) {         int j = fdi(dr, dc);         if (sv[j]) {           printf( -1 n );           return 0;         } else {           sv[j] = true;         }       }     }     printf( %I64d n , pn);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const long long N = 1e6 + 1;   long long pre[N], sub[N];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     string str;     cin >> str;     long long a = 0, b = 0, c = 0;     for (long long i = 0; i < str.size(); i++) {       if (str[i] ==  o ) {         b += a;       } else if (i > 0 && str[i - 1] ==  v ) {         a++;         c += b;       }     }     cout << c <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int sum;     int adj[6];     node() {       sum = 0;       memset(adj, -1, sizeof adj);     }   };   int n, m;   vector<node> T;   map<string, int> S;   void add(string x) {     int cur = 0;     for (int i = 0; i < x.size(); i++) {       int c = x[i] -  a ;       if (T[cur].adj[c] == -1) {         T.push_back(node());         T[cur].adj[c] = T.size() - 1;         cur = T.size() - 1;       } else {         cur = T[cur].adj[c];       }       if (i == x.size() - 1) T[cur].sum++;     }   }   int ask(string x) {     string y =   ;     for (int i = 0; i < x.size(); i++) {       if (x[i] ==  ? ) continue;       y += x[i];     }     bool take = true;     if (S.find(y) != S.end()) {       take = false;     } else {       S[y] = 1;     }     int cur = 0;     int ans = 0;     int i = 0;     for (i = 0; i < x.size(); i++) {       if (x[i] ==  ? ) {         string y = x;         for (char a =  a ; a <=  e ; a++) {           y[i] = a;           ans += ask(y);         }         if (i == x.size() - 1 && take) {           ans += T[cur].sum;         }         continue;       }       int c = x[i] -  a ;       if (T[cur].adj[c] != -1) {         cur = T[cur].adj[c];       } else {         break;       }       if (i == x.size() - 1 && take) {         ans += T[cur].sum;       }     }     return ans;   }   int main() {     cin >> n >> m;     T.push_back(node());     for (int i = 0; i < n; i++) {       string x;       cin >> x;       add(x);     }     int ans = 0;     for (int i = 0; i < m; i++) {       S.clear();       ans = 0;       string x;       cin >> x;       ans += ask(x);       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[100009], mark[100009], vis[100009];   map<pair<int, int>, int> mp;   map<pair<int, int>, int>::iterator ii;   int main() {     int n, i;     scanf( %d , &n);     for (i = 0; i < n; ++i) scanf( %d , &arr[i]);     memset(mark, 0, sizeof mark);     for (i = 0; i < n; ++i) mark[arr[i]] += 1;     int cnt = 0;     for (i = 1; i <= 100000; ++i)       if (mark[i] != 0) ++cnt;     long long ans = 0;     memset(vis, -1, sizeof vis);     for (i = 0; i < n; ++i) {       int val = arr[i];       mark[val]--;       if (mark[val] == 0) cnt--;       if (vis[val] == -1) {         vis[val] = 1;         ans = ans + (long long)cnt;       }     }     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   template <typename... Args>   void dbg(Args... args) {     ((cout << args <<    ), ...);   }   const int N = 110;   char t[N][N][N];   char f[N][N][N];   int main() {     int n, m, k;     cin >> n >> m >> k;     for (int x = 1; x <= n; ++x)       for (int y = 1; y <= m; ++y)         for (int z = 1; z <= k; ++z) {           cin >> t[x][y][z];           t[x][y][z] -=  0 ;         }     for (int x = 1; x <= n; ++x)       for (int y = 1; y <= m; ++y)         for (int z = 1; z <= k; ++z) {           if (!t[x][y][z]) continue;           bool tmp[3][3][3][3]{};           bool ok[3][3][3]{};           tmp[0][0][0][0] = 1, tmp[0][0][0][1] = 1, tmp[0][0][0][2] = 1;           ok[0][0][0] = 1;           for (int o = 0; o < 3; ++o)             for (int p = 0; p < 3; ++p)               for (int q = 0; q < 3; ++q)                 if (t[x + o][y + p][z + q]) {                   tmp[o][p][q][0] |= ((o > 0) ? ok[o - 1][p][q] : false);                   tmp[o][p][q][1] |= ((p > 0) ? ok[o][p - 1][q] : false);                   tmp[o][p][q][2] |= ((q > 0) ? ok[o][p][q - 1] : false);                   int good = tmp[o][p][q][0] + tmp[o][p][q][1] + tmp[o][p][q][2];                   ok[o][p][q] = good;                   if (good == 1) {                     if (o == 0 && p == 1 && q == 0 ||                         o == 0 && p == 0 && q == 1 ||                         o == 1 && p == 0 && q == 0 || o == 0 && p == 0 && q == 0)                       continue;                     if (o > 0 and ok[o - 1][p][q]) f[x + o - 1][y + p][z + q] = 1;                     if (p > 0 and ok[o][p - 1][q]) f[x + o][y + p - 1][z + q] = 1;                     if (q > 0 and ok[o][p][q - 1]) f[x + o][y + p][z + q - 1] = 1;                   }                 }         }     int cnt = 0;     for (int x = 1; x <= n; ++x)       for (int y = 1; y <= m; ++y)         for (int z = 1; z <= k; ++z) {           if (f[x][y][z] == 1) ++cnt;         }     dbg(cnt);   }
#include <bits/stdc++.h>   using namespace std;   const int MX = (int)2e5 + 10;   const int mod = (int)1e9 + 7;   bool check(string& t) {     bool ok = true;     int n = t.size();     for (int j = 0; j < n / 2; ++j) {       ok &= t[j] == t[n - j - 1];     }     return ok;   }   int main(int argc, char* argv[]) {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     string s;     cin >> s;     int n = s.size();     int it = 0;     while (it < n && s[it] == s[0]) it++;     if (it == n) {       cout <<  Impossible  << endl;       return 0;     }     for (int i = 1; i < n; ++i) {       string s1 = s.substr(0, i);       string s2 = s.substr(i);       string t = s2 + s1;       if (t != s && check(t)) {         cout << 1 << endl;         return 0;       }     }     if (n % 2 == 0) {       string sx = s.substr(0, n / 2);       if (!check(sx)) {         cout << 1 << endl;         return 0;       }     }     for (int i = 0; i < n / 2; ++i) {       string sx = s.substr(0, i + 1);       if (!check(sx)) {         cout << 2 << endl;         return 0;       }     }     cout <<  Impossible  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   char change(char c) {     if (c ==  B ) {       return  W ;     }     return  B ;   }   int main() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     int good = -1;     vector<int> start(n, -1);     for (int i = 0; i != n; ++i) {       if (s[(i + n - 1) % n] == s[i] || s[i] == s[(i + 1) % n]) {         start[i] = 0;         good = i;       }     }     if (good == -1) {       for (int i = 0; i != n; ++i) {         start[i] = k + 1;       }     } else {       for (int i = (good + 1) % n; i != good;) {         if (start[i]) {           int cnt = 0;           while (start[(i + cnt) % n]) {             ++cnt;           }           int pos = 1;           while (pos != cnt + 1) {             start[(i + pos - 1) % n] = min(pos, cnt - pos + 1);             ++pos;           }           i = (i + cnt) % n;         } else {           i = (i + 1) % n;         }       }     }     for (int i = 0; i != n; ++i) {       if (min(k, start[i]) & 1) {         cout << change(s[i]);       } else {         cout << s[i];       }     }   }
#include <bits/stdc++.h>   void out(int x) {     if (x) {       std::cout << x << std::endl;     } else {       std::cout <<  Impossible  << std::endl;     }     exit(0);   }   bool string_is_palindorome(const std::string& s) {     for (int i = 0, j = s.size() - 1; i < j; i++, j--)       if (s[i] != s[j]) return false;     return true;   }   int main(int argc, char** argv) {     std::cin.tie(0);     std::ios_base::sync_with_stdio(0);     std::cout << std::fixed << std::setprecision(6);     std::cerr << std::fixed << std::setprecision(6);     std::string s, t;     std::cin >> s;     t = s;     int size = s.size();     if (size <= 3) out(0);     for (int i = 1; i < size; i++) {       std::rotate(std::begin(t), std::begin(t) + 1, std::end(t));       if (!string_is_palindorome(t)) continue;       if (t != s) out(1);     }     for (int i = 1; i <= size / 2; i++)       for (int j = 0, k = size - i; j < i; j++, k++) {         assert(k < size);         if (s[k] != s[j]) out(2);       }     out(0);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 300;   const long long mod = 1e9 + 7;   const long long NN = 20 + 1;   vector<pair<long long, long long> > a[N];   long long p[N][NN], t[N], dp[N], dp2[N], sum[N], h[N], dp3[N], dp4[N], n;   void dfs2(long long v, long long par = -1, long long col = -1) {     dp3[v] = dp[v];     dp4[v] = dp2[v];     if (par != -1) {       dp4[v] = 0;       dp3[v] = (dp3[par] + ((n - 2 * t[v] + 2 * mod) * col) % mod) % mod;       dp4[v] = (dp4[par] + (((col * col) % mod) * (n - 1)) % mod +                 2 * (col * dp3[par]) % mod) %                mod;       dp4[v] += 2 * mod - (4 * dp[v] * col) % mod -                 (t[v] * 4 * ((col * col) % mod)) % mod + (col * col) % mod;       dp4[v] %= mod;     }     for (long long i = 0, u, c; i < a[v].size(); i++) {       u = a[v][i].first, c = a[v][i].second;       if (u != par) {         dfs2(u, v, c);       }     }   }   void dfs(long long v, long long par = -1) {     p[v][0] = par;     for (long long i = 1; i < NN; i++) {       if (p[v][i - 1] != -1) p[v][i] = p[p[v][i - 1]][i - 1];     }     t[v] = 1;     for (long long i = 0, u, c; i < a[v].size(); i++) {       u = a[v][i].first, c = a[v][i].second;       if (u != par) {         sum[u] = (sum[v] + c) % mod;         h[u] = h[v] + 1;         dfs(u, v);         t[v] += t[u];         dp[v] += (dp[u] + c * t[u]) % mod;         dp[v] %= mod;         dp2[v] += (dp2[u] + (t[u] * ((c * c) % mod)) % mod) % mod;         dp2[v] %= mod;         dp2[v] += 2 * ((c * dp[u]) % mod);         dp2[v] %= mod;       }     }   }   long long getp(long long v, long long k) {     for (long long i = 0; i < NN; i++) {       if ((k & (1 << i)) != 0) v = p[v][i];     }     return v;   }   long long lca(long long v, long long u) {     if (h[u] > h[v]) swap(u, v);     v = getp(v, h[v] - h[u]);     if (u == v) return v;     for (long long i = NN - 1; i >= 0; i--) {       if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];     }     return p[u][0];   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     memset(p, -1, sizeof(p));     cin >> n;     for (long long i = 0, u, v, c; i < n - 1; i++) {       cin >> u >> v >> c;       u--;       v--;       a[u].push_back(make_pair(v, c));       a[v].push_back(make_pair(u, c));     }     dfs(0);     dfs2(0);     long long m;     cin >> m;     for (long long _ = 0, u, v; _ < m; _++) {       cin >> u >> v;       u--;       v--;       long long l = lca(u, v);       long long x = sum[u] + sum[v] - 2 * sum[l] + 2 * mod;       x %= mod;       long long ans =           dp2[v] + (t[v] * ((x * x) % mod)) % mod + 2 * ((dp[v] * x) % mod);       if (v == l) {         long long h = (dp4[v] - dp2[v] + mod) % mod +                       ((n - t[v]) * ((x * x) % mod)) % mod +                       (2 * (((dp3[v] - dp[v]) * x) % mod) % mod);         h %= mod;         ans = dp4[u] - h + mod;       }       ans %= mod;       ans *= 2;       ans += mod - dp4[u];       ans %= mod;       cout << ans <<   n ;     }     return 0;   }