malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1000;   long long d[MAX + 10][3][3];   string s;   int n;   long long dyn() {     if (s[1] == 0) d[1][0][0] = 1;     d[1][0][2] = 1;     if (s[0] == 0 && s[1] == 1) d[1][1][1] = 1;     if (s[0] == 1) d[1][2][2] = 1;     for (int i = 1; i <= n - 2; i++)       for (int k = 0; k < 3; k++)         for (int l = 0; l < 3; l++)           for (int j = 0; j < 2; j++) {             if (l == 0 || l == 1) {               if (j == 0) {                 if (s[i + 1] == 0) d[i + 1][k][0] += d[i][k][l];               } else {                 d[i + 1][k][2] += d[i][k][l];               }             } else if (l == 2) {               if (j == 0) {                 if (s[i] == 0 && s[i + 1] == 1) d[i + 1][k][1] += d[i][k][l];               } else {                 if (s[i] == 1) d[i + 1][k][2] += d[i][k][l];               }             }           }     long long sum = 0;     for (int k = 0; k < 3; k++)       for (int l = 0; l < 3; l++) {         if (k == 0) {           if (l == 2) {             if (s[0] == 1 && s[n - 1] == 0) sum += d[n - 1][k][l];           } else {             if (s[0] == 0) sum += d[n - 1][k][l];           }         } else {           if (l == 2) {             if (s[n - 1] == 1) sum += d[n - 1][k][l];           } else {             sum += d[n - 1][k][l];           }         }       }     return sum;   }   int main() {     cin >> s;     n = (int)s.size();     for (int i = 0; i < n; i++) {       s[i] -=  A ;       s[i] = !s[i];     }     cout << dyn() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     string s;     cin >> s;     string ans =  0 ;     int curr = 0;     for (int i = 1; i < n; i++) {       if (s[i] ==  ( ) curr = curr ^ 1;       ans += (char)(curr +  0 );       if (s[i] ==  ) ) curr = curr ^ 1;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   long long c[200005];   int b[200005];   int a[100005];   void up(int n, int k) {     if (!n) return;     while (n <= 200000) {       b[n]++;       c[n] += k;       k++;       n <<= 1;     }   }   void solve(int n) {     int cur = 0;     long long num = n * 2;     up(num, 1);     num = n;     cur = 0;     while (num) {       if (num & 1) {         up(num / 2 * 2, cur + 2);       }       b[num]++;       c[num] += cur;       num >>= 1;       cur++;     }   }   int main() {     scanf( %d , &n);     memset(c, 0, sizeof(c));     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       solve(a[i]);     }     long long res = 1000000000;     for (int i = 0; i <= 200000; i++) {       if (b[i] == n) {         res = min(res, c[i]);       }     }     printf( %lld n , res);   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e6 + 10, mod = 1e9 + 7;   long long n, x;   long long f[N];   void solve() {     scanf( %lld , &n);     long long s1 = 0, s2 = 0, ans = 0;     for (long long i = 1; i <= n; i++) {       scanf( %lld , &x);       if (x == 1)         s1++;       else         s2++;     }     f[0] = 1, f[1] = 1;     for (long long i = 2; i <= s1; i++)       f[i] = (f[i - 1] + (i - 1) * f[i - 2]) % mod;     ans = f[s1];     for (long long i = s1 + 1; i <= n; i++) ans = ans * i % mod;     printf( %lld , ans);   }   signed main() { solve(); }
#include <bits/stdc++.h>   using namespace std;   const int N = 2000 + 10;   int n, tmp, ans, p[N], q[N], pos[N];   vector<pair<int, int> > vec;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int i, j;     cin >> n;     for (i = 1; i <= n; ++i) cin >> p[i];     for (i = 1; i <= n; ++i) cin >> q[i];     for (i = 1; i <= n; ++i) pos[p[i]] = i;     for (i = 1; i <= n; ++i) p[pos[q[i]]] = i;     for (i = 1; i <= n; ++i) pos[p[i]] = i;     for (i = n; i >= 1; --i) {       while (pos[i] != i) {         for (j = pos[i] + 1; j <= i; ++j) {           if (p[j] <= pos[i]) {             tmp = pos[i];             ans += j - pos[i];             vec.push_back({pos[i], j});             swap(pos[i], pos[p[j]]);             swap(p[tmp], p[j]);             break;           }         }       }     }     cout << ans <<   n ;     cout << vec.size() <<   n ;     for (auto x : vec) cout << x.first <<     << x.second <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   const int N = 1e7 + 100;   bool a[N];   int main() {     cin >> n >> k;     vector<long long> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];       a[v[i]] = 1;     }     int q;     cin >> q;     for (int test = 0; test < q; test++) {       long long w;       int ans = 1000;       cin >> w;       for (int t = 1; t <= k; t++) {         for (int i = 0; i < n; i++) {           long long rem = w - t * v[i];           if (rem < 0) continue;           if (rem == 0) ans = min(ans, t);           for (int e = 1; t + e <= k; e++) {             if (rem % e != 0) continue;             long long x = rem / e;             if (x >= N) continue;             if (a[x]) ans = min(e + t, ans);           }         }       }       if (ans != 1000)         cout << ans;       else         cout << -1;       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   int main(void) {     int n;     int i, i2;     int a[100000] = {0};     int flag = 0;     int max = -1000001;     int min = 1000001;     int sum = 0;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);       if (a[i] > 0) {         if (a[i] < min && a[i] % 2 != 0) {           min = a[i];         }         sum += a[i];       } else {         if (a[i] > max && a[i] % 2 != 0) {           max = a[i];         }       }     }     if (min == 1000001) {       printf( %d , sum + max);       return 0;     }     if (sum % 2 == 0) {       if (min > (-max)) {         printf( %d , sum + max);       } else {         printf( %d , sum - min);       }     } else {       printf( %d , sum);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     int sum = 0;     sum += 2 * n / k;     if (2 * n % k != 0) sum++;     sum += 5 * n / k;     if (5 * n % k != 0) sum++;     sum += 8 * n / k;     if (8 * n % k != 0) sum++;     cout << sum;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mo = 1000000007;   const int inv2 = (mo + 1) / 2;   const int inv4 = (mo + 1) / 4;   const int N = 500005;   int dep[N], fa[N], deg[N];   int sz[N], p[N], cnt[N], n, m, nd;   vector<int> e[N], vec[N];   int power(int x, int y) {     int s = 1;     for (; y; y /= 2, x = 1ll * x * x % mo)       if (y & 1) s = 1ll * s * x % mo;     return s;   }   void dfs(int x) {     dep[x] = dep[fa[x]] + 1;     for (auto i : e[x])       if (i != fa[x])         if (!dep[i]) {           fa[i] = x;           dfs(i);         } else if (dep[x] > dep[i]) {           ++nd;           for (int y = x;; y = fa[y]) {             vec[nd].push_back(y);             if (y == i) break;           }           sz[nd] = vec[nd].size();           p[nd] = power(inv2, sz[nd]);         }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = (int)(1); i <= (int)(m); i++) {       int x, y;       scanf( %d%d , &x, &y);       e[x].push_back(y);       e[y].push_back(x);     }     for (int i = (int)(1); i <= (int)(n); i++) deg[i] = e[i].size();     dfs(1);     int E1 = 0, E2 = 0, E3 = 0;     E1 = 1ll * n * inv2 % mo;     E2 = 1ll * m * inv4 % mo;     for (int i = (int)(1); i <= (int)(nd); i++) E3 = (E3 + p[i]) % mo;     int E = (E1 + mo - E2 + 1ll * E3) % mo;     int E11 = 0, E12 = 0, E13 = 0, E22 = 0, E23 = 0, E33 = 0;     E11 = (1ll * n * (n - 1) % mo * inv4 % mo + E1) % mo;     for (int i = (int)(1); i <= (int)(n); i++)       E12 = (E12 + 1ll * (m - deg[i]) * inv4 % mo * inv2 + 1ll * deg[i] * inv4) %             mo;     for (int i = (int)(1); i <= (int)(nd); i++)       E13 =           (E13 + 1ll * p[i] * (1ll * inv2 * (n - sz[i]) % mo + sz[i]) % mo) % mo;     E22 = (1ll * m * (m - 1) % mo * inv4 % mo * inv4 % mo + E2) % mo;     for (int i = (int)(1); i <= (int)(n); i++)       E22 = (E22 + 1ll * deg[i] * (deg[i] - 1) % mo * inv4 % mo * inv4) % mo;     for (int i = (int)(1); i <= (int)(nd); i++)       E23 = (E23 + 1ll * p[i] * (1ll * inv4 * (m - sz[i]) % mo + sz[i])) % mo;     for (int i = (int)(1); i <= (int)(nd); i++)       for (auto j : vec[i])         E23 = (E23 + 1ll * p[i] * inv4 % mo * (deg[j] - 2) % mo) % mo;     for (int i = (int)(1); i <= (int)(nd); i++)       E33 = (E33 + 1ll * p[i] * (E3 + mo - p[i] + 1)) % mo;     for (int i = (int)(1); i <= (int)(nd); i++)       for (auto j : vec[i]) cnt[j] = (cnt[j] + p[i]) % mo;     for (int i = (int)(1); i <= (int)(nd); i++)       for (auto j : vec[i]) E33 = (E33 + 1ll * p[i] * (cnt[j] + mo - p[i])) % mo;     int EF =         (1ll * E11 + E22 + E33 + 2 * E13 + 4ll * mo - 2 * E12 - 2 * E23) % mo;     printf( %lld n , (EF + mo - 1ll * E * E % mo) % mo);   }
#include <bits/stdc++.h>   using namespace std;   long long N;   pair<long long, long long> arr[6005];   long long cnt[20][20];   bool good(int x1, int y1, int x2, int y2, int x3, int y3) {}   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> N;     for (int i = 1; i <= N; i++) {       cin >> arr[i].first >> arr[i].second;       cnt[arr[i].first % 4][arr[i].second % 4]++;     }     vector<pair<long long, long long>> vec;     for (int i = 0; i < 16; i += 2) {       for (int j = 0; j < 16; j += 2) {         vec.emplace_back(i, j);       }     }     long long ans = N * (N - 1) * (N - 2) / 6;     ans -= cnt[0][0] * cnt[0][2] * cnt[2][0];     ans -= cnt[0][0] * cnt[0][2] * cnt[2][2];     ans -= cnt[0][0] * cnt[2][0] * cnt[2][2];     ans -= cnt[0][2] * cnt[2][0] * cnt[2][2];     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void maxtt(T& t1, T t2) {     t1 = max(t1, t2);   }   template <typename T>   void mintt(T& t1, T t2) {     t1 = min(t1, t2);   }   bool debug = 0;   int n, m, k;   string direc =  URDL ;   const long long MOD2 = (long long)1000000007 * (long long)1000000007;   long long ln, lk, lm;   void etp(bool f = 0) {     puts(f ?  YES  :  NO );     exit(0);   }   void addmod(int& x, int y, int mod = 1000000007) {     assert(y >= 0);     x += y;     if (x >= mod) x -= mod;     assert(x >= 0 && x < mod);   }   void et(int x = -1) {     printf( %d n , x);     exit(0);   }   long long fastPow(long long x, long long y, int mod = 1000000007) {     long long ans = 1;     while (y > 0) {       if (y & 1) ans = (x * ans) % mod;       x = x * x % mod;       y >>= 1;     }     return ans;   }   long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }   int a[500135], cnt[500135], M, ps[500135], pre[500135];   long long ans;   pair<int, int> b1, b2;   void upt(pair<int, int> z) {     if (z > b1) {       b2 = b1;       b1 = z;     } else       maxtt(b2, z);   }   void uptAns(long long x, long long y) {     if (x >= 2) maxtt(ans, x * y);   }   void fmain(int tid) {     scanf( %d , &n);     for (int(i) = 1; (i) <= (int)(n); (i)++) {       scanf( %d , a + i);       cnt[a[i]]++;       maxtt(M, a[i]);     }     for (int(i) = 1; (i) <= (int)(M); (i)++) {       ps[i] = ps[i - 1] + cnt[i];       pre[i] = pre[i - 1];       if (cnt[i]) pre[i] = i;     }     for (int y = 2; y <= M; y++) {       long long cntY = 0;       for (int i = y; i <= M; i += y) {         cntY += (long long)(ps[min(i + y - 1, M)] - ps[i - 1]) * (i / y);       }       b1 = {-1, -1};       b2 = {-1, -1};       for (int k = M / y, R = M + 1; k >= 0; R = k * y, k--) {         int ky = k * y, ss = (b1.first >= 0) + (b2.first >= 0);         if (pre[R - 1] >= ky) {           pair<int, int> key = {pre[R - 1] % y, pre[R - 1]};           upt(key);           if (cnt[pre[R - 1]] > 1)             upt(key);           else {             int tmp = pre[pre[R - 1] - 1];             if (tmp >= ky) upt({tmp % y, tmp});           }         }         if (b1.first >= 0) {           uptAns(min((long long)(b1.first + ky) / 2, cntY - k), y);         }         if (b2.second >= 0) {           uptAns(min((long long)(b2.first + ky), cntY - k - k), y);           if ((b1.second < R) + ss >= 2) {             uptAns(min((long long)(b1.first + ky), cntY - k - k - 1), y);           }         }       }     }     printf( %lld n , ans);   }   int main() {     int t = 1;     for (int(i) = 1; (i) <= (int)(t); (i)++) {       fmain(i);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int read() {     int X = 0, w = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) w = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) X = X * 10 + c -  0 , c = getchar();     return X * w;   }   const int N = 100000 + 10;   const long long inf = 0x3f3f3f3f3f3f3f3f;   int n, a[N], b[N];   vector<int> E[N];   int rt[N], tot = 0;   struct line {     int k;     long long b;   } s[N];   struct node {     int ls, rs, id;   } t[N * 30];   long long f(int i, int x) { return 1ll * s[i].k * x + s[i].b; }   void modify(int& o, int l, int r, int x) {     if (!o) {       o = ++tot, t[o].id = x;       return;     }     if (l == r) {       if (f(x, l) < f(t[o].id, l)) t[o].id = x;       return;     }     int mid = (l + r) >> 1;     if (s[x].k < s[t[o].id].k) {       if (f(x, mid) < f(t[o].id, mid))         modify(t[o].ls, l, mid, t[o].id), t[o].id = x;       else         modify(t[o].rs, mid + 1, r, x);     } else if (s[x].k > s[t[o].id].k) {       if (f(x, mid) < f(t[o].id, mid))         modify(t[o].rs, mid + 1, r, t[o].id), t[o].id = x;       else         modify(t[o].ls, l, mid, x);     } else if (s[x].b < s[t[o].id].b)       t[o].id = x;   }   long long query(int o, int l, int r, int x) {     if (!o) return inf;     if (l == r) return f(t[o].id, l);     int mid = (l + r) >> 1;     long long res = f(t[o].id, x);     if (x <= mid)       res = min(res, query(t[o].ls, l, mid, x));     else       res = min(res, query(t[o].rs, mid + 1, r, x));     return res;   }   int merge(int x, int y, int l, int r) {     if (!x || !y) return x | y;     if (l == r) return f(t[x].id, l) < f(t[y].id, l) ? x : y;     int mid = (l + r) >> 1;     t[x].ls = merge(t[x].ls, t[y].ls, l, mid);     t[x].rs = merge(t[x].rs, t[y].rs, mid + 1, r);     modify(x, l, r, t[y].id);     return x;   }   long long dp[N];   void dfs(int u, int f) {     if (f && E[u].size() == 1)       dp[u] = 0;     else {       for (int v : E[u])         if (v != f) dfs(v, u), rt[u] = merge(rt[u], rt[v], -1e5, 1e5);       dp[u] = query(rt[u], -1e5, 1e5, a[u]);     }     s[u] = (line){b[u], dp[u]}, modify(rt[u], -1e5, 1e5, u);   }   int main() {     n = read();     for (int i = 1; i <= n; ++i) a[i] = read();     for (int i = 1; i <= n; ++i) b[i] = read();     for (int i = 1; i < n; ++i) {       int u = read(), v = read();       E[u].emplace_back(v), E[v].emplace_back(u);     }     dfs(1, 0);     for (int i = 1; i <= n; ++i) printf( %lld  , dp[i]);     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007, oo = 1000000009;   void Emsawy() {}   int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};   int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};   vector<vector<pair<int, int> > > adj;   int n, m, k, w, h;   struct Node {     vector<int> ord;     int cnt;     Node() {       ord = vector<int>(26, -1);       cnt = 0;     }     void Add(vector<int>& v) { ord = v; }   };   int main() {     Emsawy();     string a, b;     while (cin >> a >> b) {       vector<Node> L(a.size()), R(a.size());       int po = 0;       for (int i = 0; i < a.size(); i++) {         if (i > 0) L[i].Add(L[i - 1].ord);         if (po < b.size() && b[po] == a[i]) {           L[i].ord[b[po] -  a ] = i;           po++;         }         L[i].cnt = po;       }       po = b.size() - 1;       for (int i = a.size() - 1; i >= 0; i--) {         if (i < a.size() - 1) R[i].Add(R[i + 1].ord);         if (po >= 0 && b[po] == a[i]) {           R[i].ord[b[po] -  a ] = i;           po--;         }         R[i].cnt = b.size() - po - 1;       }       bool ok = 1;       for (int i = 0; i < a.size(); i++) {         int pl = L[i].ord[a[i] -  a ];         int pr = R[i].ord[a[i] -  a ];         if (pl == -1 && pr == -1) {           ok = 0;           break;         }         int mx = 0;         if (pl != -1) {           mx = max(mx, L[pl].cnt + (i + 1 < a.size() ? R[i + 1].cnt : 0));         }         if (pr != -1) {           mx = max(mx, R[pr].cnt + (i - 1 >= 0 ? L[i].cnt : 0));         }         if (mx < b.size()) {           ok = 0;           break;         }       }       if (ok)         cout <<  Yes  << endl;       else         cout <<  No  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long dp[102][102][2];   int m = 100000000;   int main() {     int n1, n2, k1, k2;     cin >> n1 >> n2 >> k1 >> k2;     int i, j, l1;     for (i = 0; i <= n1; i++) {       for (j = 0; j <= n2; j++) {         if (i + j == 0)           dp[i][j][0] = 1, dp[i][j][1] = 1;         else if ((i == 0 && j > k2) || (j == 0 && i > k1))           dp[i][j][0] = 0, dp[i][j][1] = 0;         else if (i == 0)           dp[i][j][0] = 0, dp[i][j][1] = 1;         else if (j == 0)           dp[i][j][0] = 1, dp[i][j][1] = 0;         else {           for (l1 = 1; l1 <= min(k1, i); l1++) {             dp[i][j][0] = (dp[i][j][0] + dp[i - l1][j][1]) % m;           }           for (l1 = 1; l1 <= min(k2, j); l1++) {             dp[i][j][1] = (dp[i][j][1] + dp[i][j - l1][0]) % m;           }         }       }     }     cout << (dp[n1][n2][0] + dp[n1][n2][1]) % m;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int n, m;     cin >> n >> m;     long long int a[n];     for (long long int i = 0; i < n; i++) cin >> a[i];     priority_queue<int> pq;     long long int res[n];     pq.push(a[0]);     res[0] = 0;     long long int cursum = a[0];     long long int curcount = 0;     for (long long int i = 1; i < n; i++) {       if (cursum + a[i] <= m) {         pq.push(a[i]);         cursum += a[i];         res[i] = curcount;       } else {         vector<long long int> temp;         while (cursum + a[i] > m) {           long long int x = pq.top();           cursum = cursum - x;           temp.push_back(x);           pq.pop();           curcount++;         }         res[i] = curcount;         temp.push_back(a[i]);         sort(temp.begin(), temp.end());         long long int ab = 0;         curcount++;         while (cursum <= m and ab < temp.size()) {           pq.push(temp[ab]);           ab++;           cursum += temp[ab - 1];           curcount--;         }       }     }     for (int i = 0; i < n; i++) cout << res[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     int ans = 0;     while (n != 0) {       ans++;       string s = to_string(n);       int maior = s[0] -  0 ;       for (int i = 0; i < s.size(); i++) {         maior = max(maior, s[i] -  0 );       }       n -= maior;     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> q[250000];   int color[250000];   int a[250000], b[250000];   int ssum[250000];   int main() {     int n;     cin >> n;     for (int i = 0; i < n - 1; i++) scanf( %d%d , &a[i], &b[i]);     for (int i = 1; i <= n; i++) scanf( %d , &color[i]);     int sum = 0;     for (int i = 0; i < n - 1; i++) {       if (color[a[i]] != color[b[i]]) {         ssum[a[i]]++;         ssum[b[i]]++;         sum++;       }     }     int flag = 0;     for (int i = 1; i <= n; i++)       if (sum - ssum[i] == 0) {         flag = i;         break;       }     if (flag)       printf( YES n%d , flag);     else       printf( NO );   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const int M = 20;   struct edge1 {     int v, w;   };   struct edge2 {     int u, v, w;   } e[N];   struct qry {     int u, w, id;   } q[N];   vector<edge1> G[N];   int n, m, c, a[N], dep[N], ans1[N], ans2[N], f[N][M], g[N][M];   void add(int u, int v, int w) {     G[u].push_back((edge1){v, w}), G[v].push_back((edge1){u, w});   }   void dfs(int u, int fa) {     f[u][0] = fa, dep[u] = dep[fa] + 1;     for (auto e : G[u])       if (e.v != fa) g[e.v][0] = e.w, dfs(e.v, u);   }   int dist(int x, int y) {     int ans = 0;     if (dep[x] < dep[y]) swap(x, y);     for (int i = 18; i >= 0; --i)       if (dep[f[x][i]] >= dep[y]) ans = max(ans, g[x][i]), x = f[x][i];     if (x == y) return ans;     for (int i = 18; i >= 0; --i)       if (f[x][i] != f[y][i])         ans = max(ans, max(g[x][i], g[y][i])), x = f[x][i], y = f[y][i];     return max(ans, max(g[x][0], g[y][0]));   }   namespace dsu {   int fa[N], sz[N], pl[N], mxw[N], val[N];   void reset() {     for (int i = 1; i <= n; ++i)       fa[i] = pl[i] = i, val[i] = a[i], mxw[i] = 0, sz[i] = 1;   }   int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); }   void Merge(int a, int b) {     int x = find(a), y = find(b);     if (sz[x] > sz[y]) swap(x, y);     if (val[x] > val[y])       val[y] = val[x], mxw[y] = mxw[x], pl[y] = pl[x];     else if (val[x] == val[y])       mxw[y] = max(max(mxw[x], mxw[y]), dist(pl[x], pl[y]));     fa[x] = y, sz[y] += sz[x];   }   }  // namespace dsu   bool cmp1(edge2 a, edge2 b) { return a.w > b.w; }   bool cmp2(qry a, qry b) { return a.w > b.w; }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n - 1; ++i) {       scanf( %d%d%d%d , &e[i].u, &e[i].v, &e[i].w, &c);       add(e[i].u, e[i].v, c);     }     dfs(1, 0);     for (int j = 1; j <= 18; ++j)       for (int i = 1; i <= n; ++i)         f[i][j] = f[f[i][j - 1]][j - 1],         g[i][j] = max(g[i][j - 1], g[f[i][j - 1]][j - 1]);     for (int i = 1; i <= m; ++i) scanf( %d%d , &q[i].w, &q[i].u), q[i].id = i;     sort(e + 1, e + n, cmp1);     sort(q + 1, q + m + 1, cmp2);     dsu ::reset();     for (int i = 1, j = 1; i <= m; ++i) {       for (; j <= n - 1 && e[j].w >= q[i].w; ++j) dsu ::Merge(e[j].u, e[j].v);       int res = dsu ::mxw[dsu ::find(q[i].u)];       res = max(res, dist(q[i].u, dsu ::pl[dsu ::find(q[i].u)]));       ans1[q[i].id] = dsu ::val[dsu ::find(q[i].u)], ans2[q[i].id] = res;     }     for (int i = 1; i <= m; ++i) printf( %d %d n , ans1[i], ans2[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double dist(long long int a, long long int b, long long int c,               long long int d) {     return sqrt(((a - c) * (a - c)) + (b - d) * (b - d));   }   struct p {     double val;     long long int index;   };   int comp(const void* a, const void* b) {     if (((struct p*)a)->val - ((struct p*)b)->val < 0) return -1;     if (((struct p*)a)->val - ((struct p*)b)->val > 0) return 1;     if (((struct p*)a)->val - ((struct p*)b)->val == 0) return 0;   }   int main() {     long long int ax, ay, bx, by, tx, ty, n;     cin >> ax;     cin >> ay;     cin >> bx;     cin >> by;     cin >> tx;     cin >> ty;     cin >> n;     long long int i, x, y;     struct p a[n], b[n], c[n];     double sum = 0;     for (i = 0; i < n; i++) {       cin >> x;       cin >> y;       sum += 2 * dist(tx, ty, x, y);       a[i].val = dist(ax, ay, x, y) - dist(tx, ty, x, y);       a[i].index = i;       b[i].val = dist(bx, by, x, y) - dist(tx, ty, x, y);       b[i].index = i;     }     qsort(a, n, sizeof(struct p), comp);     qsort(b, n, sizeof(struct p), comp);     if (a[0].index == b[0].index) {       sum += min(a[0].val,                  min(b[0].val, min(a[0].val + b[1].val, b[0].val + a[1].val)));     } else {       if (a[0].val < b[0].val) {         sum += a[0].val;         if (b[0].val < 0) sum += b[0].val;       } else {         sum += b[0].val;         if (a[0].val < 0) sum += a[0].val;       }     }     cout << setprecision(12) << fixed << sum << endl;     return 0;   }