malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 1;   map<int, int> m[N];   set<pair<int, int> > s[N];   vector<int> g[N];   int n, c[N];   long long ans[N];   void dfs(int u, int p) {     m[u][c[u]] = 1;     s[u].insert(pair<int, int>(1, c[u]));     ans[u] = c[u];     for (int v : g[u]) {       if (v == p) continue;       dfs(v, u);       if (m[u].size() < m[v].size()) {         m[u].swap(m[v]);         s[u].swap(s[v]);         ans[u] = ans[v];       }     }     for (int v : g[u]) {       if (v == p) continue;       for (auto x : m[v]) {         s[u].erase(pair<int, int>(m[u][x.first], x.first));         m[u][x.first] += x.second;         if (s[u].empty() || m[u][x.first] > (*--s[u].end()).first)           ans[u] = x.first;         else if (m[u][x.first] == (*--s[u].end()).first)           ans[u] += x.first;         s[u].insert(pair<int, int>(m[u][x.first], x.first));       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> c[i];     for (int i = 1; i < n; ++i) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     dfs(1, 0);     for (int i = 1; i <= n; ++i) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 5;   long long INF = 1e18;   int n, s;   vector<int> ans;   long long ansV = 0;   long long dis[N];   int edge[N], val[N];   vector<pair<int, int> > adj[N];   void solve() {     for (int i = 1; i <= n; i++) dis[i] = INF;     priority_queue<pair<long long, int>, vector<pair<long long, int> >,                    greater<pair<long long, int> > >         pq;     dis[s] = 0;     pq.push({dis[s], s});     while (!pq.empty()) {       int u = pq.top().second;       long long Cost = pq.top().first;       pq.pop();       if (Cost > dis[u]) continue;       if (u != s) {         ans.push_back(edge[u]);         ansV += val[edge[u]];       }       for (auto P : adj[u]) {         int v = P.first, idx = P.second;         if (Cost + val[idx] < dis[v]) {           dis[v] = Cost + val[idx];           edge[v] = idx;           pq.push({dis[v], v});         } else if (Cost + val[idx] == dis[v] && val[idx] < val[edge[v]])           edge[v] = idx;       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v >> val[i];       adj[u].push_back({v, i});       adj[v].push_back({u, i});     }     cin >> s;     solve();     sort(ans.begin(), ans.end());     cout << ansV << endl;     for (auto v : ans) cout << v + 1 <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > result;   void move(pair<int, int> &from, pair<int, int> &to) {     from.first -= to.first;     to.first *= 2;     result.push_back(make_pair(to.second, from.second));   }   void removeone(pair<int, int> list[3]) {     sort(list, list + 3);     if (list[0].first == 0) {       swap(list[0], list[2]);       return;     }     int q = list[1].first / list[0].first;     for (int x = 1; q; x <<= 1)       if (q & x) {         move(list[1], list[0]);         q -= x;       } else         move(list[2], list[0]);     removeone(list);   }   int main(int argc, char **argv) {     int n;     vector<pair<int, int> > nums;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int v;       scanf( %d , &v);       if (v) nums.push_back(make_pair(v, i));     }     if (nums.size() < 2) {       printf( -1 n );       return 0;     }     result.clear();     while (nums.size() > 2) {       removeone(&nums[nums.size() - 3]);       nums.pop_back();     }     printf( %d n , (int)result.size());     for (int i = 0; i < (int)result.size(); i++)       printf( %d %d n , result[i].first + 1, result[i].second + 1);     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   long long x, y, z, a, b, c, d, n, m, mod = 1000000007;   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     string s;     bool w = true;     cin >> n >> m >> s;     a = s.find( G );     b = s.find( T );     if (a > b && (a - b) % m == 0) {       for (int i = a; i > b; i -= m) {         if (s[i] ==  # ) {           w = false;           break;         }       }     } else if (a < b && (b - a) % m == 0) {       for (int i = a; i < b; i += m) {         if (s[i] ==  # ) {           w = false;           break;         }       }     }     if (w == false || abs(b - a) % m != 0)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     string a, b;     int k;     cin >> a >> b >> k;     int n = a.length(), l = 0;     long long gd = 0, bd = 0;     int f = 0;     if (a.length() != b.length()) {       cout << 0;       return 0;     }     for (int i = 0; i < n; i++) {       int j;       for (j = 0; j < n; j++)         if (a[(i + j) % n] != a[j]) break;       if (j == n)         gd++;       else         bd++;       for (j = 0; j < n; j++)         if (a[(i + j) % n] != b[j]) break;       if (j == n) f = 1;     }     if (f == 0) {       cout << 0;       return 0;     }     vector<int> dp(2, 0), dp1(2, 0);     if (a == b)       dp[0] = 1;     else       dp1[0] = 1;     long long mod = 1e9 + 7;     for (int i = 1; i <= k; i++) {       long long x = dp[1 - i % 2] * (gd - 1) + dp1[1 - i % 2] * gd;       x %= mod;       dp[i % 2] = x;       x = dp[1 - i % 2] * bd + dp1[1 - i % 2] * (bd - 1);       x %= mod;       dp1[i % 2] = x;     }     cout << dp[k % 2] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   void slove() {     int n, m, k;     cin >> n >> m >> k;     int ls = 0;     if (n % 2) {       n--, ls += m / 2;     }     if (m % 2) m--;     int flag = 1;     if (ls > k) flag = 0;     ls += (n * m) / 2;     if (ls < k) flag = 0;     if (ls % 2 != k % 2) flag = 0;     if (flag)       puts( YES );     else       puts( NO );   }   int main() {     int T;     scanf( %d , &T);     while (T--) {       slove();     }     return 0;   }
#include <bits/stdc++.h>   template <class T, class F, class C>   auto binary_search(T l, T r, F func, C cmp) -> T;   auto division(int64_t n, int64_t d) -> int64_t { return n / d; }   int main() {     int64_t n, k, m, d;     std::cin >> n >> k >> m >> d;     int64_t answer = 0;     for (int64_t q_k = 1; q_k <= d; ++q_k) {       int64_t x_max = binary_search(           int64_t(1), m + 1,           [n, q_k, k](int64_t x) { return (division(n, x) - 1) / k + 1 >= q_k; },           [](int64_t l, int64_t r) { return l + 1 >= r; });       if ((division(n, x_max) - 1) / k + 1 != q_k) {         continue;       }       answer = std::max(answer, x_max * q_k);     }     std::cout << answer <<   n ;     return 0;   }   template <class T, class F, class C>   auto binary_search(T l, T r, F func, C cmp) -> T {     bool dir = func(l);     for (auto mid = (l + r) / 2; !cmp(l, r); mid = (l + r) / 2) {       (func(mid) == dir ? l : r) = mid;     }     return l;   }
#include <algorithm>   #include <cmath>   #include <vector>   #include <iostream>   using namespace std;      void solve() {       int n, totalCost, prev, x, cost;       totalCost = 0;       cin >> n;          if (n <= 1) {           cout << 0 <<   n ;           return;       }          cin >> prev;          int m, M;          for (int i = 1; i < n; ++i) {           cin >> x;              if (x < prev) {               m = x;               M = prev;           } else {               m = prev;               M = x;           }              if (m * 2 < M) {               cost = int(ceil(log2(float(M) / float(m)))) - 1;              // cout <<  ->  << m <<     << M <<     << cost <<   n ;               totalCost += cost;           }              prev = x;       }          cout << totalCost <<   n ;   }      int main() {       ios_base::sync_with_stdio(false);       cin.tie(nullptr);          int T;       cin >> T;          for (int t = 0; t < T; ++t) {           solve();       }          return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   char s[105];   int main() {     int n, nr = 0;     gets(s);     n = strlen(s);     int x = 0;     for (int i = 0; i < n; i++) x = (x * 2 + s[i] -  0 ) % mod;     for (int i = 1; i < n; i++) x = (x * 2) % mod;     printf( %d n , x);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, b, mod;     cin >> n >> m >> b >> mod;     int dp[502][502];     int bugs[n + 2];     for (int i = 0; i < n; i++) cin >> bugs[i];     memset(dp, 0, sizeof dp);     dp[0][0] = 1;     for (int k = 0; k < n; k++) {       for (int j = 0; j <= b - bugs[k]; j++) {         for (int i = 0; i < m; i++) {           dp[i + 1][j + bugs[k]] += dp[i][j];           dp[i + 1][j + bugs[k]] %= mod;         }       }     }     int ans = 0;     for (int i = 0; i <= b; i++) {       ans += dp[m][i];       ans %= mod;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937_64 rang(       chrono::high_resolution_clock::now().time_since_epoch().count());   int rng(int lim) {     uniform_int_distribution<int> uid(0, lim - 1);     return uid(rang);   }   const int INF = 0x3f3f3f3f;   const int mod = 1e9 + 7;   void solve() {     int sum, limit;     cin >> sum >> limit;     vector<int> res;     for (int i = limit; i >= 1; i--) {       int lowbit = i ^ (i & (i - 1));       if (sum >= lowbit) res.push_back(i), sum -= lowbit;       if (sum == 0) break;     }     if (sum != 0)       cout << -1;     else {       cout << res.size() <<   n ;       for (auto x : res) cout << x <<    ;     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     srand(chrono::high_resolution_clock::now().time_since_epoch().count());     int t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = (int)4e6 + 11;   const long long inf = 1e9 + 7;   int n, m, p[1 << 20], can[1 << 20];   int find_parent(int x) {     if (x == p[x]) {       return x;     }     return p[x] = find_parent(p[x]);   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < 1 + n; ++i) {       p[i] = i;     }     memset(can, -1, sizeof(can));     for (int i = 0; i < m; ++i) {       int l, r, x;       scanf( %d %d %d , &l, &r, &x);       --x;       l = find_parent(l - 1);       while (l < r) {         if (l != x) {           can[l] = x;           p[l] = 1 + l;         }         l = find_parent(1 + l);       }     }     for (int i = 0; i < n; ++i) {       printf( %d  , can[i] + 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 110, alpha = 26;   int n, m;   int dp[maxn][maxn][alpha];   bool mark[maxn];   vector<int> ts;   vector<pair<int, int> > G[maxn];   void dfs(int a) {     mark[a] = 1;     for (pair<int, int> p : G[a]) {       int b = p.first;       if (!mark[b]) {         dfs(b);       }     }     ts.push_back(a);   }   int32_t main() {     cin >> n >> m;     for (int i = int(0); i < int(m); i++) {       int a, b;       char c;       cin >> a >> b >> c;       a--, b--;       G[a].push_back({b, c -  a });     }     for (int i = int(0); i < int(n); i++) {       if (!mark[i]) {         dfs(i);       }     }     for (int size = int(0); size < int(2 * n - 1); size++) {       for (int i = int(0); i < int(min(size + 1, n)); i++) {         int j = size - i;         if (j >= n) continue;         int a = ts[i], b = ts[j];         for (int x = int(0); x < int(alpha); x++) {           for (pair<int, int> p : G[a]) {             int ap = p.first, c = p.second;             if (c >= x) dp[a][b][x] = max(dp[a][b][x], 1 - dp[b][ap][c]);           }         }       }     }     for (int i = int(0); i < int(n); i++) {       for (int j = int(0); j < int(n); j++) {         cout << (dp[i][j][0] ?  A  :  B );       }       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int a[105];   int main() {     int n, i, f;     cin >> n;     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     f = 0;     for (i = 1; i < n; i++) {       if (a[i] != a[0]) {         f = 1;         break;       }     }     if (f == 1)       cout << a[i];     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 110;   string s[maxn];   int cur[maxn], c[maxn];   int n, m, ans = 0;   void dfs(int x) {     if (x == n) {       int flag = 1;       for (int i = 1; i <= m; i++)         if (cur[i] - c[i]) flag = 0;       ans += flag;       return;     }     for (char v =  0 ; v <=  1 ; v++) {       int can = 1;       for (int i = 1; i <= m; i++) {         cur[i] += (v == s[i][x]);         if (cur[i] > c[i]) can = 0;       }       if (can) dfs(x + 1);       for (int i = 1; i <= m; i++) cur[i] -= (v == s[i][x]);     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) cin >> s[i] >> c[i];     memset(cur, 0, sizeof(cur));     dfs(0);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Hint {     int up, cnt;     bool operator<(Hint o) const { return up < o.up; }   };   int n, b, q;   Hint h[10013];   int has[10013][5], take[10013];   int main() {     scanf( %d%d%d , &n, &b, &q);     for (int i = 1; i <= q; i++) {       scanf( %d%d , &h[i].up, &h[i].cnt);     }     h[++q] = {b, n};     sort(h, h + q + 1);     for (int i = 1; i <= q; i++) {       for (int j = h[i - 1].up + 1; j <= h[i].up; j++) has[i][j % 5] += 1;       take[i] = h[i].cnt - h[i - 1].cnt;       if (0 > take[i] || take[i] > h[i].up - h[i - 1].up)         return printf( unfair n ), 0;     }     for (int i = 1; i < 32; i++) {       int need = n / 5 * __builtin_popcount(i);       int can = 0;       for (int k = 1; k <= q; k++) {         int cur = 0;         for (int j = 0; j < 5; j++)           if (i & (1 << j)) {             cur += has[k][j];           }         can += min(cur, take[k]);       }       if (need > can) return printf( unfair n ), 0;     }     printf( fair n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 60;   map<double, int> p;   bitset<N * N> g[N * N + 5];   int n, m;   double ya[N + 5], yb[N + 5];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %lf , &ya[i]);     for (int i = 1; i <= m; i++) scanf( %lf , &yb[i]);     int tot = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         double now = (ya[i] + yb[j]) / 2;         if (p.find(now) == p.end()) tot++, p[now] = tot;         g[p[now]].set(i, 1);         g[p[now]].set(j + n, 1);       }     }     int ans = -1;     for (int i = 1; i <= tot; i++) {       for (int j = i; j <= tot; j++) {         bitset<N * N> s;         s = g[i] | g[j];         ans = max(ans, (int)s.count());       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 100;   int ans;   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     int a[1111] = {}, cnt[1111] = {};     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) {       if (cnt[i] == 0) {         ans++;         for (int j = i + 1; j <= n; j++) {           if (a[j] % a[i] == 0) {             cnt[j] = 1;           }         }       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long n, k;       cin >> n;       vector<long long> dp(n + 3, 0);       vector<long long> v;       long long sum = 0;       for (int j = 0; j < n; j++) {         cin >> k;         if (j % 2 == 0) {           sum = sum + k;         }         v.push_back(k);       }       dp[0] = 0;       long long ans = 0;       long long z = 0;       if (n > 1) {         dp[1] = max(z, v[1] - v[0]);         ans = max(ans, dp[1]);       }       k = 2;       for (int j = 1; j < n - 1; j++) {         if (j % 2 == 0) {           dp[k] = max(z, dp[k - 2] + (v[j + 1] - v[j]));         } else {           dp[k] = max(z, dp[k - 2] + (v[j] - v[j + 1]));         }         ans = max(ans, dp[k]);         k++;       }       cout << sum + ans << endl;     }   }
#include <stdio.h>      int main() {       int t;       scanf( %d , &t);       while (t--) {           int x, i;           scanf( %d , &x);           for (i = 1; i * (i + 1) / 2 < x; i++);           if (i * (i + 1) / 2 - x != 1) {               printf( %d n , i);           } else {               printf( %d n , i + 1);           }       }   }
#include <bits/stdc++.h>   template <typename Y>   inline bool updmin(Y &a, Y b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <typename Y>   inline bool updmax(Y &a, Y b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <typename Y>   inline Y abs(Y a) {     if (a < 0) a = -a;     return a;   }   template <typename Y>   inline Y sqr(Y a) {     return a * a;   }   int read() {     int w = 1, q = 0, ch =    ;     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) w = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) q = q * 10 + ch - 48;     return q * w;   }   inline void FileIO() {     freopen(                     .in ,          r , stdin);     freopen(                     .out ,          w , stdout);   }   int n, m, h;   const int N = 100020;   std::vector<int> G[N], s;   int tim = 0, cnt = 0;   int low[N], dfn[N], vis[N], bel[N];   void tarjan(int p) {     vis[p] = 1;     s.push_back(p);     low[p] = dfn[p] = ++tim;     for (int t : G[p]) {       if (!dfn[t]) {         tarjan(t);         updmin(low[p], low[t]);       } else if (vis[t]) {         updmin(low[p], dfn[t]);       }     }     if (low[p] == dfn[p]) {       ++cnt;       while (s.back() != p) {         int cur = s.back();         s.pop_back();         vis[cur] = 0;         bel[cur] = cnt;       }       bel[p] = cnt;       vis[p] = 0;       s.pop_back();     }   }   int u[N], size[N], out[N];   int main() {     n = read();     m = read();     h = read();     for (int i = 1; i <= n; i++) {       u[i] = read();     }     for (int i = 1; i <= m; i++) {       int x = read(), y = read();       if ((u[x] + 1) % h == u[y]) {         G[x].push_back(y);       }       if ((u[y] + 1) % h == u[x]) {         G[y].push_back(x);       }     }     for (int i = 1; i <= n; i++) {       if (!dfn[i]) {         tarjan(i);       }     }     for (int i = 1; i <= n; i++) {       size[bel[i]]++;       for (int j : G[i]) {         if (bel[i] != bel[j]) {           out[bel[i]]++;         }       }     }     int ans = n + 1, pos = 0;     for (int i = 1; i <= cnt; i++) {       if (!out[i] && updmin(ans, size[i])) {         pos = i;       }     }     printf( %d n , ans);     for (int i = 1; i <= n; i++) {       if (bel[i] == pos) {         printf( %d  , i);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int n = s.size();     string r;     int i = 0;     while (i < n) {       if (n - i >= 3) {         if (s.substr(i, 3) ==  WUB ) {           if (r.size() && r.back() !=    ) r +=    ;           i += 3;         } else           r += s[i++];       } else {         r += s[i++];       }     }     cout << r << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long ara[12];   int main() {     long long n, i, j, k;     for (i = 0; i < 4; i++) {       cin >> k;       ara[i] = k;     }     if (ara[0] == 0 && ara[3] == 0 && ara[2] > 0) {       printf( 0 n );     } else {       if (ara[0] == ara[3])         printf( 1 n );       else         printf( 0 n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool isvowel(char c) {     return c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ;   }   int main() {     char arr[110];     scanf( %s , arr);     int len = strlen(arr);     bool check = true;     for (int i = 0; i < len; i++) {       if (isvowel(arr[i])) {       } else if (arr[i] ==  n ) {       } else {         if (i == len - 1 || !isvowel(arr[i + 1])) check = false;       }     }     puts(check ?  YES  :  NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int power(long long int a, long long int x) {     long long int res = 1;     while (x > 0) {       if (x & 1) {         res *= a;       }       a *= a;       x = x / 2;     }     return res;   }   int main() {     long long int n;     scanf( %lld , &n);     long long int ans = 0;     ans = power(4, n - 2) * 6;     if (n > 3) {       ans += (n - 3) * 4 * 9 * power(4, n - 4);     }     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int A = 1e5 + 11;   const int B = 1e6 + 11;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   inline int read() {     char c = getchar();     int x = 0, f = 1;     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);     return x * f;   }   struct edgexx {     int v, nxt, w;   } edge[B];   struct edgex {     int v, nxt;   } edge_[B];   struct node {     long long d;     int id;     bool operator<(const node &a) const { return d > a.d; }   };   struct xxs {     int u, v;     long long w;     bool operator<(const xxs &e) const { return w < e.w; }   } e[B];   int n, cnt, js, k, c, t, head[B], m, q, head2[B], fa[B], u_fa[B];   long long dis[B], wnd[B];   int sz[B], son[B], top[B], dep[B];   priority_queue<node> pq;   void add1(int u, int v, int w) {     edge[++cnt].nxt = head[u];     edge[cnt].v = v;     edge[cnt].w = w;     head[u] = cnt;   }   void add2(int u, int v) {     edge_[++js].nxt = head2[u];     edge_[js].v = v;     head2[u] = js;   }   int find(int x) {     if (x == u_fa[x]) return x;     return u_fa[x] = find(u_fa[x]);   }   void dfs1(int u, int f) {     sz[u] = 1;     dep[u] = dep[fa[u] = f] + 1;     for (int i = head2[u]; i; i = edge_[i].nxt) {       int v = edge_[i].v;       if (v == f) continue;       dfs1(v, u);       sz[u] += sz[v];       if (sz[v] > sz[son[u]]) son[u] = v;     }   }   void dfs2(int u, int topf) {     top[u] = topf;     if (son[u]) dfs2(son[u], topf);     for (int i = head2[u]; i; i = edge_[i].nxt) {       int v = edge_[i].v;       if (v == fa[u] || v == son[u]) continue;       dfs2(v, v);     }   }   int lca(int u, int v) {     while (top[u] != top[v]) {       if (dep[top[u]] < dep[top[v]]) swap(u, v);       u = fa[top[u]];     }     return dep[u] < dep[v] ? u : v;   }   int main() {     cin >> n >> m >> k >> q;     for (int i = 1; i <= m; i++) {       int x = read(), y = read(), z = read();       add1(x, y, z);       add1(y, x, z);       e[i] = (xxs){x, y, z};     }     memset(dis, 0x3f, sizeof(dis));     for (int i = 1; i <= k; i++) pq.push((node){dis[i] = 0, i});     while (!pq.empty()) {       long long d = pq.top().d;       int u = pq.top().id;       pq.pop();       if (d != dis[u]) continue;       for (int i = head[u]; i; i = edge[i].nxt) {         int v = edge[i].v;         if (dis[v] > d + edge[i].w) pq.push((node){dis[v] = d + edge[i].w, v});       }     }     for (int i = 1; i <= m; i++) e[i].w += dis[e[i].u] + dis[e[i].v];     sort(e + 1, e + 1 + m);     for (int i = 1; i <= 2 * n; i++) u_fa[i] = i;     cnt = n;     for (int i = 1; i <= m; i++) {       int u = e[i].u, v = e[i].v;       long long w = e[i].w;       u = find(u);       v = find(v);       if (u == v) continue;       u_fa[u] = u_fa[v] = ++cnt;       wnd[cnt] = w;       add2(cnt, u);       add2(cnt, v);     }     dfs1(cnt, 0);     dfs2(cnt, cnt);     while (q--) {       int u = read(), v = read();       printf( %lld n , wnd[lca(u, v)]);     }     return 0;   }