malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int mod = 998244353;   const int M = 1e6 + 10;   const int N = 1e5 + 10;   inline long long read() {     long long b = 1, sum = 0;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) b = -1;       c = getchar();     }     while (isdigit(c)) {       sum = sum * 10 + c -  0 ;       c = getchar();     }     return b * sum;   }   int a[N], in[N], d[N];   int n, m, k, T;   int vis[N];   vector<int> e[N];   vector<pair<int, int> > ans;   void dfs(int x, int _fa, int need) {     int i, j;     if (x == 1) {       for (i = 0; i < e[x].size(); i++) {         int y = e[x][i];         ans.push_back({y, i + 1});         dfs(y, x, i);       }       return;     }     int cha = need - in[x] + 1, sum = 0;     if (in[x] == 1) {       ans.push_back({x, need});     } else if (cha >= 0) {       ans.push_back({x, cha});       for (i = 0; i < e[x].size(); i++) {         int y = e[x][i];         if (y == _fa) continue;         ans.push_back({y, cha + sum + 1});         dfs(y, x, cha + sum);         sum++;       }     } else {       int now = need + 1, sum = 0;       for (i = 0; sum < -cha && i < e[x].size(); i++) {         int y = e[x][i];         if (y == _fa) continue;         ans.push_back({y, now + 1});         dfs(y, x, now);         now++;         sum++;       }       ans.push_back({x, 0});       sum = 0;       for (; i < e[x].size(); i++) {         int y = e[x][i];         if (y == _fa) continue;         ans.push_back({y, sum + 1});         dfs(y, x, sum);         sum++;       }     }     ans.push_back({_fa, need + 1});   }   int main() {     int i, j;     n = read();     for (i = 1; i <= n - 1; i++) {       int x = read(), y = read();       e[x].push_back(y);       e[y].push_back(x);       in[x]++;       in[y]++;     }     ans.push_back({1, 0});     dfs(1, 0, 0);     printf( %d n , ans.size());     for (i = 0; i < ans.size(); i++) {       printf( %d %d n , ans[i].first, ans[i].second);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int maxn = 3e5 + 100;   int t, T, n, k;   int A[maxn];   int B[maxn];   int main() {     ios::sync_with_stdio(false);     cin >> T;     while (T--) {       cin >> n;       int sum = 0;       for (int i = 1; i <= n; i++) {         cin >> A[i];         sum += A[i];         B[i] = A[i];       }       if (sum % n) {         cout << -1 << endl;         continue;       }       sum /= n;       int cnt = 0;       for (int i = 2; i <= n; i++) {         int d = (A[i] + i - 1) / i * i - A[i];         if (d <= A[1]) {           A[i] += d;           A[1] -= d;           cnt++;         }         int x = A[i] / i * i;         A[1] += x;         A[i] -= x;       }       int ok = 1;       for (int i = n; i > 1; i--) {         if (A[i] > sum) ok = 0;         A[1] -= (sum - A[i]);         if (A[1] < 0) ok = 0;         if (!ok) break;       }       if (!ok) {         cout << -1 << endl;         continue;       }       if (ok) {         cout << 2 * (n - 1) + cnt << endl;         for (int i = 2; i <= n; i++) {           int d = (B[i] + i - 1) / i * i - B[i];           if (d <= B[1]) {             B[i] += d;             B[1] -= d;             cout << 1 <<     << i <<     << d << endl;           }           int x = B[i] / i * i;           B[1] += x;           B[i] -= x;           cout << i <<     << 1 <<     << x / i << endl;         }         for (int i = n; i > 1; i--) {           int x = sum - B[i];           B[1] -= x;           B[i] += x;           cout << 1 <<     << i <<     << x << endl;         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, v, k1 = 0, k2 = 0;     int how_many1 = 0;     vector<int> molt[99 + 1];     scanf( %d , &n);     short b[n << 1];     for (int i = 0; i < (n << 1); i++) {       scanf( %d , &v);       molt[v].push_back(i);     }     for (int v = 10; v <= 99; v++)       if (molt[v].size() >= 2) {         k1++;         k2++;         how_many1++;         b[molt[v][0]] = 1;         b[molt[v][1]] = 2;       }     for (int v = 10; v <= 99; v++)       if (molt[v].size() == 1) {         if (k1 <= k2) {           k1++;           how_many1++;           b[molt[v][0]] = 1;         } else {           k2++;           b[molt[v][0]] = 2;         }       }     for (int v = 10; v <= 99; v++) {       for (int i = 2; i < molt[v].size(); i++) {         if (how_many1 < n) {           b[molt[v][i]] = 1;           how_many1++;         } else {           b[molt[v][i]] = 2;         }       }     }     printf( %d n , k1 * k2);     for (int i = 0; i < 2 * n; i++) printf( %d  , b[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[105][3];   vector<int> vec[105];   bool vis[105];   void dfs(int i) {     vis[i] = true;     for (int j = 0; j < vec[i].size(); j++) {       if (!vis[vec[i][j]]) dfs(vec[i][j]);     }     return;   }   int main() {     int n, i, cnt = 0, j;     cin >> n;     for (i = 1; i <= n; i++) {       scanf( %d%d , &arr[i][0], &arr[i][1]);     }     for (i = 1; i <= n; i++) {       for (j = i + 1; j <= n; j++) {         if (i != j) {           if (arr[i][0] == arr[j][0] || arr[i][1] == arr[j][1]) {             vec[i].push_back(j);             vec[j].push_back(i);           }         }       }     }     for (i = 1; i <= n; i++) {       if (!vis[i]) {         cnt++;         dfs(i);       }     }     cout << cnt - 1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int kMaxN = 1 + 50;   char A[kMaxN][kMaxN], B[kMaxN][kMaxN];   int N, M;   int type;   void ReadInput() {     scanf( %d%d , &N, &M);     for (int i = 1; i <= N; i++) {       scanf( %s , A[i] + 1);     }     for (int i = 1; i <= N; i++) {       scanf( %s , B[i] + 1);     }     if (N % 2 == 0) {       type = 0;     } else {       type = 1;     }   }   bool Perfect(char mat[kMaxN][kMaxN]) {     if (type == 0) {       for (int i = 1; i <= N; i++) {         for (int j = 1; j <= M; j++) {           if (i % 2 == 1 && mat[i][j] !=  U ) {             return false;           }           if (i % 2 == 0 && mat[i][j] !=  D ) {             return false;           }         }       }       return true;     } else {       for (int i = 1; i <= N; i++) {         for (int j = 1; j <= M; j++) {           if (j % 2 == 1 && mat[i][j] !=  L ) {             return false;           }           if (j % 2 == 0 && mat[i][j] !=  R ) {             return false;           }         }       }       return true;     }   }   vector<pair<int, int> > Normalize(char mat[kMaxN][kMaxN]) {     vector<pair<int, int> > moves;     bool now = 0;     while (!Perfect(mat)) {       if (now == 0) {         for (int i = 1; i < N; i++) {           for (int j = 1; j < M; j++) {             if (mat[i][j] ==  U  && mat[i][j + 1] ==  U  &&                 mat[i + 1][j] ==  D  && mat[i + 1][j + 1] ==  D ) {               mat[i][j] = mat[i + 1][j] =  L ;               mat[i][j + 1] = mat[i + 1][j + 1] =  R ;               moves.push_back(make_pair(i, j));             }           }         }       } else {         for (int i = 1; i < N; i++) {           for (int j = 1; j < M; j++) {             if (mat[i][j] ==  L  && mat[i + 1][j] ==  L  &&                 mat[i][j + 1] ==  R  && mat[i + 1][j + 1] ==  R ) {               mat[i][j] = mat[i][j + 1] =  U ;               mat[i + 1][j] = mat[i + 1][j + 1] =  D ;               moves.push_back(make_pair(i, j));             }           }         }       }       now ^= 1;     }     return moves;   }   int main() {     ReadInput();     vector<pair<int, int> > first = Normalize(A);     vector<pair<int, int> > second = Normalize(B);     reverse(second.begin(), second.end());     printf( %d n , (int)first.size() + (int)second.size());     for (auto p : first) {       printf( %d %d n , p.first, p.second);     }     for (auto p : second) {       printf( %d %d n , p.first, p.second);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int MX = 1e5 + 6;   long long l, r;   int main() {     cin >> l >> r;     puts( YES );     for (long long i = l; i <= r; i += 2) cout << i <<     << i + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, m, a, b;       cin >> n >> m >> a >> b;       if (n * a != m * b) {         cout <<  NO  << endl;         continue;       }       int index = 0;       cout <<  YES  << endl;       for (int i = 0; i < n; ++i) {         for (int j = 0; j < m; ++j) {           cout << ((index + j) % m < a ? 1 : 0);         }         index -= a;         if (index < 0) index += m;         cout << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct dot {     string f, s;   };   bool cmp(dot x, dot y) {}   int main() {     int i, j, k, n;     int p[100010];     dot A[100010];     cin >> n;     for (int(i) = int(1); i <= int(n); i++) {       cin >> A[i].f >> A[i].s;     }     string las =   ;     for (int(i) = int(1); i <= int(n); i++) {       cin >> p[i];     }     bool OK = true;     las = min(A[p[1]].f, A[p[1]].s);     for (int(i) = int(2); i <= int(n); i++) {       if (min(A[p[i]].f, A[p[i]].s) > las)         las = min(A[p[i]].f, A[p[i]].s);       else if (max(A[p[i]].f, A[p[i]].s) > las) {         las = max(A[p[i]].f, A[p[i]].s);       } else {         OK = false;         break;       }     }     if (OK)       cout <<  YES n ;     else       cout <<  NO  << endl;   }
#include <bits/stdc++.h>   using namespace std;   int f[1005][1005], s[1005][1005];   int n, k, a[1005], ans;   inline int X(int x) {     for (int i = 1; i <= k; i++)       for (int j = 1; j <= n; j++) f[i][j] = s[i][j] = 0;     for (int i = 1; i <= n; i++) f[1][i] = 1;     for (int i = 1; i <= k; i++) {       int l = 1;       for (int j = 1; j <= n; j++) {         while (a[j] - a[l] >= x) l++;         f[i][j] = (f[i][j] + s[i - 1][l - 1]) % 998244353;         s[i][j] = (f[i][j] + s[i][j - 1]) % 998244353;       }     }     int ret = 0;     for (int i = k; i <= n; i++) (ret += f[k][i]) %= 998244353;     return ret;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     for (int i = 1; i <= (a[n]) / (k - 1); i++) (ans += X(i)) %= 998244353;     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   long long num[30];   int main() {     int n, m;     long long k;     string temp;     scanf( %d%I64d , &n, &k);     long long cnt = 0;     cin >> temp;     for (int i = 0; i < n; i++) {       num[temp[i] -  A  + 1]++;     }     for (int i = 0; i < 26 && k != 0; i++) {       m = 1;       for (int j = 2; j <= 26; j++) {         if (num[m] < num[j]) m = j;       }       if (num[m] <= k) {         cnt += num[m] * num[m];         k -= num[m];         num[m] = 0;       } else {         cnt += k * k;         break;       }     }     printf( %I64d n , cnt);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int c[1000010];   int mp[1000];   int in[1000010];   int fir[1000010];   int ans[1000010];   vector<int> v[1000010];   int calc(int nod) {     if (c[nod] == 5) return fir[nod] = in[nod];     if (c[nod] == 4) return fir[nod] = 1 - calc(v[nod][0]);     int q1 = calc(v[nod][0]);     int q2 = calc(v[nod][1]);     if (c[nod] == 3) return fir[nod] = q1 ^ q2;     if (c[nod] == 2) return fir[nod] = q1 | q2;     if (c[nod] == 1) return fir[nod] = q1 & q2;   }   void dfs(int nod, int ch) {     if (c[nod] == 5) {       ans[nod] = ch;       return;     }     if (c[nod] == 4) {       dfs(v[nod][0], ch);       return;     }     if (ch == 0) {       dfs(v[nod][0], 0);       dfs(v[nod][1], 0);       return;     }     int q1 = fir[v[nod][0]];     int q2 = fir[v[nod][1]];     if (c[nod] == 3) {       if ((q1 ^ (!q2)) == fir[nod]) {         dfs(v[nod][1], 0);       } else {         dfs(v[nod][1], 1);       }       if ((q2 ^ (!q1)) == fir[nod]) {         dfs(v[nod][0], 0);       } else {         dfs(v[nod][0], 1);       }     }     if (c[nod] == 2) {       if ((q1 | (!q2)) == fir[nod]) {         dfs(v[nod][1], 0);       } else {         dfs(v[nod][1], 1);       }       if ((q2 | (!q1)) == fir[nod]) {         dfs(v[nod][0], 0);       } else {         dfs(v[nod][0], 1);       }     }     if (c[nod] == 1) {       if ((q1 & (!q2)) == fir[nod]) {         dfs(v[nod][1], 0);       } else {         dfs(v[nod][1], 1);       }       if ((q2 & (!q1)) == fir[nod]) {         dfs(v[nod][0], 0);       } else {         dfs(v[nod][0], 1);       }     }   }   int main() {     memset(ans, -1, sizeof ans);     mp[ A ] = 1;     mp[ O ] = 2;     mp[ X ] = 3;     mp[ N ] = 4;     mp[ I ] = 5;     cin >> n;     for (int i = 1; i <= n; i++) {       string s;       cin >> s;       if (s ==  IN ) {         int a;         cin >> a;         c[i] = 5;         in[i] = a;       } else if (s ==  NOT ) {         int a;         cin >> a;         c[i] = mp[ N ];         v[i].push_back(a);       } else {         int a, b;         cin >> a >> b;         v[i].push_back(a);         v[i].push_back(b);         c[i] = mp[s[0]];       }     }     calc(1);     dfs(1, 1);     string s1 =   ;     for (int i = 1; i <= n; i++) {       if (ans[i] != -1) {         if (ans[i])           ans[i] = 1 - fir[1];         else           ans[i] = fir[1];         s1 += char( 0  + ans[i]);       }     }     cout << s1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[1010];   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     sort(a, a + n);     int ans = 0;     for (int i = 0; i < n; i++) {       if (a[i] <= k * 2) {         k = max(k, a[i]);         continue;       } else {         k = k * 2;         ans++;         i--;       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using std::string;   int main() {     int n, k;     cin >> n >> k;     if (n * (n - 1) / 2 > k) {       int i = 0, j = 0;       while (n--) {         cout << i <<     << j++ << endl;       }     } else       cout <<  no solution ;   }
#include <bits/stdc++.h>   int main() {     int g[5][5];     int ans = 0;     for (int i = 0; i < 5; i++)       for (int j = 0; j < 5; j++) scanf( %d , &g[i][j]);     for (int i1 = 0; i1 < 5; i1++) {       for (int i2 = 0; i2 < 5; i2++) {         if (i2 == i1) continue;         for (int i3 = 0; i3 < 5; i3++) {           if (i3 == i1 || i3 == i2) continue;           for (int i4 = 0; i4 < 5; i4++) {             if (i4 == i1 || i4 == i2 || i4 == i3) continue;             for (int i5 = 0; i5 < 5; i5++) {               if (i5 == i1 || i5 == i2 || i5 == i3 || i5 == i4) continue;               ans = std::max(ans, g[i1][i2] + g[i2][i1] + g[i2][i3] + g[i3][i2] +                                       g[i3][i4] + g[i3][i4] + g[i4][i3] +                                       g[i4][i3] + g[i4][i5] + g[i4][i5] +                                       g[i5][i4] + g[i5][i4]);             }           }         }       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, ans = 1;   long long C(long long m, long long n) {     long long p = 1;     for (long long i = 1; i <= n; i++) p *= i;     for (long long i = 1; i <= m; i++) p /= i;     for (long long i = 1; i <= n - m; i++) p /= i;     return p;   }   signed main() {     cin >> n;     for (long long i = 1; i <= n / 2 - 1; i++) ans *= i;     ans *= ans;     ans *= C(n / 2, n);     ans /= 2;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int T[100010], first[100010], canKill[100010], ans = 0;   char flag;   int X, M, N, fle = 0, A[100010];   int main() {     scanf( %d %d , &N, &M);     memset(T, 0, sizeof(T));     memset(first, 0, sizeof(first));     memset(canKill, 0, sizeof(canKill));     ans = 0;     for (int i = 1; i <= N; i++) {       scanf( %d , &A[i]);       if (A[i] > 0)         T[A[i]]++;       else {         first[-1 * A[i]]++;         fle++;       }     }     for (int i = 1; i <= N; i++) {       if (fle - first[i] + T[i] == M) {         canKill[i] = 1;         ans++;       }     }     for (int i = 1; i <= N; i++) {       if (A[i] > 0) {         if (canKill[A[i]] == 1) {           if (ans == 1)             printf( Truth n );           else             printf( Not defined n );         } else {           printf( Lie n );         }       } else {         A[i] = A[i] * -1;         if (canKill[A[i]] == 0) {           printf( Truth n );         } else {           if (ans == 1)             printf( Lie n );           else             printf( Not defined n );         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     vector<long long> v(2000000);     for (long long i = 0; i < 2000000; i++) v[i] = 0;     for (long long i = 0; i < n; i++) {       long long t1;       cin >> t1;       v[t1]++;     }     long long t = 0;     long long l = 1;     long long r = 1000000;     while (n > 0) {       t++;       l++;       r--;       if (v[l]) n--;       if (v[r]) n--;     }     cout << t;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n = 0;     cin >> n;     string s;     vector<vector<int> > v(5, vector<int>(0));     for (int i = 0; i < n; i++) {       cin >> s;       vector<int> cnt(5, 0);       int sum = 0;       for (int j = 0; j < s.size(); j++) {         cnt[s[j] -  a ]++;         sum++;       }       for (int j = 0; j < 5; j++) {         v[j].push_back(cnt[j] - (sum - cnt[j]));       }     }     long long ans = -1;     for (int i = 0; i < 5; i++) {       sort(v[i].rbegin(), v[i].rend());       long long sum = 0;       long long cnt = 0;       for (int j = 0; j < v[i].size(); j++) {         sum += v[i][j];         if (sum > 0) {           cnt++;         }       }       ans = max(ans, cnt);     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 0;     cin >> t;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long read() {     char x = getchar();     long long ans = 0, flag = 1;     while (!isdigit(x))       if (x ==  - )         flag = -1, x = getchar();       else         x = getchar();     while (isdigit(x)) ans = ans * 10 + x -  0 , x = getchar();     return ans * flag;   }   long long n, a[200005], ans, las;   map<long long, long long> mp;   signed main() {     cin >> n;     for (long long i = 1; i <= n; i++) cin >> a[i];     for (long long i = 1; i <= n; i++) a[i] += a[i - 1];     mp[0] = 0;     las = 0;     for (long long i = 1; i <= n; i++) {       if (mp.find(a[i]) != mp.end()) {         if (mp[a[i]] >= las) {           ans -= (mp[a[i]] - las + 1) * ((n - i + 1));           las = mp[a[i]] + 1;         }       }       mp[a[i]] = i;     }     cout << ans + n * (n + 1) / 2;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std;   ;   int n;   long long mod = 1000 * 1000 * 1000 + 7;   const int maxn = 1000005;   int gcd(int a, int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long d[maxn];   long long cnt[maxn];   long long pow2[maxn];   long long coef[maxn];   long long bad[maxn];   int main() {     pow2[0] = 1;     for (int i = 1; i < maxn; ++i) {       pow2[i] = pow2[i - 1] * 2;       pow2[i] %= mod;     }     cin >> n;     vector<int> v;     v.resize(n);     for (int i = 0; i < n; ++i) {       scanf( %d , &v[i]);       cnt[v[i]]++;     }     for (int i = 2; i < maxn; ++i) {       for (int j = i; j < maxn; j += i) {         d[i] += cnt[j];         if (i != j) bad[j] += i;         bad[j] %= mod;       }     }     long long ans = 0;     for (int i = maxn - 1; i >= 2; --i) {       if (d[i] == 0) continue;       coef[i] = (1LL * ((d[i] * pow2[d[i] - 1]) % mod)) % mod;       for (int j = i * 2; j < maxn; j += i) coef[i] -= coef[j];       ans += i * coef[i];       ans %= mod;       ans += mod;       ans %= mod;     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int x, int y) {     if (y == 0)       return x;     else       return gcd(y, x % y);   }   vector<int> ed[200001];   int an[200001];   int b[200001];   set<int> o[2000001];   void dfs(int x, int g, int p) {     int i;     if (x == 1) {       an[x] = b[1];       o[x].insert(0);     } else {       o[x].insert(g);       an[x] = g;       if (o[p].size()) {         set<int>::iterator f = o[p].begin();         for (i = 0; i < o[p].size(); i++) {           int y = gcd(b[x], *f);           an[x] = max(an[x], y);           if (y != 1) o[x].insert(y);           f++;         }       }     }     g = gcd(g, b[x]);     for (i = 0; i < ed[x].size(); i++) {       if (ed[x][i] != p) {         dfs(ed[x][i], g, x);       }     }   }   int main() {     int n;     cin >> n;     int i;     for (i = 1; i <= n; i++) cin >> b[i];     for (i = 1; i < n; i++) {       int x, y;       cin >> x >> y;       ed[x].push_back(y);       ed[y].push_back(x);     }     dfs(1, 0, 0);     for (i = 1; i <= n; i++) cout << an[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   int main() {     char arr[200];     int i, j, jp, g, t, cl, jm, a, m, n;     scanf( %d%d , &cl, &jm);     scanf( %c , &arr[0]);     for (i = 1; i <= cl; i++) scanf( %c , &arr[i]);     for (i = 1; i <= cl; i++) {       if (arr[i] ==  G ) {         g = i;       }       if (arr[i] ==  T ) {         t = i;       }     }     if (g > t) {       m = t;       n = g;       a = g - t;     } else {       m = g;       n = t;       a = t - g;     }     for (i = m; i < n; i = i + jm) {       if (arr[i] ==  # ) {         printf( NO n );         return 0;       }     }     if ((a % jm) == 0)       printf( YES n );     else       printf( NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double eps = 1e-9;   const int INF = 1e9 + 7;   const int MAXN = int(2e5 + 7);   int n, k, b, ans;   long long cur = 1, t;   int main() {     cin >> k >> b >> n >> t;     while (cur <= t) {       cur = cur * k + b;       n--;     }     cout << max(0, ++n);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void boost() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   const int N = 2e5 + 5;   int arr[N], ans[N];   vector<int> adj[N];   vector<int> factor[2][N];   bool avail[N];   void dfs(int u, int p, int d) {     if (u != 1) {       for (auto x : factor[0][p])         if (arr[u] % x == 0)           factor[0][u].push_back(x);         else           factor[1][u].push_back(x);       for (auto x : factor[1][p])         if (arr[u] % x == 0) factor[1][u].push_back(x);     }     if (d == 2) {       for (int i = 1; i * i <= arr[u]; i++) {         if (arr[u] % i == 0) {           if (!avail[i]) factor[1][u].push_back(i);         }         if (i * i != arr[u] && (arr[u] % (arr[u] / i)) == 0) {           if (!avail[arr[u] / i]) {             factor[1][u].push_back(arr[u] / i);           }         }       }     }     for (auto x : factor[0][u]) ans[u] = max(ans[u], x);     for (auto x : factor[1][u]) ans[u] = max(ans[u], x);     for (auto v : adj[u]) {       if (v != p) dfs(v, u, d + 1);     }   }   void solve() {     int n;     cin >> n;     for (int i = 1; i < n + 1; ++i) {       cin >> arr[i];     }     int u, v;     for (int i = 0; i < n - 1; ++i) {       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     for (int i = 1; i * i <= arr[1]; i++) {       if (arr[1] % i == 0) {         factor[0][1].push_back(i);         avail[i] = true;       }       if (i * i != arr[1] && (arr[1] % (arr[1] / i)) == 0) {         factor[0][1].push_back(arr[1] / i);         avail[arr[1] / i] = true;       }     }     dfs(1, 0, 1);     for (int i = 1; i < n + 1; ++i) {       cout << ans[i] <<    ;     }     return;   }   int main() {     boost();     int tc = 1;     while (tc--) solve();     return 0;   }