malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   mt19937 mt_rand(       chrono::high_resolution_clock::now().time_since_epoch().count());   const int INF = 2e9;   const long long ML = 4e18;   long long Pow(long long x, int p) {     long long res = 1;     while (p) {       if (p & 1) res = (res * x) % 998244353;       x = (x * x) % 998244353;       p >>= 1;     }     return res;   }   void solve() {     int n, q;     cin >> n >> q;     vector<int> ax(n);     for (int i = (0); i < (n); ++i) cin >> ax[i];     sort((ax).begin(), (ax).end());     vector<long long> sum(n + 1, 0);     for (int i = (1); i < (n + 1); ++i) {       sum[i] = sum[i - 1] + ax[i - 1];       sum[i] %= 998244353;     }     long long inv = Pow(n, 998244353 - 2);     for (int i = (0); i < (q); ++i) {       int a, b;       cin >> a >> b;       int p = lower_bound((ax).begin(), (ax).end(), b) - ax.begin();       int big = n - p, small = p;       if (big < a) {         cout << 0 << endl;         continue;       } else {         long long bs = (sum[n] - sum[p] + 998244353) % 998244353, sm = sum[p];         long long ans =             bs * (big - a) % 998244353 * Pow(big, 998244353 - 2) % 998244353;         ans = (ans + sm * (big + 1 - a) % 998244353 *                          Pow(big + 1, 998244353 - 2) % 998244353) %               998244353;         cout << ans << endl;       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     solve();     return 0;   }
#include <bits/stdc++.h>   const long long MOD = 1e9 + 7;   const long long MAXN = 1e6 + 1;   using namespace std;   long long readInt() {     bool minus1 = false;     long long result = 0;     char ch;     ch = getchar();     while (true) {       if (ch ==  - ) break;       if (ch >=  0  && ch <=  9 ) break;       ch = getchar();     }     if (ch ==  - )       minus1 = true;     else       result = ch -  0 ;     while (true) {       ch = getchar();       if (ch <  0  || ch >  9 ) break;       result = result * 10 + (ch -  0 );     }     if (minus1)       return -result;     else       return result;   }   const int N = 3333;   int dx[] = {+1, -1, 0, 0};   int dy[] = {0, 0, +1, -1};   string dirs =  UDRL ;   int directiony[N];   int directionx[N];   long long curx = 5000000001LL, cury = 5000000001LL;   vector<long long> xler;   vector<long long> yler;   void add() {     xler.push_back(curx);     xler.push_back(curx - 1);     xler.push_back(curx + 1);     yler.push_back(cury);     yler.push_back(cury - 1);     yler.push_back(cury + 1);   }   int used[N][N];   int H, W;   void dfs(int x, int y) {     used[x][y] = 2;     for (int i = 0; i < 4; i++) {       int tmpx = x + dx[i];       int tmpy = y + dy[i];       if ((0 <= tmpx && tmpx < H) && (0 <= tmpy && tmpy < W) &&           used[tmpx][tmpy] == 0) {         dfs(tmpx, tmpy);       }     }   }   int main() {     int n = readInt();     add();     for (int i = 0; i < n; i++) {       char x;       int y;       cin >> x >> y;       for (int j = 0; j < 4; j++) {         if (dirs[j] == x) {           directionx[i] = dx[j] * y;           directiony[i] = dy[j] * y;           curx += directionx[i];           cury += directiony[i];           add();         }       }     }     sort(xler.begin(), xler.end());     sort(yler.begin(), yler.end());     xler.resize(unique(xler.begin(), xler.end()) - xler.begin());     yler.resize(unique(yler.begin(), yler.end()) - yler.begin());     H = xler.size();     W = yler.size();     curx = 5000000001LL;     cury = 5000000001LL;     long long compx = lower_bound(xler.begin(), xler.end(), curx) - xler.begin();     long long compy = lower_bound(yler.begin(), yler.end(), cury) - yler.begin();     for (int i = 0; i < n; i++) {       long long tmpx = curx + directionx[i];       long long tmpy = cury + directiony[i];       long long tmpcompx =           lower_bound((xler).begin(), (xler).end(), tmpx) - xler.begin();       long long tmpcompy =           lower_bound((yler).begin(), (yler).end(), tmpy) - yler.begin();       for (int j = min(tmpcompx, compx); j <= max(tmpcompx, compx); j++) {         for (int k = min(tmpcompy, compy); k <= max(tmpcompy, compy); k++) {           used[j][k] = 1;         }       }       curx = tmpx;       cury = tmpy;       compx = tmpcompx;       compy = tmpcompy;     }     dfs(0, 0);     long long res = 0;     for (int i = 0; i < H; i++) {       for (int j = 0; j < W; j++) {         if (used[i][j] != 2) {           res += (xler[i + 1] - xler[i]) * (yler[j + 1] - yler[j]);         }       }     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   const long long MAX_N = 3e5 + 10;   const long long MOD = 1e9 + 9;   using namespace std;   vector<int> g1[MAX_N];   vector<int> g[MAX_N];   vector<int> current;   int p[MAX_N];   int mark[MAX_N];   int d[MAX_N];   int h[MAX_N];   int n, m, k;   void rt(int v) {     mark[v]++;     for (auto x : g1[v]) {       if (!mark[x]) {         p[x] = v;         h[x] = h[v] + 1;         g[x].push_back(v);         g[v].push_back(x);         rt(x);       }     }   }   void solve() {     cout <<  CYCLES n ;     int ct = 0;     for (int i = 2; i <= n; ++i) {       int v = i;       if (g[v].size() > 1) continue;       ct++;       pair<int, int> pr;       int res = 1e9;       vector<pair<int, int>> vc;       for (auto x : g1[v]) {         if (x == p[v]) continue;         if ((h[v] - h[x]) % 3 != 2 and h[v] - h[x] + 1 < res) {           res = h[v] - h[x] + 1;           pr.second = v;           pr.first = x;         }         vc.push_back({h[x], x});       }       sort(vc.begin(), vc.end());       for (int i = 0; i < vc.size() - 1; ++i) {         int len = vc[i + 1].first - vc[i].first + 2;         if (len % 3 and res > len) {           res = len;           pr = {vc[i].second, vc[i + 1].second};         }       }       int now = pr.second;       cout << res <<   n ;       if (pr.second != v) cout << v <<   n ;       while (1) {         cout << now <<    ;         if (now == pr.first) break;         now = p[now];       }       cout <<   n ;       if (ct == k) exit(0);     }     exit(0);   }   void dfs(int v, int f) {     for (auto x : g[v]) {       if (x != f) {         d[x] = d[v] + 1;         dfs(x, v);       }     }   }   void sfd(int v, int f, int dest) {     current.push_back(v);     if (v == dest) {       cout << current.size() <<   n ;       for (auto x : current) cout << x <<    ;       exit(0);     }     for (auto x : g[v]) {       if (x != f) sfd(x, v, dest);     }     current.pop_back();   }   int main() {     cin >> n >> m >> k;     if (n == 1) return cout <<  PATH n1 n1 , 0;     if (n == 2) return cout <<  PATH n2 n1 2 , 0;     for (int i = 0; i < m; ++i) {       int u, v;       scanf( %d%d , &v, &u);       g1[u].push_back(v);       g1[v].push_back(u);     }     rt(1);     int now = 0, v = 1;     for (int i = 1; i <= n; ++i) {       if (h[i] > now) {         now = h[i];         v = i;       }     }     dfs(v, 0);     now = 0;     int best = 0;     for (int i = 1; i <= n; ++i) {       if (d[i] > now) {         now = d[i];         best = i;       }     }     if (n / (double)k <= now) {       cout <<  PATH n ;       sfd(v, 0, best);     } else       solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long mod = 1e9 + 7;   const int N = 100005;   int a[N * 2], ans[N];   stack<int> s;   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= 2 * n; i++) {       char op;       scanf(  %c , &op);       if (op ==  + ) {         a[i] = -1;       } else {         scanf( %d , &a[i]);       }     }     int t = n;     for (int i = 2 * n; i >= 1; i--) {       if (a[i] == -1) {         if (s.empty()) return puts( NO ), 0;         ans[t--] = s.top();         s.pop();       } else {         if (!s.empty() && a[i] > s.top()) return puts( NO ), 0;         s.push(a[i]);       }     }     puts( YES );     for (int i = 1; i <= n; i++) printf( %d%c , ans[i],    n [i == n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long mod = 1e9 + 7;   const int N = 1e5 + 10;   int n, m;   int main() {     scanf( %d%d , &n, &m);     long long sum1 = 0, sum2 = 0;     int t = n - n / 2;     for (int i = 1; i <= n; i++) {       sum1 += i - 1;       sum2 += abs(t - i);     }     int x, d;     long long sum3 = 0;     long long z1 = 0;     long long z2 = 0;     while (m--) {       scanf( %d%d , &x, &d);       sum3 += x;       if (d < 0)         z1 += d * sum2;       else         z2 += d * sum1;     }     double ans = sum3;     ans += 1.0 * (z1 + z2) / n;     printf( %.12f , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   int w[55];   int n;   int num_teams(int s) {     map<int, int> cnt;     for (int i = 0; i < n; ++i) {       ++cnt[w[i]];     }     int teams = 0;     for (int i = 0; i < n; ++i) {       int wt = w[i];       if (cnt[wt] == 0) continue;       if (wt >= s) continue;       if (cnt[s - wt] == 0) continue;       if (wt == (s - wt) && cnt[wt] < 2) continue;       --cnt[wt];       --cnt[s - wt];       ++teams;     }     return teams;   }   void solve() {     int max_teams = 0;     for (int s = 2; s <= 100; ++s) {       max_teams = max(max_teams, num_teams(s));     }     cout << max_teams <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int tt;     cin >> tt;     while (tt--) {       cin >> n;       for (int i = 0; i < n; ++i) {         cin >> w[i];       }       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e6 + 5;   const long long mod = 1e9 + 7;   const int inf = 0x7fffffff;   int a[maxn];   int main() {     int t;     cin >> t;     while (t--) {       int r, g, b, w;       cin >> r >> g >> b >> w;       int f = 0;       if (r % 2 == 1) f++;       if (g % 2 == 1) f++;       if (b % 2 == 1) f++;       if (w % 2 == 1) f++;       if (f <= 1)         cout <<  Yes  <<   n ;       else {         if (r && g && b) r--, g--, b--, w += 3;         f = 0;         if (r % 2 == 1) f++;         if (g % 2 == 1) f++;         if (b % 2 == 1) f++;         if (w % 2 == 1) f++;         if (f <= 1)           cout <<  Yes  <<   n ;         else           cout <<  No  <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, n, sum = 0, maxi, mini, start, end;     ;     cin >> a >> b >> n;     mini = min(a, b);     maxi = max(a, b);     if (n <= min(a, b)) {       cout << n + 1;     } else {       if (mini + maxi >= n) {         int k = n - maxi;         if (k < 0) {           k = 0;         }         sum = mini - k + 1;       }       cout << sum;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> v;   int main() {     v.push_back(1);     long long x, i, xx = 10, c = 2, cc = 0;     while (1) {       i = 1;       cc = c - 1;       xx = 0;       while (i <= c) {         xx = xx + pow(2, cc);         cc++;         i++;       }       c++;       if (xx <= 100000) {         v.push_back(xx);       } else {         break;       }     }     int sz = v.size();     long long nn;     cin >> nn;     for (int ii = sz - 1; ii >= 0; ii--) {       if (v[ii] <= nn && nn % v[ii] == 0) {         cout << v[ii] << endl;         return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   const int d = 998244353;   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<vector<long long int>> t(n + 1);     vector<long long int> init(k + 1, 0);     for (int i = 0; i <= n; i++) {       t[i] = init;     }     t[1][0] = m;     t[1][1] = 0;     for (int i = 2; i <= n; i++) {       for (int j = 0; j <= min(i - 1, k); j++) {         if (j >= 1) {           t[i][j] = (t[i - 1][j] + (m - 1) * t[i - 1][j - 1]) % d;         }         if (j == 0) {           t[i][0] = m;         }       }     }     cout << t[n][k];     return 0;   }
#include <bits/stdc++.h>   static const int MAXLEN = 1e6 + 12;   int len = 0;   char s[MAXLEN];   char *tokens[MAXLEN];   int token_ct = 0;   int max_depth = 0;   std::vector<char *> nested[MAXLEN];   int parse_comment_recursive(int start_token, int level) {     if (max_depth < level) max_depth = level;     nested[level].push_back(tokens[start_token++]);     int children = strtol(tokens[start_token++], NULL, 10);     while (children--) {       start_token = parse_comment_recursive(start_token, level + 1);     }     return start_token;   }   int main() {     while ((s[len] = getchar()) !=   n  && s[len] != EOF) ++len;     s[len] =   0 ;     tokens[token_ct++] = s;     for (int i = 0; i < len; ++i)       if (s[i] ==  , ) {         tokens[token_ct++] = s + i + 1;         s[i] =   0 ;       }     int parsed_tokens = 0;     while (parsed_tokens < token_ct)       parsed_tokens = parse_comment_recursive(parsed_tokens, 0);     printf( %d n , max_depth + 1);     for (int i = 0; i <= max_depth; ++i) {       for (std::vector<char *>::iterator j = nested[i].begin();            j != nested[i].end(); ++j) {         fputs(*j, stdout);         putchar(   );       }       putchar(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 0;   int n, k, x;   string s;   vector<int> v;   int main() {     int a, b, c, d, e, f;     int cnt = 0;     scanf( %d%d%d%d%d%d , &a, &b, &c, &d, &e, &f);     if (a == d) cnt++;     if (b == e) cnt++;     if (c == f) cnt++;     if (cnt >= 1) {       printf( YES n );     } else {       printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   char s[100100];   int n, ans = 1, tmp;   int main() {     scanf( %d %s , &n, s);     for (int i = 1; i < n; i++)       if (s[i] != s[i - 1])         ans++;       else         tmp++;     if (tmp) ans++;     if (tmp > 1) ans++;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int a, b;       cin >> a >> b;       if (a > b) {         if (a % 2 == b % 2)           cout <<  1  << endl;         else           cout <<  2  << endl;       } else if (a < b) {         if (a % 2 == b % 2)           cout <<  2  << endl;         else           cout <<  1  << endl;       } else         cout <<  0  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int keep[201];   int tt[201];   int first[201];   int main() {     while (cin >> n) {       n *= 2;       int sum = 0, n1 = 0, n2 = 0, s1 = 0, s2 = 0;       memset(keep, 0, sizeof(keep));       memset(first, 0, sizeof(first));       for (int i = 0; i < n; i++) {         int a;         cin >> a;         keep[a]++;         tt[i] = a;       }       for (int i = 10; i < 100; i++) {         if (keep[i] > 1) {           s1++;           s2++;           if (n1 > n2)             n1 += keep[i] / 2, n2 += (keep[i] - keep[i] / 2), first[i] = 2;           else             n2 += keep[i] / 2, n1 += (keep[i] - keep[i] / 2), first[i] = 1;         } else if (keep[i] == 1)           sum++;       }       int turn = 1;       if (s1 > s2) {         turn = 2;         s1 += sum / 2;         s2 += (sum - sum / 2);       } else {         turn = 2;         s2 += sum / 2;         s1 += (sum - sum / 2);       }       cout << s1 * s2 << endl;       for (int i = 0; i < n; i++) {         if (keep[tt[i]] > 1) {           cout << first[tt[i]];           first[tt[i]] = (first[tt[i]] == 2 ? 1 : 2);         } else if (keep[tt[i]] == 1) {           cout << turn;           turn = (turn == 2 ? 1 : 2);         }         if (i < n - 1)           cout <<    ;         else           cout << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   char second[N];   long long x, y, xx, yy;   int n;   bool check(long long days) {     long long px = x, py = y;     for (int i = 0; i < n; i++) {       long long mult = days / n;       if (i < (days % n)) mult++;       if (second[i] ==  U ) py += mult;       if (second[i] ==  D ) py -= mult;       if (second[i] ==  L ) px -= mult;       if (second[i] ==  R ) px += mult;     }     long long d = abs(xx - px) + abs(yy - py);     return d <= days;   }   int main() {     scanf( %lld%lld%lld%lld , &x, &y, &xx, &yy);     scanf( %d , &n);     scanf( %s , second);     long long lo = 0, hi = 1e18, ans = -1;     if (!check(hi)) {       cout << -1 << endl;       return 0;     }     while (hi - lo > 1) {       long long mid = (lo + hi) / 2;       if (check(mid)) {         ans = mid;         hi = mid;       } else {         lo = mid;       }     }     cout << hi << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, x, y;     cin >> n;     vector<pair<long long, long long>> a, b;     for (long long i = 0; i < n; i++) {       cin >> x >> y;       a.push_back(make_pair(x, y));     }     for (long long i = 0; i < n; i++) {       cin >> x >> y;       b.push_back(make_pair(x, y));     }     sort(a.begin(), a.end());     sort(b.begin(), b.end());     cout << a[0].first + b[n - 1].first <<     << a[0].second + b[n - 1].second;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[1024], f[10010];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, l = INT_MAX, r = INT_MIN, cnt = 0, src, dst;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> a[i];       ++f[a[i]];     }     for (int i = 0; i < 10001; ++i) {       if (f[i]) ++cnt;       if (cnt > 3) {         cout <<  Unrecoverable configuration. ;         return 0;       }     }     if (cnt == 1) {       cout <<  Exemplary pages. ;       return 0;     }     for (int i = 0; i < n; ++i) {       if (l > a[i]) {         l = a[i];         src = i + 1;       }       if (r < a[i]) {         r = a[i];         dst = i + 1;       }     }     int dif = r - l;     if (dif % 2) {       cout <<  Unrecoverable configuration. ;       return 0;     }     dif /= 2;     if (n == 2) {       cout << dif <<   ml. from cup #  << src <<   to cup #  << dst <<  . n ;       return 0;     }     for (int i = 0; i < n; ++i) {       if (i != src - 1 && i != dst - 1) {         if (l + dif != a[i] || r - dif != a[i]) {           cout <<  Unrecoverable configuration. ;           return 0;         }       }     }     cout << dif <<   ml. from cup #  << src <<   to cup #  << dst <<  . n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int M = 1000000007;   const int MM = 998244353;   const long double PI = acos(-1);   long long power(long long b, long long e, long long m) {     if (e == 0) return 1;     if (e & 1) return b * power(b * b % m, e / 2, m) % m;     return power(b * b % m, e / 2, m);   }   long long power(long long b, long long e) {     if (e == 0) return 1;     if (e & 1) return b * power(b * b, e / 2);     return power(b * b, e / 2);   }   template <typename T, typename U>   static inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   static inline void amax(T &x, U y) {     if (x < y) x = y;   }   template <typename T, typename U>   ostream &operator<<(ostream &os, const pair<T, U> &p) {     return os <<  (  << p.first <<  ,  << p.second <<  ) ;   }   const int N = 500005;   long long c[N];   map<long long, vector<pair<int, int>>> ed;   struct DSU {     vector<int> p, r;     int n;     int components;     DSU(int _) : n(_), components(_) {       p.resize(n + 1);       r.resize(n + 1);       for (int i = 0; i <= _; ++i) p[i] = i, r[i] = 0;     }     int parent(int i) { return p[i] = (p[i] == i) ? i : parent(p[i]); }     void merge(int x, int y) {       x = parent(x);       y = parent(y);       if (x == y) return;       if (r[x] > r[y])         p[y] = x;       else         p[x] = y;       if (r[x] == r[y]) ++r[y];       --components;     }   };   int _runtimeTerror_() {     long long ans = 0;     int n, m, k;     cin >> n >> m >> k;     for (int i = 1; i <= n; ++i) cin >> c[i];     ans += power(2, n, M) * ((1LL << k) % M) % M;     for (int i = 1; i <= m; ++i) {       int x, y;       cin >> x >> y;       ed[c[x] ^ c[y]].push_back({x, y});     }     DSU dsu(n);     for (auto &j : ed) {       int cnt = 0;       for (auto &k : j.second)         dsu.p[k.first] = k.first, dsu.p[k.second] = k.second;       dsu.components = n;       for (auto &k : j.second) {         dsu.merge(k.first, k.second);       }       ans -= power(2, n, M);       ans %= M;       ans += power(2, dsu.components, M);       ans = (ans + M) % M;     }     cout << ans <<   n ;     return 0;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     while (TESTS--) _runtimeTerror_();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int const N = 1e5 + 5;   int di[] = {1, 1, -1, -1};   int dj[] = {1, -1, 1, -1};   long long n, k;   long long solv() {     long long st = 0, ed = k, ans;     while (st <= ed) {       long long x = (k * (k + 1)) / 2;       long long mid = (st + ed) / 2;       long long xx = k - mid;       x -= ((xx * (xx + 1)) / 2) + mid - 1;       if (x < n)         st = mid + 1;       else if (x >= n)         ans = mid, ed = mid - 1;       else         return mid;     }     return ans;   }   int main() {     cin >> n >> k;     if ((k * (k + 1)) / 2 - k + 1 < n) return cout << -1, 0;     cout << solv();   }
#include <bits/stdc++.h>   using namespace std;   const int N = 300010;   int t, n, x[N], y[N];   long long a[N], val[N];   int main() {     cin >> t;     while (t--) {       scanf( %d , &n);       long long sum = 0;       for (int i = 1; i <= n; ++i) {         scanf( %lld , a + i);         sum += a[i];       }       if (sum % n) {         puts( -1 );         continue;       }       sum /= n;       int m = 0;       for (int i = 2; i <= n; ++i) {         long long req = a[i] % i;         if (req) {           req = i - req, ++m;           x[m] = 1, y[m] = i, val[m] = req;           a[1] -= req, a[i] += req;         }         ++m;         x[m] = i, y[m] = 1, val[m] = a[i] / i;         a[1] += a[i], a[i] = 0;       }       for (int i = 2; i <= n; ++i) {         ++m;         x[m] = 1, y[m] = i, val[m] = sum;         a[1] -= sum, a[i] += sum;       }       printf( %d n , m);       for (int i = 1; i <= m; ++i) printf( %d %d %lld n , x[i], y[i], val[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t, n, i, x, y, best, w, cw, rt, a[200200];   vector<int> g[200200];   bool all;   void dfs(int i, int p, int cur, int dst) {     if (dst > best) {       best = dst;       cw = cur;       w = i;     }     for (int j : g[i])       if (j != p) {         dfs(j, i, ((a[j] == 0) ? cur : a[j]),             (dst + int(a[j] != 0 && a[j] != cur)));       }   }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       all = true;       for (i = 1; i <= n; i++) {         scanf( %d , &a[i]);         if (a[i] != 0) {           all = false;           rt = i;         }         g[i].clear();       }       for (i = 1; i < n; i++) {         scanf( %d%d , &x, &y);         g[x].push_back(y);         g[y].push_back(x);       }       if (all) {         puts( 1 );         continue;       }       best = 0;       w = rt;       cw = a[rt];       dfs(rt, 0, a[rt], 0);       best = 0;       dfs(w, 0, cw, 0);       printf( %d n , (best + 3) / 2);     }     return 0;   }