malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int k;   long long s[16], sum;   map<long long, int> id;   vector<long long> v;   bool vis[16];   bool dp[1 << 15];   vector<pair<int, int> > way[1 << 15];   int pre[1 << 15];   pair<int, int> ans[16];   int main() {     scanf( %d , &k);     for (int i = 0; i < k; i++) {       int n;       scanf( %d , &n);       while (n--) {         int x;         scanf( %d , &x);         s[i] += x, sum += x;         id[x] = i;         v.push_back(x);       }     }     if (sum % k)       printf( No n );     else {       sum /= k;       for (auto x : v) {         for (int i = 0; i < k; i++) vis[i] = false;         long long now = x;         int mask = 0;         vector<pair<int, int> > opt;         bool f = true;         while (!vis[id[now]]) {           vis[id[now]] = true;           long long pre = now;           now = sum - s[id[now]] + now;           if (id.count(now) == 0) {             f = false;             break;           }           if (vis[id[now]] && now != x) {             f = false;             break;           }           mask |= 1 << id[pre];           opt.push_back(pair<int, int>(now, pre));         }         if (f == true) way[mask] = opt;       }       dp[0] = 1;       for (int i = 1; i < (1 << k); i++) {         for (int j = i - 1; j != i; j = (j - 1) & i) {           if (dp[j] && !way[i ^ j].empty()) {             dp[i] = 1;             pre[i] = j;             break;           }         }       }       if (!dp[(1 << k) - 1])         printf( No n );       else {         printf( Yes n );         int now = (1 << k) - 1;         while (now) {           int x = now ^ (pre[now]);           for (auto p : way[x])             ans[id[p.first]] = pair<int, int>(p.first, id[p.second]);           now = pre[now];         }         for (int i = 0; i < k; i++)           printf( %d %d n , ans[i].first, ans[i].second + 1);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool mark[100007] = {0};   vector<pair<long long int, long long int> > adj[100007];   vector<long long int> ans;   long long int d[100007];   long long int par[100007];   void dijkstra(long long int v) {     d[v] = 0;     long long int u;     priority_queue<pair<long long int, long long int>,                    vector<pair<long long int, long long int> >,                    greater<pair<long long int, long long int> > >         pq;     pq.push(make_pair(d[v], v));     while (!pq.empty()) {       u = pq.top().second;       pq.pop();       if (mark[u]) continue;       mark[u] = true;       for (auto p : adj[u])         if (d[p.first] > d[u] + p.second) {           par[p.first] = u;           d[p.first] = d[u] + p.second;           pq.push({d[p.first], p.first});         }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, m;     cin >> n >> m;     for (long long int i = 1; i <= m; i++) {       long long int a, b, w;       cin >> a >> b >> w;       adj[a].push_back(make_pair(b, w));       adj[b].push_back(make_pair(a, w));     }     for (long long int i = 1; i <= n; i++) {       d[i] = 100000000000000;       par[i] = -1;     }     dijkstra(1);     if (d[n] == 100000000000000)       cout <<  -1  << endl;     else {       long long int x = n;       while (x != 1) {         ans.push_back(x);         x = par[x];       }       ans.push_back(1);       reverse(ans.begin(), ans.end());       for (auto it : ans) cout << it <<    ;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mxn = 1e5 + 5;   const int inf = 1e9;   vector<int> adj[mxn];   bool f1[mxn], f2[mxn];   bool done;   int ask(char x, int y) {     cout << x <<     << y << endl;     int r;     cin >> r;     return r;   }   int dfs(int cur, int prev) {     if (done) return -1;     if (f1[cur]) {       done = true;       int x = ask( A , cur);       if (f2[x]) return cur;       return -1;     }     for (int u : adj[cur]) {       if (u == prev) continue;       int x = dfs(u, cur);       if (done) return x;     }     return -1;   }   void solve() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       adj[i].clear();       f1[i] = false;       f2[i] = false;     }     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     int k1;     cin >> k1;     int x[k1];     for (int i = 0; i < k1; i++) {       cin >> x[i];       f1[x[i]] = true;     }     int k2;     cin >> k2;     int y[k2];     for (int i = 0; i < k2; i++) {       cin >> y[i];       f2[y[i]] = true;     }     done = false;     int r = ask( B , y[0]);     cout <<  C   << dfs(r, -1) << endl;   }   int main() {     int t = 1;     cin >> t;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char s[3];     cin >> s;     int a;     char b;     b = s[0];     a = s[1];     if ((b ==  a  || b ==  h ) && (a ==  1  || a ==  8 ))       cout << 3;     else if ((b ==  a  || b ==  h ) && (a >  1  && a <  8 ))       cout << 5;     else if ((a ==  1  || a ==  8 ) || (a >  a  && a <  h ))       cout << 5;     else       cout << 8;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[1 << 18];   long long sum[1 << 18];   int p[1 << 18];   int n, T, m;   double c;   void Calc() {     double mean = 0.0;     for (int i = 0, j = 0; i < n && j < m; i++) {       mean = (mean + (a[i] + .0) / T) / c;       if (i == p[j] - 1) {         double real = (.0 + sum[i + 1] - sum[i - T + 1]) / T;         printf( %.9lf  , real);         printf( %.9lf  , mean);         printf( %.9lf n , fabs(real - mean) / real);         j++;       }     }   }   int main() {     scanf( %d%d%lf , &n, &T, &c);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       sum[i + 1] = sum[i] + a[i];     }     scanf( %d , &m);     for (int i = 0; i < m; i++) scanf( %d , &p[i]);     Calc();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, x, res = -1000000000;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x;       if (floor(sqrt(x)) != ceil(sqrt(x))) {         res = max(res, x);       }     }     cout << res <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, pay = 0, kd;     int U = 0, R = 0;     char c;     cin >> n;     cin >> c;     if (c ==  U ) {       kd = 1;       U++;     } else {       kd = 0;       R++;     }     for (int i = 1; i < n; i++) {       cin >> c;       if (c ==  U )         U++;       else if (c ==  R )         R++;       if (kd == 0 && R < U) {         pay++;         kd = 1;       } else if (kd && R > U) {         pay++;         kd = 0;       }     }     cout << pay;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 20500;   int A[N];   int n;   int B[N];   int get(int k) {     if (n / k < 3) return -1e9;     for (int i = 0; i < k; i++) B[i] = 0;     for (int i = 0; i < n; i++) B[i % k] += A[i];     int ans = -1e9;     for (int i = 0; i < k; i++) ans = max(ans, B[i]);     return ans;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> A[i];     int ans = -1e9;     for (int k = 1; k * k <= n; k++)       if (n % k == 0) ans = max(ans, max(get(k), get(n / k)));     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int a = 0, b = 0, c = 0;       string line;       cin >> line;       for (char d : line) {         if (d ==  B ) b++;         if (d ==  A ) a++;         if (d ==  C ) c++;       }       if (b == a + c) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long int MOD = 1000000007;   long long int power(long long int x, long long int y, long long int p) {     long long int res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int t;     t = 1;     while (t--) {       long long int n, m, k;       cin >> n >> m >> k;       vector<long long int> xs, ys;       for (long long int i = 0; i < n * m; i++) {         ys.push_back(i / n + 1);         if ((i / n) % 2 == 0) {           xs.push_back(i % n + 1);         } else {           xs.push_back(n - i % n);         }       }       for (long long int i = 0; i < k - 1; i++) {         cout << 2 <<     << xs[2 * i] <<     << ys[2 * i] <<     << xs[2 * i + 1]              <<     << ys[2 * i + 1] << endl;       }       cout << n * m - 2 * (k - 1) <<    ;       for (long long int i = 2 * (k - 1); i < n * m; i++) {         cout << xs[i] <<     << ys[i] <<    ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long solve(int N, const vector<int>& A) {     long long res = 0;     vector<int> freq(32);     for (int x : A) {       int hsb = (sizeof(int) * 8 - 1 - __builtin_clz((x)));       res += freq[hsb];       ++freq[hsb];     }     return res;   }   int main(int argc, char* argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int TC;     cin >> TC;     for (int tc = (1), _b = (TC); tc <= _b; ++tc) {       int N;       cin >> N;       vector<int> A(N);       for (int i = 0, _n = (N); i < _n; ++i) cin >> A[i];       long long res = solve(N, A);       cout << res <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   void read(vector<T>& a) {     for (auto& e : a) {       cin >> e;     }   }   template <class T>   void print(vector<T>& a) {     for (auto& e : a) {       cout << e <<    ;     }     cout <<   n ;   }   void solve() {     long long n, h;     cin >> n >> h;     vector<long long> a(n);     read(a);     if (n == 1) {       cout << h <<   n ;       return;     }     vector<long long> b;     for (long long i = 1; i < n; ++i) {       b.push_back(a[i] - a[i - 1]);     }     long long l = 0, r = 1e18;     while (l + 1 != r) {       long long m = (l + r) / 2;       long long res = m;       for (long long i = 0; i < n - 1; ++i) {         res += min(m, b[i]);       }       if (res >= h) {         r = m;       } else {         l = m;       }     }     cout << r <<   n ;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     x = 0;     char c = getchar();     int f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }     x *= f;   }   const int N = 505, M = 25005000, inf = 0x3f3f3f3f;   int n, m, num, dfn[N][N], dis[N];   bool mtr[2505][2505], v[2505];   char s[N][N];   int dx[] = {0, 0, 1, -1};   int dy[] = {1, -1, 0, 0};   int q[2505], head[2505], nxt[M], to[M], edgenum;   void addedge(int u, int v) {     to[++edgenum] = v;     nxt[edgenum] = head[u];     head[u] = edgenum;   }   inline void dfs(int x, int y) {     dfn[x][y] = num;     for (register int i = 0; i <= 3; i++) {       int nx = x + dx[i], ny = y + dy[i];       if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {         if (s[nx][ny] == s[x][y] && !dfn[nx][ny]) dfs(nx, ny);         if (s[nx][ny] != s[x][y] && dfn[nx][ny])           mtr[num][dfn[nx][ny]] = mtr[dfn[nx][ny]][num] = 1;       }     }   }   inline int getdis(int s) {     memset(dis, inf, sizeof(dis));     int res = 0, f = 0, r = 1;     q[f] = s;     dis[s] = 0;     while (f != r) {       int u = q[f++];       res = max(res, dis[u]);       for (int i = head[u]; i != 0; i = nxt[i])         if (dis[to[i]] > dis[u] + 1) {           dis[to[i]] = dis[u] + 1;           q[r++] = to[i];         }     }     return res;   }   int main() {     read(n);     read(m);     for (register int i = 1; i <= n; i++) scanf( %s , s[i] + 1);     for (register int i = 1; i <= n; i++)       for (register int j = 1; j <= m; j++)         if (!dfn[i][j]) {           ++num;           v[num] = s[i][j] ==  B ;           dfs(i, j);         }     for (register int i = 1; i <= num; i++)       for (register int j = 1; j <= num; j++)         if (mtr[i][j]) addedge(i, j);     int ans = 1e9;     for (register int i = 1; i <= num; i++) {       int d = getdis(i);       ans = min(ans, d + (((d + v[i]) & 1) ? 1 : 0));     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);   int main()   {       fast;       int t;       cin >> t;       while (t--)       {           int n;           cin >> n;           vector<int> a(n + 1);           a[0] = 0;           for (int i = 1; i <= n; i++)               a[i] = i;           if (n % 2 == 0)           {               for (int i = 1; i < n; i += 2)                   swap(a[i], a[i + 1]);           }           else           {               for (int i = 1; i < n - 2; i+= 2)                   swap(a[i], a[i + 1]);               int temp = a[n];               a[n] = a[n - 1];               a[n - 1] = a[n - 2];               a[n - 2] = temp;           }           for (int i = 1; i <= n; i++)               cout << a[i] <<    ;           cout << endl;       }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 3, Mod = 1e9 + 7;   const int maxN = 1e5 + 3;   void solve() {     long long n;     cin >> n;     long long ans = 6;     long long bin = 4;     for (int i = 1; i < n; i++) {       bin *= bin;       bin %= Mod;       ans *= bin;       ans %= Mod;     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int t = 1;     for (int i = 0; i < t; i++) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int x[102], pile[102];   int MN(int a, int b) { return (a < b) ? a : b; }   int main() {     int n, i, sz, j, flag;     while (scanf( %d , &n) != EOF) {       for (i = 1; i <= n; i++) {         scanf( %d , &x[i]);       }       sort(x + 1, x + n + 1);       sz = 1;       pile[sz] = 1;       for (i = 2; i <= n; i++) {         flag = 0;         for (j = 1; j <= sz; j++) {           if (pile[j] <= x[i]) {             flag = 1;             pile[j]++;             break;           }         }         if (flag == 0) pile[++sz] = 1;       }       printf( %d n , sz);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   int n;   int inDegree[MAXN];   long long A[MAXN], _A[MAXN], B[MAXN];   vector<int> adj[MAXN];   void build() {     for (int i = 0; i < n; i++) {       if (B[i] == -1) continue;       adj[i].push_back(B[i]);       inDegree[B[i]]++;     }   }   vector<int> solve() {     vector<int> pos, neg;     queue<int> q;     for (int i = 0; i < n; i++) {       if (inDegree[i] == 0) {         q.push(i);       }     }     while (!q.empty()) {       int i = q.front();       q.pop();       if (A[i] > 0) {         pos.push_back(i);         if (B[i] != -1) A[B[i]] += A[i];       } else {         neg.push_back(i);       }       for (auto v : adj[i]) {         inDegree[v]--;         if (inDegree[v] == 0) q.push(v);       }     }     reverse(neg.begin(), neg.end());     pos.insert(pos.end(), neg.begin(), neg.end());     return pos;   }   int main() {     ;     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> A[i];       _A[i] = A[i];     }     for (int i = 0; i < n; i++) {       cin >> B[i];       if (B[i] != -1) B[i]--;     }     build();     long long ans = 0;     vector<int> v = solve();     for (auto i : v) {       ans += _A[i];       if (B[i] != -1) _A[B[i]] += _A[i];     }     cout << ans <<   n ;     for (auto i : v) cout << i + 1 <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, t;   int ask(int r) {     cout <<  ?   << 1 <<     << r << endl;     int ans;     cin >> ans;     return ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     srand(time(0));     cin >> n >> t;     vector<int> a(1, t);     int inv = 0, rep = 0;     for (int i = n - 1; i >= 1; i--) {       int t2 = ask(i);       inv ^= 1;       int cur = i - (t - t2 + i) / 2;       if (inv) cur = i - cur;       if (t2 == n - t && rep < 25) {         i++;         rep++;       } else {         a.push_back(cur);         rep = 0;       }       t = t2;     }     reverse(a.begin(), a.end());     cout <<  !   << a[0];     for (int i = 1; i < (int)a.size(); i++) cout << a[i] - a[i - 1];     cout << endl;   }
//a.9   #include<bits/stdc++.h>   #define ll long long   #define all(x) x.begin(),x.end()   #define CASE(t) cout<< Case # <<(t)<< :  ;   #define endll endl   #define endl   n    #define INF 1e18   #define deb(x) cout <<  [  << (#x) <<  =  << x <<  ]  <<   n    #define deb2(x,y) cout <<  [  << (#x) <<  =  << x <<  ] [  << (#y) <<  =  << y <<  ]  <<   n    #define deb3(x,y,z) cout <<  [  << (#x) <<  =  << x <<  ] [  << (#y) <<  =  << y <<  ] [  << (#z) <<  =  << z <<  ]  <<   n    #define popcnt __builtin_popcountll   #define precise(x) fixed << setprecision(x)   #define ios ios_base::sync_with_stdio(false); cin.tie(NULL)   using namespace std;   const ll mod=1000000007;   const ll maxN=50005;      ll ceill(ll a,ll b){return a/b+bool(a%b);}   ll gcd(ll a,ll b){if(b==0)return a;return gcd(b, a % b);}   ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}   ll power(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x)%mod;x=(x*x)%mod;y>>=1;}return res;}   void bin(unsigned n,string &str){str=  ;if(n>1)bin(n>>1,str);str+=to_string(n&1);}   bool isPrime(ll n)   {       if(n<=1)return false;else if(n<=3)return true;else if(n%2==0 || n%3==0)return false;       for(ll i=5;i*i<=n;i=i+6){if(n%i==0 || n%(i+2)==0)return false;}return true;   }      vector<ll>primes;   void SieveOfEratosthenes()   {       bool prime[maxN];primes.clear();       memset(prime, true, sizeof(prime));       for(ll i=2;i*i<maxN;i++)if(prime[i]==true)for(ll j=i*i;j<maxN;j+=i)prime[j]=false;       for(ll i=2;i<maxN;i++)if(prime[i])primes.push_back(i);   }   //vector<ll>fact(maxN);   //void factpre(){fact[0]=1;for(ll i=1;i<maxN;i++)fact[i]=(fact[i-1]*1LL*i)%mod;}   //ll mul(ll a,ll b){return (a*1LL*b)%mod;}   //ll nCr(ll n,ll k){if(k>n)return 0;return mul(fact[n],power(mul(fact[k],fact[n-k]),mod-2));}      /*ll spf[maxN];   void sieve()   {       spf[1]=1;for(ll i=2;i<maxN;i++)spf[i]=i;for(ll i=4;i<maxN;i+=2)spf[i]=2;       for(ll i=3;i*i<maxN;i++){if(spf[i]==i)for(ll j=i*i;j<maxN;j+=i)if(spf[j]==j)spf[j]=i;}   }   vector<ll>getfacts(ll x){vector<ll>vec;while(x!=1)vec.push_back(spf[x]),x=x/spf[x];return vec;}*/      /*ll fen[maxN];   void update(ll i,ll val,ll n){i++;for(;i<=n;i+=i&-i)fen[i]+=val;}   ll get(ll i){i++;ll res=0;for(;i>0;i-=i&-i)res+=fen[i];return res;}*/      void solve()   {       ll a,b;cin>>a>>b;       if(b==1){cout<< NO <<endl;return;}       cout<< YES <<endl;       cout<<a<<   <<a*b<<   <<a*(b+1)<<endl;   }   signed main()   {       ios;       ll tc=1; cin>>tc;       for(ll t=1;t<=tc;t++)       {           //CASE(t);           solve();       }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     int b[n];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     int c = 0, r = 0;     for (int i = 0; i < n; i++) {       if (a[i] == 0 and b[i] == 1) c++;       if (a[i] == 1 and b[i] == 0) r++;     }     if (r > c) {       cout <<  1  << endl;     } else if (r == 0)       cout <<  -1  << endl;     else {       int val = ceil(((float)(c + 1) / (float)r));       cout << val << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[90];   void error() {     cout << -1 << endl;     exit(0);   }   int main() {     int n, m, l = 1, r;     scanf( %d%d n , &n, &m);     r = n;     while (m--) {       int v;       scanf( To the %s of %d n , s, &v);       if (!strcmp(s,  left )) {         if (l >= v) error();         if (r >= v) r = v - 1;       } else {         if (r <= v) error();         if (l <= v) l = v + 1;       }     }     cout << r - l + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 10000005;   int sum[MAXN];   bool ok[MAXN];   int n;   char a[200005];   bool grp[200005];   void submit(int s) {     vector<string> ret;     for (int i = 0; i < n; i++) {       if (grp[i]) {         string str;         str.push_back(a[i]);         str.push_back(a[i + 1]);         ret.push_back(str);         i++;         continue;       } else {         string str = {a[i]};         ret.push_back(str);       }     }     for (int i = 0; i < ret.size(); i++) {       printf( %s , ret[i].c_str());       if (i + 1 < ret.size()) printf( + );     }     puts(  );     if (s >= 10) {       vector<int> a;       int bs = sum[s];       while (s) {         a.push_back(s % 10);         s /= 10;       }       reverse(a.begin(), a.end());       for (int i = 0; i < a.size(); i++) {         printf( %d , a[i]);         if (i + 1 < a.size()) printf( + );       }       puts(  );       if (bs >= 10)         printf( %d+%d , bs / 10, bs % 10);       else         printf( %d , bs);     } else {       printf( %d n%d n , s, s);     }     exit(0);   }   int main() {     for (int i = 1; i < MAXN; i++) {       sum[i] = sum[i / 10] + (i % 10);     }     for (int i = 0; i < MAXN; i++) {       if (sum[sum[i]] <= 9) ok[i] = 1;     }     scanf( %d %s , &n, a);     int sum = 0;     for (int i = 0; i < n; i++) sum += a[i] -  0 ;     if (ok[sum]) submit(sum);     for (int i = 0; i + 1 < n; i += 2) {       sum -= a[i] -  0 ;       sum -= a[i + 1] -  0 ;       sum += (a[i] -  0 ) * 10 + (a[i + 1] -  0 );       grp[i] = 1;       if (ok[sum]) submit(sum);     }     for (int i = 0; i + 1 < n; i += 2) {       sum += a[i] -  0 ;       sum += a[i + 1] -  0 ;       sum -= (a[i] -  0 ) * 10 + (a[i + 1] -  0 );       grp[i] = 0;       if (ok[sum]) submit(sum);     }     for (int i = 1; i + 1 < n; i += 2) {       sum -= a[i] -  0 ;       sum -= a[i + 1] -  0 ;       sum += (a[i] -  0 ) * 10 + (a[i + 1] -  0 );       grp[i] = 1;       if (ok[sum]) submit(sum);     }     for (int i = 1; i + 1 < n; i += 2) {       sum += a[i] -  0 ;       sum += a[i + 1] -  0 ;       sum -= (a[i] -  0 ) * 10 + (a[i + 1] -  0 );       grp[i] = 0;       if (ok[sum]) submit(sum);     }     assert(0);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200000;   int H[N];   vector<int> g[N], b[N];   int ans[N];   int n, q;   map<int, set<pair<int, int> > > m;   void dfs(int v, int h = 0, int p = -1) {     ans[v] = -1;     H[v] = h;     for (auto i : b[v]) {       if (!m[i].empty()) {         auto t = *m[i].begin();         if (ans[v] == -1 || H[ans[v]] < -t.first) ans[v] = t.second;       }       m[i].insert({-h, v});     }     for (auto u : g[v]) {       if (p != u) {         dfs(u, h + 1, v);       }     }     for (auto i : b[v]) {       m[i].erase({-h, v});     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> q;     for (int j = 1; j <= n; j++) {       int w;       cin >> w;       for (int i = 2; i * i <= w; i++) {         if (w % i == 0) {           b[j].push_back(i);           while (w % i == 0) w /= i;         }       }       if (w > 1) b[j].push_back(w);     }     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1);     while (q--) {       int t;       cin >> t;       if (t == 2) {         int v, w;         cin >> v >> w;         b[v].clear();         for (int i = 2; i * i <= w; i++) {           if (w % i == 0) {             b[v].push_back(i);             while (w % i == 0) w /= i;           }         }         if (w > 1) b[v].push_back(w);         dfs(1);       } else {         int v;         cin >> v;         cout << ans[v] << endl;       }     }   }