malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using vvi = vector<vi>;   using vpii = vector<pair<int, int>>;   using vc = vector<char>;   using vvc = vector<vc>;   long long binpow(long long a, long long n) {     long long res = 1;     while (n) {       if (n & 1) res *= a;       a *= a;       n >>= 1;     }     return res;   }   bool prime(long long n) {     for (long long i = 2; i <= sqrt(n); i++) {       if (n % i == 0) return false;     }     return true;   }   int gcd(int a, int b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   long long lcm(int a, int b) { return a / gcd(a, b) * b; }   template <class T1, class T2>   void Cin_vector(vector<T1>& a, T2 n) {     for (auto& val : a) {       std::cin >> val;     }   }   template <class T1, class T2>   void Cout_vector(vector<T1>& a, T2 n) {     for (auto& val : a) {       std::cout << val <<    ;     }   }   template <class T1, class T2, class T3>   void Cin_matrix(vector<vector<T1>>& a, T2 n, T3 m) {     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         std::cin >> a[i][j];       }     }   }   template <class T1, class T2, class T3>   void Cout_matrix(vector<vector<T1>>& a, T2 n, T3 m) {     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         std::cout << a[i][j] <<    ;       }       std::cout <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     vector<string> otvet = { F ,  M ,  S };     vector<string> strs;     map<string, int> counts;     for (int i = 0; i < 3; i++) {       string s;       std::cin >> s;       strs.push_back(s);       counts[s]++;     }     int maxs = 0;     string temp;     for (auto& [strings, cnt] : counts) {       if (cnt > maxs) {         maxs = cnt;         temp = strings;       }     }     if (maxs == 1 || maxs == 3) {       std::cout <<  ? ;     } else {       string c;       for (auto& [strings, cnt] : counts) {         if (strings != temp) {           c = strings;         }       }       if (temp ==  rock  && c ==  paper  || temp ==  paper  && c ==  scissors  ||           temp ==  scissors  && c ==  rock ) {         for (int i = 0; i < 3; i++) {           if (c == strs[i]) {             std::cout << otvet[i];             break;           }         }       } else {         std::cout <<  ? ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int inf = 0x7f7f7f7f;   const int maxn = 1e3 + 5;   int n, u, v, root, cnt[2], S;   vector<int> vec[maxn];   int sz[maxn], msz[maxn];   void dfs1(int x, int par) {     for (int i = 0; i <= (int)vec[x].size() - 1; i++) {       if (vec[x][i] == par) continue;       dfs1(vec[x][i], x);       sz[x] += sz[vec[x][i]];       msz[x] = max(msz[x], sz[vec[x][i]]);     }     sz[x]++;     msz[x] = max(msz[x], n - sz[x]);   }   int par[maxn];   int find(int x) {     if (x == par[x]) return x;     return par[x] = find(par[x]);   }   void dfs2(int x, int par, int f, int used) {     for (int i = 0; i <= (int)vec[x].size() - 1; i++) {       if (par == vec[x][i]) continue;       int now = ++cnt[f] - used;       dfs2(vec[x][i], x, f, now + used);       if (f) now *= (S + 1);       printf( %d %d %d n , x, vec[x][i], now);     }   }   int main() {     cin >> n;     if (n == 1) return 0;     for (int i = 1; i <= n - 1; i++) {       scanf( %d%d , &u, &v);       vec[u].push_back(v);       vec[v].push_back(u);     }     dfs1(1, -1);     root = 1;     for (int i = 1; i <= n; i++) {       if (msz[i] < msz[root]) root = i;     }     memset(sz, 0, sizeof(sz));     dfs1(root, -1);     priority_queue<pair<int, int>, vector<pair<int, int> >,                    greater<pair<int, int> > >         que;     for (int i = 0; i <= (int)vec[root].size() - 1; i++) {       que.push(pair<int, int>(sz[vec[root][i]], vec[root][i]));     }     for (int i = 1; i <= n; i++) par[i] = i;     while (que.size() >= 3) {       pair<int, int> p1 = que.top();       que.pop();       pair<int, int> p2 = que.top();       que.pop();       par[p2.second] = find(p1.second);       que.push(pair<int, int>(p1.first + p2.first, par[p1.second]));     }     for (int i = 1; i <= n; i++) find(i);     int pp = par[vec[root][0]];     for (int i = 0; i <= (int)vec[root].size() - 1; i++) {       if (par[vec[root][i]] == pp) S += sz[vec[root][i]];     }     for (int i = 0; i <= (int)vec[root].size() - 1; i++) {       if (par[vec[root][i]] == pp) {         printf( %d %d %d n , root, vec[root][i], ++cnt[0]);         dfs2(vec[root][i], root, 0, cnt[0]);       } else {         printf( %d %d %d n , root, vec[root][i], ++cnt[1] * (S + 1));         dfs2(vec[root][i], root, 1, cnt[1]);       }     }   }
#include <bits/stdc++.h>      using namespace std;      #define _ ios_base::sync_with_stdio(0);cin.tie(0);   #define endl   n       typedef long long ll;      const int INF = 0x3f3f3f3f;   const ll LINF = 0x3f3f3f3f3f3f3f3fll;      int main(){ _       int n; cin >> n;    vector<int> a(n);    for(int& i : a) cin >> i;        auto oa = a;    sort(oa.begin(), oa.end());    auto it = unique(oa.begin(), oa.end());    oa.resize(distance(oa.begin(), it));    map<int,int> mp;    int m = oa.size();    for(int i = 0; i < m; i++) mp[oa[i]] = i;    for(auto& i : a) i = mp[i];       vector<vector<int>> ct(m);    for(int i = 0; i < n; i++) ct[a[i]].push_back(i);       vector<vector<int>> g(n);    for(int i = 0; i < n-1; i++) {     int u, v; cin >> u >> v; u--, v--;     g[u].push_back(v);     g[v].push_back(u);    }       int time = 0;    vector<int> in(n,-1), out(n);    function<void(int)> tour = [&] (int v) {     in[v] = time;     for(auto u : g[v]) if(in[u] == -1) tour(u);     out[v] = time++;    };       tour(0);       auto very_bad = [&] () {     cout << 0 << endl;     exit(0);    };       vector<int> sum(n+1, 0);    auto update = [&] (int l, int r, int x) {     sum[l] += x, sum[r + 1] -= x;    };       for(int i = 0; i < m; i++) if(ct[i].size() > 1) {     vector<pair<int,int>> aux;     for(auto u : ct[i]) aux.push_back({out[u], u});     sort(aux.begin(), aux.end());        set<int> cur;     int anc = -1, nanc = 0;     for(auto [outt, u] : aux) {      int l = in[u], r = out[u];      if(nanc and r > out[anc]) very_bad();            auto itl = cur.lower_bound(l);      auto itr = cur.upper_bound(r);            if(itl != itr) {       anc = u, nanc++;       if(itl != cur.begin() or itr != cur.end()) very_bad();      }      if(nanc > 2) very_bad();            cur.insert(r);     }        if(nanc == 0) {      for(auto u : ct[i]) update(in[u], out[u], 1);     }     else {      bool found = false;      int good_child = -1;      for(auto u : g[anc]) if(out[u] < out[anc]) {       int l = in[u], r = out[u];       auto itl = cur.lower_bound(l);       auto itr = cur.upper_bound(r);          if(itl != itr) {        if(found) very_bad();        found = true;        good_child = u;       }      }         update(0, n-1, 1);      update(in[good_child], out[good_child], -1);      for(auto u : ct[i]) if(u != anc) {       update(in[u], out[u], 1);      }     }    }       int ans = 0;    for(int i = 0; i < n; i++) {     if(i) sum[i] += sum[i-1];     if(!sum[i]) {      ans++;     }    }       cout << ans << endl;       return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, long long int y, long long int m) {     if (y == 0) return 1;     long long int p = power(x, y / 2, m) % m;     p = (p * p) % m;     return (y % 2 == 0) ? p : (x * p) % m;   }   long long int nCr(long long int n, long long int r, long long int m) {     if (r > n) return 0;     long long int a = 1, b = 1, i;     for (i = 0; i < r; i++) {       a = (a * n) % m;       --n;     }     while (r) {       b = (b * r) % m;       --r;     }     return (a * power(b, m - 2, m)) % m;   }   vector<vector<int>> sufs, suft, ans;   string s, t;   int dp(int a, int b) {     if (b == 0) return 0;     if (ans[a][b] != -1) return ans[a][b];     ans[a][b] = 1e5;     if (a > 0) ans[a][b] = min(ans[a][b], 1 + dp(a - 1, b));     if (s[a - 1] == t[b - 1] && a > 0)       ans[a][b] = min(ans[a][b], dp(a - 1, b - 1));     int ch = t[b - 1] -  a ;     if (sufs[a][ch] - suft[b][ch] > 0) ans[a][b] = min(ans[a][b], dp(a, b - 1));     return ans[a][b];   }   void solve() {     int n;     cin >> n;     cin >> s >> t;     sufs = suft = vector<vector<int>>(n + 1, vector<int>(26, 0));     for (int i = n - 1; i >= 0; --i) {       sufs[i][s[i] -  a ]++;       suft[i][t[i] -  a ]++;       for (int j = 0; j < 26; ++j) {         sufs[i][j] += sufs[i + 1][j];         suft[i][j] += suft[i + 1][j];         if (i == 0 && sufs[i][j] != suft[i][j]) {           cout << -1 <<   n ;           return;         }       }     }     ans = vector<vector<int>>(n + 1, vector<int>(n + 1, -1));     cout << dp(n, n);     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   int main() {     char a[100];     int i, j, k, low = 0, high = 0;     gets(a);     for (i = 0; a[i] !=   0 ; i++) {       if (a[i] >= 65 && a[i] <= 90)         high++;       else         low++;     }     if (high == low) {       for (i = 0; a[i] !=   0 ; i++) {         if (a[i] >= 65 && a[i] <= 90) a[i] += 32;       }     } else if (high > low) {       for (i = 0; a[i] !=   0 ; i++) {         if (a[i] >= 97 && a[i] <= 122) a[i] -= 32;       }     } else if (low > high) {       for (i = 0; a[i] !=   0 ; i++) {         if (a[i] >= 65 && a[i] <= 90) a[i] += 32;       }     }     puts(a);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long n, d = 0, e = 0, a[10000] = {0}, c = 0;     cin >> n;     for (long i = 0; i < n; i++) {       cin >> a[i];       c = c + a[i];     }     for (long j = 0; j < n; j++) {       d = c - a[j];       if ((d % 2) == 0) {         e = e + 1;       }     }     cout << e;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const int N = 100005;   const double PI = 4 * atan(1);   const double eps = 1e-7;   const long long oo = 1e10;   const int K = 26;   vector<int> sort_cyclic_shifts(string const& s) {     int n = s.size();     const int alphabet = 256;     vector<int> p(n), c(n), cnt((alphabet > n ? alphabet : n), 0);     for (int i = 0; i < n; i++) cnt[s[i]]++;     for (int i = 1; i < alphabet; i++) cnt[i] += cnt[i - 1];     for (int i = 0; i < n; i++) p[--cnt[s[i]]] = i;     c[p[0]] = 0;     int classes = 1;     for (int i = 1; i < n; i++) {       if (s[p[i]] != s[p[i - 1]]) classes++;       c[p[i]] = classes - 1;     }     vector<int> pn(n), cn(n);     for (int h = 0; (1 << h) < n; ++h) {       for (int i = 0; i < n; i++) {         pn[i] = p[i] - (1 << h);         if (pn[i] < 0) pn[i] += n;       }       fill(cnt.begin(), cnt.begin() + classes, 0);       for (int i = 0; i < n; i++) cnt[c[pn[i]]]++;       for (int i = 1; i < classes; i++) cnt[i] += cnt[i - 1];       for (int i = n - 1; i >= 0; i--) p[--cnt[c[pn[i]]]] = pn[i];       cn[p[0]] = 0;       classes = 1;       for (int i = 1; i < n; i++) {         pair<int, int> cur = {c[p[i]], c[(p[i] + (1 << h)) % n]};         pair<int, int> prev = {c[p[i - 1]], c[(p[i - 1] + (1 << h)) % n]};         if (cur != prev) ++classes;         cn[p[i]] = classes - 1;       }       c.swap(cn);     }     return p;   }   vector<int> suffix_array_construction(string s) {     s +=  $ ;     vector<int> sorted_shifts = sort_cyclic_shifts(s);     sorted_shifts.erase(sorted_shifts.begin());     return sorted_shifts;   }   vector<int> lcp_construction(string const& s, vector<int> const& p) {     int n = s.size();     vector<int> rank(n, 0);     for (int i = 0; i < n; i++) rank[p[i]] = i;     int k = 0;     vector<int> lcp(n - 1, 0);     for (int i = 0; i < n; i++) {       if (rank[i] == n - 1) {         k = 0;         continue;       }       int j = p[rank[i] + 1];       while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;       lcp[rank[i]] = k;       if (k) k--;     }     return lcp;   }   string s;   long long k;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> s;     long long n = s.size();     cin >> k;     if (k > n * (n + 1) / 2) {       cout <<  No such line. ;       return 0;     }     vector<int> p = suffix_array_construction(s);     vector<int> lcp = lcp_construction(s, p);     int st = 0;     for (int i = 0; i < n; i++) {       if (i == 0) {         st = 0;       } else {         st = lcp[i - 1];       }       while (st < n - p[i] && k) {         int j = i;         k--;         while (j < n - 1 && lcp[j] > st && k) {           j++;           k--;           if (k == 0) break;         }         if (k == 0) break;         st++;       }       if (k == 0) {         for (int o = p[i]; o <= p[i] + st; o++) {           cout << s[o];         }         return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const long long MOD = 1e9 + 7;   vector<int> ans1, ans2;   int a[N];   bool work(bool c[], int n) {     vector<int> p1, p2;     for (int i = 0; i < n; ++i) {       if (c[i])         p1.push_back(a[i]);       else         p2.push_back(a[i]);     }     if (p2.size() == 0)       return 0;     else if (p2.size() <= 2) {       ans1 = p1;       ans2 = p2;       return 1;     } else {       int r = p2[1] - p2[0];       for (int i = 2; i < p2.size(); ++i) {         if (p2[i] - p2[i - 1] != r) return 0;       }       ans1 = p1;       ans2 = p2;       return 1;     }   }   bool check(int f, int r, int n) {     bool c[n];     memset(c, false, sizeof(c));     vector<int> p1, p2;     for (int i = 0; i < n; ++i) {       if (a[i] == f) {         c[i] = 1;         f += r;       }     }     if (work(c, n)) return 1;     for (int i = n - 1; i >= 0; --i) {       if (c[i]) {         c[i] = 0;         return work(c, n);       }     }     return 0;   }   void printSolution() {     for (int i = 0; i < ans1.size(); ++i) {       cout << ans1[i] <<    n [ans1.size() - i == 1];     }     for (int i = 0; i < ans2.size(); ++i) {       cout << ans2[i] <<    n [ans2.size() - i == 1];     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     if (check(a[0], a[1] - a[0], n)) {       printSolution();     } else if (check(a[0], a[2] - a[0], n)) {       printSolution();     } else if (check(a[1], a[2] - a[1], n)) {       printSolution();     } else       cout <<  No solution  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   long long n, i;   long long a[102];   long long dp[102][102][2];   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       cin >> n;       string s[2];       cin >> s[0] >> s[1];       long long r, c;       r = 0;       for (i = 0; i < n; ++i) {         if (s[r % 2][i] >=  3 ) {           if (s[(r + 1) % 2][i] <  3 ) {             break;           } else {             r = (r + 1) % 2;           }         }       }       if (i == n && r == 1) {         puts( yes n );       } else         puts( no n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200018;   int arr[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     map<long long, int> mp;     long long sum1 = 0;     for (int i = 1; i <= n; i++) {       cin >> arr[i], sum1 += arr[i];       mp[sum1] = i;     }     long long sum3 = 0, ans = 0;     for (int i = n; i >= 1; i--) {       sum3 += arr[i];       if (mp[sum3] && mp[sum3] < i) {         ans = sum3;       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 1e5 + 10;   const long long INF = 2e9;   const double PI = acos(-1);   long long n, m, p, pi = 1;   long long fa[MAXN], k;   long long siz[MAXN];   template <typename T>   inline void read(T &a) {     a = 0;     char c = getchar();     long long f = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       a = a * 10 + (c ^ 48);       c = getchar();     }     a *= f;   }   long long find(long long x) {     if (x != fa[x]) return fa[x] = find(fa[x]);     return fa[x];   }   void merge(long long x, long long y) {     x = find(x), y = find(y);     if (x == y) return;     siz[x] += siz[y];     fa[y] = x;   }   long long qpow(long long x, long long b) {     long long res = 1;     if (b <= 0) return res;     for (; b; b >>= 1, (x *= x) %= p)       if (b & 1) (res *= x) %= p;     return res;   }   signed main() {     read(n);     read(m);     read(p);     for (long long i = 1; i <= n; ++i) fa[i] = i, siz[i] = 1;     for (long long i = 1, x, y; i <= m; ++i) {       read(x);       read(y);       merge(x, y);     }     for (long long i = 1; i <= n; ++i)       if (fa[i] == i) (pi *= siz[i]) %= p, k++;     if (k < 2)       printf( %d n , 1 % p);     else       printf( %lld n , qpow(n, k - 2) * pi % p);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long len(long long x, long long y) { return x * x + y * y; }   long long scal(long long x1, long long y1, long long x2, long long y2) {     if (x1 * y2 == x2 * y1) return -1;     return x1 * x2 + y1 * y2;   }   bool in(long long x1, long long y1, long long x2, long long y2, long long x,           long long y) {     long long vx = x1 - x;     long long vy = y1 - y;     long long wx = x2 - x;     long long wy = y2 - y;     if (vx * wy == vy * wx && vx * wx + vy * wy < 0) return true;     return false;   }   bool sootn(long long x1, long long y1, long long x2, long long y2, long long x,              long long y) {     long long vx = x - x1;     long long vy = y - y1;     long long wx = x - x2;     long long wy = y - y2;     long long l1 = len(vx, vy);     long long l2 = len(wx, wy);     if (l1 >= l2)       return (16 * l2 >= l1);     else       return (16 * l1 >= l2);   }   int Solution() {     int t;     scanf( %d , &t);     for (int i = 0; i < t; ++i) {       long long x[6], y[6];       for (int j = 0; j < 6; ++j) scanf( %I64d%I64d , &x[j], &y[j]);       int ind = -1;       for (int j = 0; j < 6; ++j)         for (int k = j + 1; k < 6; ++k)           if (x[j] == x[k] && y[j] == y[k]) {             ind = j;             break;           }       if (ind == -1) {         printf( NO n );         continue;       }       int ind1 = -1, ind2;       for (int j = 0; j < 6; ++j)         if (x[j] == x[ind] && y[j] == y[ind])           if (ind1 == -1)             ind1 = j;           else             ind2 = j;       if (ind1 & 1)         ind1--;       else         ind1++;       if (ind2 & 1)         ind2--;       else         ind2++;       long long an = scal(x[ind1] - x[ind], y[ind1] - y[ind], x[ind2] - x[ind],                           y[ind2] - y[ind]);       if (an < 0) {         printf( NO n );         continue;       }       int num3 = 3 - ind1 / 2 - ind2 / 2;       if (in(x[ind], y[ind], x[ind1], y[ind1], x[num3 * 2], y[num3 * 2]) &&           in(x[ind], y[ind], x[ind2], y[ind2], x[num3 * 2 + 1],              y[num3 * 2 + 1])) {         bool soot =             sootn(x[ind], y[ind], x[ind1], y[ind1], x[num3 * 2], y[num3 * 2]);         if (!soot) {           printf( NO n );           continue;         }         soot = sootn(x[ind], y[ind], x[ind2], y[ind2], x[num3 * 2 + 1],                      y[num3 * 2 + 1]);         if (!soot) {           printf( NO n );           continue;         }       } else if (in(x[ind], y[ind], x[ind1], y[ind1], x[num3 * 2 + 1],                     y[num3 * 2 + 1]) &&                  in(x[ind], y[ind], x[ind2], y[ind2], x[num3 * 2], y[num3 * 2])) {         bool soot = sootn(x[ind], y[ind], x[ind1], y[ind1], x[num3 * 2 + 1],                           y[num3 * 2 + 1]);         if (!soot) {           printf( NO n );           continue;         }         soot = sootn(x[ind], y[ind], x[ind2], y[ind2], x[num3 * 2], y[num3 * 2]);         if (!soot) {           printf( NO n );           continue;         }       } else {         printf( NO n );         continue;       }       printf( YES n );     }     return 0;   }   int main() {     Solution();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const long double pi = acos(-1.0);   const long double eps = 1e-9;   const int MAX_SIZE = 50;   const int dX[] = {-1, 1, 0, 0, -1, 1, -1, 1};   const int dY[] = {0, 0, -1, 1, 1, -1, -1, 1};   template <class S, class T>   ostream& operator<<(ostream& o, const pair<S, T>& a) {     o << a.first <<     << a.second;     return o;   }   template <class S, class T>   ostream& operator<<(ostream& o, const map<S, T>& a) {     for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)       o << (*it) <<   n ;     return o;   }   template <class S>   ostream& operator<<(ostream& o, const vector<S>& a) {     for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)       o << *it <<    ;     return o;   }   template <class S>   ostream& operator<<(ostream& o, const vector<vector<S> >& a) {     for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) {       o << (*it) <<   n ;     }     return o;   }   template <class S>   ostream& operator<<(ostream& o, const set<S>& a) {     for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)       o << *it <<    ;     return o;   }   const long long MAX = 30;   int cnt[MAX];   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     ;     int w, h, x, y, x1, y1;     long long ans = 0;     cin >> w >> h;     for (x = (1);          (((1) > 0) - ((1) < 0)) * x < (((1) > 0) - ((1) < 0)) * (w / 2 + 1);          x += (1)) {       for (y = (1);            (((1) > 0) - ((1) < 0)) * y < (((1) > 0) - ((1) < 0)) * (h / 2 + 1);            y += (1)) {         x1 = w - x;         y1 = h - y;         ans += (y1 - y + 1) * (x1 - x + 1);       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, x, s, d, t, a, b, r[5000], l, res1, res2, k;   int main() {     cin >> n >> k;     for (int i = 0; i < k; i++) {       cin >> t;       if (t == 1) {         cin >> a >> b;         r[a] = r[b] = 1;         l += 2;       } else {         cin >> a;         r[a] = 1;         l++;       }     }     res2 = n - 1 - l;     for (int i = 1; i < n; i++) {       if (r[i] == 0) {         if ((i < n - 1) && (r[i + 1] == 0))           r[i] = r[i + 1] = 1, res1++;         else           res1++;       }     }     cout << res1 <<     << res2 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxx = 1e5 + 20;   int n, c[maxx], sz[maxx], rang[maxx], okd[maxx], oku[maxx], t[maxx];   bool vis[maxx], takrang[maxx];   vector<int> adj[maxx];   void dfs(int root) {     vis[root] = true;     int tedad = t[c[0]];     if (c[0] == c[root]) tedad--;     bool bb = false, payin = true, trb = true;     for (int x : adj[root]) {       if (!vis[x]) {         bb = true;         dfs(x);         if (c[0] == c[x]) tedad -= sz[x];         sz[root] += sz[x];         if (c[root] != c[x] || !takrang[x]) trb = false;         if (!takrang[x]) payin = false;       }     }     if (!bb) {       takrang[root] = true, rang[root] = c[root], okd[root] = true, sz[root] = 1;       if ((c[root] == c[0] && t[c[0]] == n) ||           (c[root] != c[0] && t[c[0]] == n - 1))         oku[root] = true;       return;     }     sz[root]++;     takrang[root] = trb;     okd[root] = payin;     if (root == 0)       oku[root] = true;     else if (tedad == n - sz[root])       oku[root] = true;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       u--, v--;       adj[u].push_back(v);       adj[v].push_back(u);     }     for (int i = 0; i < n; i++) cin >> c[i], t[c[i]]++;     dfs(0);     for (int i = 0; i < n; i++)       if (oku[i] && okd[i]) return cout <<  YES  << endl << i + 1 << endl, 0;     cout <<  NO  << endl;     return 0;   }