malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n;   string s;   int main() {     cin >> n;     cin >> s;     for (int i = 0; i < n; ++i) {       int c1 = 0, c2 = 0;       for (int j = i - 1; j >= 0; --j) {         if (s[j] ==  L ) {           break;         }         if (s[j] ==  R ) {           ++c1;         }       }       for (int j = i; j < n - 1; ++j) {         if (s[j] ==  R ) {           break;         }         if (s[j] ==  L ) {           ++c2;         }       }       cout << max(c1, c2) + 1 <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<long long> x(n), y(n);     for (int i = 0; i < n; i++) {       cin >> x[i] >> y[i];       y[i] -= x[i];     }     vector<vector<int>> g(n);     for (int i = 0; i < m; i++) {       int foo, bar;       cin >> foo >> bar;       foo--;       bar--;       g[foo].push_back(bar);       g[bar].push_back(foo);     }     long long sum_x = accumulate(x.begin(), x.end(), 0LL);     vector<long long> sorted_y = y;     sort(sorted_y.begin(), sorted_y.end());     vector<long long> pref(n + 1);     for (int i = 0; i < n; i++) {       pref[i + 1] = pref[i] + sorted_y[i];     }     for (int i = 0; i < n; i++) {       long long ans = x[i] * (n - 1) + (sum_x - x[i]);       int pos = (int)(lower_bound(sorted_y.begin(), sorted_y.end(), y[i]) -                       sorted_y.begin());       ans += pref[pos] + y[i] * (n - pos - 1);       for (int j : g[i]) {         long long cur = x[i] + x[j] + min(y[i], y[j]);         ans -= cur;       }       if (i > 0) {         cout <<    ;       }       cout << ans;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int boxes;     cin >> boxes;     int arr[boxes];     for (int i = 0; i < boxes; i++) cin >> arr[i];     int shots;     cin >> shots;     int x, y;     for (int i = 0; i < shots; i++) {       cin >> x >> y;       if (x == 1) {         arr[x] += (arr[x - 1] - y);         arr[0] = 0;       } else if (x == boxes) {         arr[boxes - 1] = 0;         arr[x - 2] += (y - 1);       } else {         arr[x] += (arr[x - 1] - y);         arr[x - 2] += (y - 1);         arr[x - 1] = 0;       }     }     for (int i = 0; i < boxes; i++) cout << arr[i] << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int a[2 * n];     for (int i = 0; i < 2 * n; i++) cin >> a[i];     sort(a, a + (2 * n));     long long int s = 2e19;     ;     for (int i = 1; i < n; i++) {       s = min(s, (a[n * 2 - 1] - a[0]) * (a[n + i - 1] - a[i]));     }     s = min(s, (a[n - 1] - a[0]) * (a[n * 2 - 1] - a[n]));     cout << s;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   struct node {     char type;     vector<node*> children;     ll dp = 0;     ll r = 0;   };   node* get(string& s, int& idx) {     node* v = new node();     if (s[idx] ==  * ) {       v->type =  * ;       idx++;       return v;     } else if (s[idx] ==  ( ) {       idx++;       while (true) {         v->children.push_back(get(s, idx));         if (s[idx] ==  P  || s[idx] ==  S ) {           v->type = s[idx];           idx++;         } else if (s[idx] ==  ) ) {           idx++;           break;         } else           assert(false);       }     } else       assert(false);     return v;   }   const ll Z = 2e18;   ll sq(ll r) {     if (r > Z / r) return Z;     return min(r * r, Z);   }   void make_dp(node* v) {     if (v->type ==  * ) {       v->dp = 1;     } else {       for (node* w : v->children) {         make_dp(w);       }       if (v->type ==  S ) {         v->dp = Z;         for (node* w : v->children) v->dp = min(v->dp, w->dp);       } else if (v->type ==  P ) {         v->dp = 0;         for (node* w : v->children) v->dp += w->dp;       } else         assert(false);     }   }   ll res = 0;   void make_resistances(node* v, bool good) {     if (v->type ==  * ) {       cout <<     << (good ? res : 0);     } else if (v->type ==  P ) {       for (node* w : v->children) {         make_resistances(w, good);       }     } else if (v->type ==  S ) {       for (node* w : v->children) {         if (w->dp == v->dp) {           make_resistances(w, good);           good = false;         } else {           make_resistances(w, false);         }       }     } else       assert(false);   }   void solve() {     string s;     getline(cin, s);     auto g = s.find(   );     ll R = stoll(s.substr(0, g));     s = s.substr(g + 1);     s.resize(remove_if(s.begin(), s.end(), [&](char a) { return a ==    ; }) -              s.begin());     int idx = 0;     node* z = get(s, idx);     make_dp(z);     res = R * z->dp;     cout <<  REVOLTING ;     make_resistances(z, true);     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int T;     string s;     getline(cin, s);     T = stoi(s);     while (T--) solve();   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 10;   int n, b[maxn];   pair<int, int> a[maxn];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i].first);       a[i].second = i;     }     sort(a + 1, a + 1 + n);     reverse(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) {       b[i] = a[i].second * 2 - 1;       if (i > 1) printf( %d %d n , b[i], b[i - 1]);     }     int res, cur = n, des;     for (int i = 1; i <= n; i++) {       res = a[i].second * 2;       des = i + a[i].first - 1;       if (des >= cur) {         b[++cur] = res;         printf( %d %d n , res, b[cur - 1]);       } else         printf( %d %d n , res, b[des]);     }   }
#include <bits/stdc++.h>   int main() {     int n, x, y, z, sum1 = 0, sum2 = 0, sum3 = 0;     scanf( %d , &n);     while (n--) {       scanf( %d %d %d , &x, &y, &z);       sum1 = sum1 + x;       sum2 = sum2 + y;       sum3 = sum3 + z;     }     if (sum1 == 0 && sum2 == 0 && sum3 == 0)       printf( YES );     else       printf( NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 22;   long long a[N];   long long dp[N][N][N];   long long DP(long long pos, long long sum, long long limit) {     if (!pos) return 1;     if (!limit && dp[pos][sum][limit] != -1) return dp[pos][sum][limit];     long long high = limit ? a[pos] : 9;     long long ans = 0;     for (long long i = 0; i <= high; i++) {       if (sum == 3 && i != 0) continue;       ans += DP(pos - 1, sum + (i != 0), limit && (i == high));     }     if (!limit) dp[pos][sum][limit] = ans;     return ans;   }   long long slove(long long n) {     long long len = 0;     while (n) {       a[++len] = n % 10;       n = n / 10;     }     return DP(len, 0, 1);   }   signed main() {     memset(dp, -1, sizeof(dp));     long long T;     cin >> T;     while (T--) {       long long L, R;       cin >> L >> R;       cout << slove(R) - slove(L - 1) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[101000];   long long pre[101000];   map<long long, long long> Hash;   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];     Hash[0] = 1;     long long count = 0;     for (int i = 1; i <= n; i++) {       if (abs(k) > 1) {         for (long long p = 1; (p > 0 ? p : -p) <= 1e15; p *= k) {           if (Hash[pre[i] - p]) count += Hash[pre[i] - p];         }       } else {         if (k == 1 || k == -1) {           if (Hash[pre[i] - 1]) count += Hash[pre[i] - 1];           if (k == -1) {             if (Hash[pre[i] + 1]) count += Hash[pre[i] + 1];           }         } else {           if (Hash[pre[i]]) count += Hash[pre[i]];         }       }       Hash[pre[i]] += 1;     }     cout << count << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mat[4005][4005];   int acum[4005][4005];   int dp[4005][805];   int step(int n, int k, int min_i, int max_i) {     dp[n][k] = dp[min_i - 1][k - 1] + (acum[n][n] + acum[min_i - 1][min_i - 1] -                                        acum[n][min_i - 1] - acum[min_i - 1][n]) /                                           2;     for (int i = min_i + 1; i <= max_i; i++) {       if (dp[i - 1][k - 1] + (acum[n][n] + acum[i - 1][i - 1] - acum[n][i - 1] -                               acum[i - 1][n]) /                                  2 <           dp[n][k]) {         dp[n][k] = dp[i - 1][k - 1] + (acum[n][n] + acum[i - 1][i - 1] -                                        acum[n][i - 1] - acum[i - 1][n]) /                                           2;         min_i = i;       }     }     return min_i;   }   void conquer(int min_n, int max_n, int k, int min_i, int max_i) {     if (min_n > max_n) return;     max_i = min(max_n, max_i);     if (min_n == max_n) {       step(min_n, k, min_i, max_i);       return;     }     if (min_i == max_i) {       for (int i = min_n; i <= max_n; i++) step(i, k, min_i, max_i);       return;     }     int mid = (min_n + max_n) / 2;     int i = step(mid, k, min_i, min(max_i, mid));     conquer(min_n, mid - 1, k, min_i, i);     conquer(mid + 1, max_n, k, i, max_i);   }   char buff[100005];   int main() {     int n, k;     scanf( %d %d n , &n, &k);     for (int i = 1; i <= n; i++) {       int acum2 = 0;       gets(buff);       for (int j = 1; j <= n; j++) {         acum2 += buff[j * 2 - 2] -  0 ;         acum[i][j] = acum[i - 1][j] + acum2;       }     }     for (int i = 1; i <= n; i++) {       dp[i][1] = acum[i][i] / 2;     }     for (int j = 2; j <= k; j++) {       conquer(1, n, j, j - 1, n);     }     printf( %d n , dp[n][k]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<int> si;   int n, k;   bool solve(set<int> &cands, vector<int> &chosen) {     if (cands.empty()) {       if (chosen.empty()) {         return false;       }       for (int i = 0; i < k; ++i) {         int cnt = 0;         for (const int num : chosen) {           cnt += (((num >> i) & 0x1) ? -1 : 1);         }         if (cnt < 0) {           return false;         }       }       return true;     }     int val = *cands.begin();     bool ok = false;     cands.erase(val);     ok |= solve(cands, chosen);     chosen.push_back(val);     ok |= solve(cands, chosen);     chosen.pop_back();     cands.insert(val);     return ok;   }   int main(int argc, char **argv) {     std::ios::sync_with_stdio(false);     cin >> n >> k;     for (int i = 0; i < n; ++i) {       int val = 0;       for (int j = 0; j < k; ++j) {         int t;         cin >> t;         val |= (t << j);       }       si.insert(val);     }     vector<int> chosen;     cout << (solve(si, chosen) ?  YES n  :  NO n );   }
#include <bits/stdc++.h>   using namespace std;   int a[5005], cnt, tmp, nxt, V[5005], j, n, i;   long long dp[2][5005], ans;   struct LS {     int pos, val;   } ls[5005];   inline bool cmp(LS aa, LS bb) { return aa.val < bb.val; }   int main() {     cin >> n;     for (i = 1; i <= n; ++i) cin >> a[i], ls[i].val = a[i], ls[i].pos = i;     sort(ls + 1, ls + n + 1, cmp);     ls[0].val = -1000000001;     for (i = 1; i <= n; ++i) {       if (ls[i].val != ls[i - 1].val) ++cnt, V[cnt] = ls[i].val;       a[ls[i].pos] = cnt;     }     for (i = 1; i <= a[1]; ++i) dp[0][i] = V[a[1]] - V[i];     for (i = a[1] + 1; i <= cnt; ++i) dp[0][i] = 0;     for (i = 2; i <= n; tmp ^= 1, ++i) {       nxt = tmp ^ 1;       dp[nxt][0] = 1e16;       for (j = 1; j <= cnt; ++j)         dp[nxt][j] = min(dp[nxt][j - 1], dp[tmp][j] + abs(V[a[i]] - V[j]));     }     for (ans = 1e16, j = 1; j <= cnt; ++j) ans = min(ans, dp[tmp][j]);     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, d;     cin >> n >> d;     int t;     cin >> t;     while (t--) {       long long int x, y;       cin >> x >> y;       long long int eq1 = y * (n - d) - ((n - d) * (x - d));       long long int eq2 = (y - (n - d)) + (x - (n));       long long int eq3 = ((y - n) - (x - (n - d)));       long long int eq4 = (y - d) + (x);       if (eq1 >= 0 && eq2 <= 0 && eq3 <= 0 && eq4 >= 0)         cout <<  YES n ;       else         cout <<  NO n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     bool flag = 0, flag2 = 0;     long long n, m, c = 0;     cin >> n >> m;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         if (i % 2 != 0 && c % 2 == 0 && j != m - 1) {           cout <<  . ;           flag = 1;         } else if (i % 2 != 0 && c % 2 != 0 && j != 0) {           cout <<  . ;           flag2 = 1;         } else           cout <<  # ;       }       cout << endl;       if (flag || flag2) c++;       flag = 0, flag2 = 0;     }     return 0;   }
#include<bits/stdc++.h>   using namespace std;   typedef long long int ll;   ll mod=1e9+7;   int main()   {    int t;    cin>>t;    while(t--)    {     ll n,k;     cin>>n>>k;     ll ans=1;     for(ll i=0;i<k;i++)     {      ans=(ans*n)%mod;     }     cout<<ans<<endl;         }   }
#include <bits/stdc++.h>   using namespace std;   int _I() {     int x;     scanf( %d , &x);     return x;   }   const int lmt = 10000000;   bool prime[lmt + 5];   int rng = 1, v[lmt + 5];   void sv() {     v[0] = 2;     for (int i = 4; i <= lmt; i += 2) prime[i] = 1;     for (int i = 3; i <= lmt; i += 2) {       if (!prime[i]) {         v[rng++] = i;         for (int j = i * 3; j <= lmt; j += i << 1) prime[j] = 1;       }     }     prime[1] = 1;   }   int rev(int n) {     int num = 0;     while (n) {       num *= 10;       num += (n % 10);       n /= 10;     }     return num;   }   void solve() {     int n = _I();     int ans[100001];     for (int i = 13, j = 1; j <= 11184; i++) {       if (i != rev(i) && !prime[i] && !prime[rev(i)]) ans[j++] = i;     }     cout << ans[n] <<   n ;   }   int main() {     sv();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double radian = 180 / acos(-1);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long x, mini = LLONG_MAX, p;     cin >> x;     x %= 360;     if (x < 0) x = 360 + x;     for (int i = 0; i < 4; i++) {       if (min(x, 360 - x) < mini) {         mini = min(x, 360 - x);         p = i;       }       x = (x + 270) % 360;     }     cout << p;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T sqr(const T &a) {     return a * a;   }   template <typename T>   inline int nread(vector<T> &a) {     int n;     cin >> n;     a.clear();     a.resize(n);     for (int i = 0; i < n; i++) cin >> a[i];     return n;   }   template <typename T>   inline void nwread(int n, vector<T> &a) {     a.clear();     a.resize(n);     for (int i = 0; i < n; i++) cin >> a[i];   }   int main() {     int n, k;     cin >> n >> k;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (a[j] != 0) continue;         cout << j + 1 <<    ;         a[j] = -1;         for (int l = 0; l <= j - k; l++) a[l]--;         break;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   long long t;   long long n;   long long a[N];   long long b[N];   map<long long, long long> ma;   int main() {     scanf( %lld , &t);     while (t--) {       ma.clear();       memset(a, 0, sizeof(a));       memset(b, 0, sizeof(b));       scanf( %lld , &n);       for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]);       for (long long i = 1; i <= n; i++) scanf( %lld , &b[i]);       if (a[1] != b[1]) {         printf( NO n );         continue;       }       ma[a[1]] = 1;       bool f = false;       for (long long i = 2; i <= n; i++) {         if (a[i] == b[i]) {           ma[a[i]] = 1;           continue;         }         if (f) break;         long long cha = b[i] - a[i];         if (cha < 0) {           if (ma[-1] > 0) {           } else {             f = true;             break;           }         }         if (cha > 0) {           if (ma[1] > 0) {           } else {             f = true;             break;           }         }         if (f) break;         ma[a[i]] = 1;       }       if (f) {         printf( NO n );       } else {         printf( YES n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b, c, d;     cin >> a >> b >> c >> d;     int c1 = 0, c2 = 0, c3 = 0, c4 = 0;     if ((a.size() - 2) >= (b.size() - 2) * 2 &&         (a.size() - 2) >= (c.size() - 2) * 2 &&         (a.size() - 2) >= (d.size() - 2) * 2) {       c1 = 1;     } else if ((a.size() - 2) * 2 <= (b.size() - 2) &&                (a.size() - 2) * 2 <= (c.size() - 2) &&                (a.size() - 2) * 2 <= (d.size() - 2)) {       c1 = 1;     }     if ((b.size() - 2) >= (a.size() - 2) * 2 &&         (b.size() - 2) >= (c.size() - 2) * 2 &&         (b.size() - 2) >= (d.size() - 2) * 2) {       c2 = 1;     } else if ((b.size() - 2) * 2 <= (a.size() - 2) &&                (b.size() - 2) * 2 <= (c.size() - 2) &&                (b.size() - 2) * 2 <= (d.size() - 2)) {       c2 = 1;     }     if ((c.size() - 2) >= (a.size() - 2) * 2 &&         (c.size() - 2) >= (b.size() - 2) * 2 &&         (c.size() - 2) >= (d.size() - 2) * 2) {       c3 = 1;     } else if ((c.size() - 2) * 2 <= (a.size() - 2) &&                (c.size() - 2) * 2 <= (b.size() - 2) &&                (c.size() - 2) * 2 <= (d.size() - 2)) {       c3 = 1;     }     if ((d.size() - 2) >= (a.size() - 2) * 2 &&         (d.size() - 2) >= (b.size() - 2) * 2 &&         (d.size() - 2) >= (c.size() - 2) * 2) {       c4 = 1;     } else if ((d.size() - 2) * 2 <= (a.size() - 2) &&                (d.size() - 2) * 2 <= (b.size() - 2) &&                (d.size() - 2) * 2 <= (c.size() - 2)) {       c4 = 1;     }     if (c1 + c2 + c3 + c4 > 1 || c1 + c2 + c3 + c4 == 0)       cout << c[0] << endl;     else {       if (c1 == 1)         cout << a[0] << endl;       else if (c2 == 1)         cout << b[0] << endl;       else if (c3 == 1)         cout << c[0] << endl;       else if (c4 == 1)         cout << d[0] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   char playLog[1111];   char posLet[3] = { W ,  D ,  L };   int delta[3] = {1, 0, -1};   int val[256];   char dp[1111][2222];   void clearArray() {     for (int i = 0; i <= n + 1; i++) {       for (int j = -k; j <= k; j++) dp[i][j + k] =  X ;     }   }   void printAnswer() {     int currentState = 0;     for (int i = 0; i < n; i++) {       char thisOne = dp[i][currentState + k];       printf( %c , thisOne);       currentState += val[thisOne];     }     printf(  n );   }   int main() {     val[ W ] = 1;     val[ D ] = 0;     val[ L ] = -1;     scanf( %d%d , &n, &k);     scanf( %s , playLog);     clearArray();     dp[n][k + k] =  P ;     for (int i = n - 1; i >= 0; i--) {       for (int j = -(k - 1); j <= (k - 1); j++) {         for (int q = 0; q < 3; q++) {           if (playLog[i] !=  ?  && (playLog[i] != posLet[q])) continue;           int target = j + delta[q];           if (dp[i + 1][target + k] ==  X ) continue;           dp[i][j + k] = posLet[q];           break;         }       }     }     if (dp[0][0 + k] !=  X ) {       printAnswer();       return 0;     }     clearArray();     dp[n][0] =  P ;     for (int i = n - 1; i >= 0; i--) {       for (int j = -(k - 1); j <= (k - 1); j++) {         for (int q = 0; q < 3; q++) {           if (playLog[i] !=  ?  && (playLog[i] != posLet[q])) continue;           int target = j + delta[q];           if (dp[i + 1][target + k] ==  X ) continue;           dp[i][j + k] = posLet[q];           break;         }       }     }     if (dp[0][0 + k] !=  X ) {       printAnswer();       return 0;     }     printf( NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int shape[4][4][2] = {{{1, 0}, {2, 0}, {1, 1}, {1, 2}},                               {{1, 0}, {2, 0}, {2, -1}, {2, 1}},                               {{0, 1}, {0, 2}, {-1, 2}, {1, 2}},                               {{0, 1}, {0, 2}, {1, 1}, {2, 1}}};   int x, y;   struct node {     char g[9][9];     int num;     int sx, sy;     node() {       sx = sy = 0;       memset(g, 0, sizeof(g));       num = 0;     }     void printNode() {       for (int i = 0; i < y; i++) {         for (int j = 0; j < x; j++) printf( %c , g[i][j] ? g[i][j] :  . );         cout <<    << endl;       }     }   };   stack<node> q;   int ans;   node ansNode;   void bfs() {     while (true) {     The_End_Where_I_Began:       if (q.empty()) break;       node now = q.top();       q.pop();       if (now.num > ans) {         ans = now.num;         ansNode = now;       }       now.num++;       char ch =  A  + now.num - 1;       bool flag = true;       for (int i = now.sy; i < y; i++) {         int j = (now.sy == i ? now.sx : 0);         for (; j < x; j++) {           if (!now.g[i][j]) {             flag = true;             for (int k = 3; k >= 0; k--) {               flag = true;               for (int l = 0; l < 4; l++) {                 int tx = j + shape[k][l][1];                 int ty = i + shape[k][l][0];                 if (tx < 0 || tx >= x || ty < 0 || ty >= y || now.g[ty][tx]) {                   flag = false;                   break;                 }               }               if (flag) {                 node tNode = now;                 tNode.g[i][j] = ch;                 for (int l = 0; l < 4; l++) {                   int tx = j + shape[k][l][1];                   int ty = i + shape[k][l][0];                   tNode.g[ty][tx] = ch;                 }                 tNode.sx = j + 1;                 tNode.sy = i;                 q.push(tNode);               }             }             if (flag) goto The_End_Where_I_Began;           }         }       }     }   }   int main() {     ans = -1;     cin >> y >> x;     while (!q.empty()) q.pop();     q.push(node());     bfs();     cout << ans << endl;     ansNode.printNode();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   const ll inf = 1;   const ll mod = 1E9;   int main() {     int n, m;     scanf( %d %d , &n, &m);     int colors = 1;     vector<vector<int> > mset(n, vector<int>());     vector<vector<int> > G(n, vector<int>());     for (int i = 0; i < n; i++) {       int lim;       scanf( %d , &lim);       for (int j = 0; j < lim; j++) {         int x;         scanf( %d , &x);         mset[i].push_back(x - 1);       }       colors = max(colors, lim);     }     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d %d , &u, &v);       G[u - 1].push_back(v - 1);       G[v - 1].push_back(u - 1);     }     set<int> ms;     vector<int> col(m, 0);     vector<bool> vis(n, false);     queue<int> Q;     Q.push(0);     vis[0] = true;     while (!Q.empty()) {       int u = Q.front();       Q.pop();       for (auto j : mset[u]) {         if (col[j] != 0) ms.insert(col[j]);       }       int now = 1;       for (auto j : mset[u]) {         if (col[j] == 0) {           while (ms.find(now) != ms.end()) now++;           col[j] = now;           now++;         }       }       for (auto v : G[u]) {         if (!vis[v]) {           vis[v] = true;           Q.push(v);         }       }       ms.clear();     }     printf( %d n , colors);     for (int i = 0; i < m; i++) {       if (col[i] == 0) col[i] = 1;       printf( %d  , col[i]);     }     printf(  n );     return 0;   }