malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MAX = 100 * 1000 + 5;   const int INF = 1000 * 1000 * 1000 + 7;   const long long LINF = 1LL * INF * INF;   const int MOD = INF;   const double PI = acos(-1.);   int main() {     ios_base::sync_with_stdio(0);     int n, p;     cin >> n >> p;     int ans = INF;     for (int k = (1); k < (200); ++k) {       long long tmp = n - k * p;       long long x = tmp;       if (tmp < 0) continue;       int cnt = 0;       while (tmp) {         cnt += tmp % 2;         tmp /= 2;       }       if (cnt <= k && k <= x) ans = min(ans, k);     }     if (ans == INF) ans = -1;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int get_index(int i, int j, int k) { return i * 4 + j * 2 + k; }   int diff(int i, int j) { return i != j; }   pair<int, string> input() {     int n;     cin >> n;     string s;     cin >> s;     return make_pair(n, s);   }   int work2(int n, string s) {     int first_diff = 1;     for (; first_diff < n; first_diff++) {       if (s[first_diff - 1] != s[first_diff]) break;     }     if (first_diff == n) return (n + 2) / 3;     s = s.substr(first_diff, n - first_diff) + s.substr(0, first_diff);     int start = 0;     int ans = 0;     for (int i = 1; i < n; i++) {       if (s[i] != s[i - 1]) {         ans += (i - start) / 3;         start = i;       }     }     ans += (n - start) / 3;     return ans;   }   void work() {     pair<int, string> in = input();     int n = in.first;     string s = in.second;     cout << work2(n, s) << endl;   }   int main() {     int t;     cin >> t;     while (t--) {       work();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, s;     cin >> n;     vector<int> x(n);     for (int i = 0; i < n; i++) cin >> x[i];     sort(x.begin(), x.end());     k = n / 2;     s = x[k] - x[0];     for (int i = 1; i < n - k; i++) {       s = min(s, x[k + i] - x[i]);     }     cout << s;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long cnt[1001] = {};     for (long long i = 0; i < n; ++i) {       long long x;       cin >> x;       cnt[x]++;       if (cnt[x] > (n + 1) / 2) {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 0x3ffffffffffff;   int main() {     long long T;     scanf( %lld , &T);     while (T--) {       long long k, n, a, b;       scanf( %lld%lld%lld%lld , &k, &n, &a, &b);       k -= b * n;       if (k <= 0)         printf( -1 n );       else         printf( %lld n , min(n, (k - 1) / (a - b)));     }   }
#include <bits/stdc++.h>   using namespace std;   int dij[4444][4444];   int x[4444], y[4444];   int num[4444];   int main(void) {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &x[i], &y[i]);       dij[x[i]][y[i]] = dij[y[i]][x[i]] = 1;       num[x[i]]++;       num[y[i]]++;     }     int flag = 0;     int ans = 99999999;     for (int i = 0; i < m; i++) {       for (int j = 1; j <= n; j++) {         if (dij[x[i]][j] && dij[j][y[i]]) {           if (ans > num[x[i]] + num[j] + num[y[i]]) {             ans = num[x[i]] + num[j] + num[y[i]];             flag = 1;           }         }       }     }     if (flag == 0) {       puts( -1 );       return 0;     } else       printf( %d n , ans - 6);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<int> atk, def, ciel;     for (int i = 0; i < n; i++) {       string s;       int x;       cin >> s >> x;       if (s ==  ATK )         atk.push_back(x);       else         def.push_back(x);     }     for (int i = 0; i < m; i++) {       int x;       cin >> x;       ciel.push_back(x);     }     sort(atk.begin(), atk.end());     sort(def.begin(), def.end());     sort(ciel.begin(), ciel.end());     int flag = 1;     if (n >= m) flag = 0;     int ans = 0, i = m - 1, j = 0, sum = 0;     while (i >= 0 && j < atk.size()) {       if (atk[j] > ciel[i]) {         break;       }       sum += max(0, ciel[i] - atk[j]);       ans = max(ans, sum);       i--;       j++;     }     i = m - 1;     j = def.size() - 1;     int ans1 = 0, ans2 = 0;     while (i >= 0 && j >= 0) {       if (ciel[i] <= def[j]) {         flag = 0;         break;       }       i--;       j--;     }     i = 0;     vector<int> temp;     if (flag) {       j = 0;       while (i < m && j < def.size()) {         while (ciel[i] <= def[j]) {           temp.push_back(ciel[i]);           i++;           if (i == m) break;         }         i++;         j++;       }     }     while (i < m) {       temp.push_back(ciel[i]);       i++;     }     j = 0;     i = 0;     sum = 0;     int flag2 = 1;     int ans3 = 0;     while (i < temp.size() && j < atk.size()) {       while (temp[i] < atk[j]) {         ans3 += temp[i];         i++;         if (i == temp.size()) break;       }       if (i == temp.size()) break;       ans1 += temp[i] - atk[j];       i++;       j++;     }     while (i < temp.size()) {       ans3 += temp[i];       i++;     }     if (flag && j == atk.size()) ans1 += ans3;     i = temp.size() - 1;     j = 0;     while (i >= 0 && j < atk.size()) {       if (atk[j] > temp[i]) {         flag2 = 0;         break;       }       sum += max(0, temp[i] - atk[j]);       ans2 = max(ans2, sum);       i--;       j++;     }     if (flag2 && flag) {       while (i >= 0) {         ans2 += temp[i];         i--;       }     }     cout << max(ans1, ans) << endl;   }
#include <bits/stdc++.h>   using namespace std;   const string pocetno =  0000000000 ;   const string oznake =  RGBYW ;   const int maxn = 105;   int N, razlicite, sol = maxn * maxn;   vector<pair<int, int> > V;   set<string> S;   void rijesi() {     for (int maska = 0; maska < (1 << 10) + 1; maska++) {       S.clear();       int tmp = 0;       for (int i = 0; i < 10; i++)         if (maska & (1 << i)) tmp++;       for (int i = 0; i < N; i++) {         string stanje = pocetno;         for (int j = 0; j < 5; j++) {           if (maska & (1 << j) && V[i].first == j) stanje[j] =  1 ;           if (maska & (1 << (j + 5)) && V[i].second == j) stanje[j + 5] =  1 ;         }         S.insert(stanje);       }       if (S.size() == razlicite) sol = min(sol, tmp);     }     printf( %d n , sol);   }   void ucitaj() {     scanf( %d , &N);     for (int i = 0; i < N; i++) {       string s;       cin >> s;       V.push_back(make_pair(oznake.find(s[0]), s[1] -  0  - 1));       S.insert(s);     }     razlicite = S.size();   }   int main(void) {     ucitaj();     rijesi();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};   const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},                          {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};   template <typename T, typename TT>   ostream& operator<<(ostream& s, pair<T, TT> t) {     return s <<  (  << t.first <<  ,  << t.second <<  ) ;   }   template <typename T>   ostream& operator<<(ostream& s, vector<T> t) {     for (int i = 0; i < ((int)(t.size())); i++)       s << t[i] << (i == ((int)(t.size())) - 1 ?   n  :    );     return s;   }   int start, finish, mid;   string answer;   int main() {     cin >> answer;     while (answer ==  start ) {       start = 0;       finish = 1;       while (true) {         cout <<  ?   << start <<     << finish << endl;         cin >> answer;         if (answer ==  x ) {           break;         }         start = finish;         finish <<= 1;       }       while (start < finish - 1) {         mid = (start + finish + 1) >> 1;         cout <<  ?   << start <<     << mid << endl;         cin >> answer;         if (answer[0] ==  x )           finish = mid;         else           start = mid;       }       cout <<  !   << finish << endl;       cin >> answer;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> v(2 << n);     int size = 2 << n;     for (int i = (2); i < (int)(size); ++i) cin >> v[i];     int ans = 0;     for (int i = size - 1; i >= 2; i -= 2) {       v[i / 2] += max(v[i], v[i - 1]);       ans += abs(v[i] - v[i - 1]);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, A, B, C, T;   int temp[1005];   int main() {     cin >> n >> A >> B >> C >> T;     long long ans = 0;     for (int i = 1; i <= n; i++) cin >> temp[i];     ans = n * A;     if (C > B) {       for (int i = 1; i <= n; i++) ans += (C - B) * (T - temp[i]);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   const long long MOD = 1000000007;   const double EBS = 1e-7;   int dX[] = {0, 0, 1, -1};   int dY[] = {1, -1, 0, 0};   long long GCD(long long num, long long ones) {     return !ones ? num : GCD(ones, num % ones);   }   const int L = 17;   vector<int> adj[N];   vector<int> Root;   vector<int> level[N];   int depth[N], ID[N], rID[N], dp[L + 1][N], currID = 1, n, m, k, WantedNode;   void dfs(int cur, int par) {     dp[0][cur] = par;     ID[cur] = currID++;     rID[ID[cur]] = cur;     level[depth[cur]].push_back(ID[cur]);     for (auto x : adj[cur])       if (x != par) depth[x] = depth[cur] + 1, dfs(x, cur);   }   inline int getKthAncestor(int u, int k) {     int d = depth[u] - k;     for (int j = L; j >= 0; --j) {       int nu = dp[j][u];       if (!nu) continue;       if (depth[nu] == d)         return nu;       else if (depth[nu] > d)         u = nu;     }     return 0;   }   inline int BSL(int d) {     int l = 0, r = (level[d].size()) - 1, mid;     while (l < r) {       mid = l + (r - l) / 2;       int node = getKthAncestor(rID[level[d][mid]], k);       node = ID[node];       if (node >= WantedNode)         r = mid;       else         l = mid + 1;     }     return r;   }   inline int BSR(int d) {     int l = 0, r = (level[d].size()) - 1, mid;     while (l < r) {       mid = l + (r - l + 1) / 2;       int node = getKthAncestor(rID[level[d][mid]], k);       node = ID[node];       if (node <= WantedNode)         l = mid;       else         r = mid - 1;     }     return l;   }   int main() {     scanf( %d , &n);     int p, v;     for (int i = 0; i < n; ++i) {       scanf( %d , &p);       if (p)         adj[p].push_back(i + 1), adj[i + 1].push_back(p);       else         Root.push_back(i + 1);     }     for (auto x : Root) dfs(x, 0);     for (int j = 1; j <= L; ++j)       for (int i = 0; i < N; ++i)         dp[j][i] = (dp[j - 1][i] == -1 ? -1 : dp[j - 1][dp[j - 1][i]]);     scanf( %d , &m);     while (m--) {       scanf( %d , &v);       scanf( %d , &k);       WantedNode = getKthAncestor(v, k);       if (WantedNode) {         WantedNode = ID[WantedNode];         printf( %d  , BSR(depth[v]) - BSL(depth[v]));       } else         printf( 0  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, M;   long long Ans;   struct rec {     int w, h, s, k;     rec() {}     rec(int a, int b, int c, int i) {       w = a;       h = b;       s = c;       k = i;     }   } P[1010], Q[1010];   int vis[1010];   bool cmp_h(const rec &a, const rec &b) { return a.h > b.h; }   bool cmp_s(const rec &a, const rec &b) { return a.s > b.s; }   long long sumw(int maxh) {     long long sum = 0;     int cnt = 0;     for (int i = 1; i <= N; ++i) {       if (P[i].h <= maxh) break;       int s = 1e10;       if (P[i].w <= maxh && cnt < M) {         ++cnt;         vis[P[i].k] = maxh;         s = P[i].h;       }       sum += s;     }     for (int i = 1; i <= N; ++i)       if (vis[Q[i].k] != maxh) {         int s = Q[i].w;         if (Q[i].w <= maxh && cnt < M && s > Q[i].h) {           ++cnt;           vis[Q[i].k] = maxh;           s = Q[i].h;         }         sum += s;       }     return sum;   }   int main() {     scanf( %d , &N);     M = N / 2;     for (int i = 1; i <= N; ++i) {       int w, h;       scanf( %d%d , &w, &h);       Q[i] = P[i] = rec(w, h, w - h, i);     }     sort(P + 1, P + N + 1, cmp_h);     sort(Q + 1, Q + N + 1, cmp_s);     Ans = 1e9;     for (int hi = 1; hi <= 1000; ++hi) {       Ans = min(Ans, (long long)hi * sumw(hi));     }     printf( %lld n , Ans);   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int ans = 0;     char ch = getchar();     while (!isdigit(ch)) {       ch = getchar();     }     while (isdigit(ch)) {       ans = (ans << 3) + (ans << 1) + (ch ^ 48), ch = getchar();     }     ans = ans + 0 + 0;     return ans;   }   int first[100005], n, cnt, dep[100005];   double ans = 0;   struct edge {     int v, next;   } e[100005 << 1];   inline void add(int u, int v) {     e[++cnt].v = v, e[cnt].next = first[u], first[u] = cnt;   }   inline void dfs(int p, int fa) {     for (int i = first[p]; i; i = e[i].next) {       int v = e[i].v;       if (v == fa) continue;       dep[v] = dep[p] + 1, dfs(v, p);     }   }   int main() {     n = read();     for (int i = 1; i < n; ++i) {       int u = read(), v = read();       add(u, v), add(v, u);     }     dep[1] = 1, dfs(1, 1);     for (int i = 1; i <= n; ++i) ans += 1.0 / (1.0 * dep[i]);     printf( %.20lf , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     vector<int> answer;     if (n == 1000000) {       cout <<  1 n1000000 n ;     } else {       while (n) {         int p = n;         string w;         while (p) {           if (p % 10 == 0) {             w.insert(w.begin(),  0 );           } else {             w.insert(w.begin(),  1 );           }           p = p / 10;         }         int x = atoi(&w[0]);         answer.push_back(x);         n = n - x;       }       cout << answer.size() <<   n ;       for (int i = 0; i < answer.size(); i++) {         cout << answer[i] <<    ;       }       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 505;   long long d[N][N];   long long g[N][N];   int per[N];   long long ans[N];   int n;   void upd(int q) {     for (int j = q; j < N; j++) {       g[per[q]][per[j]] = d[per[q]][per[j]];       g[per[j]][per[q]] = d[per[j]][per[q]];     }     for (int j = q; j <= n; j++) {       for (int i = q; i <= n; i++) {         g[per[q]][per[j]] =             min(g[per[q]][per[j]], g[per[q]][per[i]] + g[per[i]][per[j]]);       }     }     for (int j = q; j <= n; j++) {       for (int i = q; i <= n; i++) {         g[per[j]][per[q]] =             min(g[per[j]][per[q]], g[per[j]][per[i]] + g[per[i]][per[q]]);       }     }     for (int i = q; i <= n; i++) {       for (int j = q; j <= n; j++) {         g[per[i]][per[j]] =             min(g[per[i]][per[j]], g[per[i]][per[q]] + g[per[q]][per[j]]);       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cin >> d[i][j];         g[i][j] = (i != j) * (int)(1e9 + 9);       }     }     for (int i = 1; i <= n; i++) {       cin >> per[i];     }     for (int i = n; i >= 1; i--) {       upd(i);       for (int j = i; j <= n; j++) {         for (int z = i; z <= n; z++) {           ans[i] += g[per[j]][per[z]];         }       }     }     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline void splay(int &v) {     v = 0;     char c = 0;     int p = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) p = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       v = (v << 3) + (v << 1) + c -  0 ;       c = getchar();     }     v *= p;   }   struct Edge {     int to, next;     int col;   } edge[200010];   int first[100010], size;   void addedge(int x, int y, int z) {     size++;     edge[size].to = y;     edge[size].next = first[x];     first[x] = size;     edge[size].col = z;   }   int n, m, tmp;   bool f, flag;   int cnt = 100001, ans[100010], p[100010];   int C[100010];   bool vis[100010];   int ff[100010], sum;   void dfs(int now, int col) {     ff[++sum] = now;     vis[now] = 1;     C[now] = col;     if (C[now]) tmp++;     for (int u = first[now]; u; u = edge[u].next) {       if (vis[edge[u].to]) {         if (edge[u].col) {           if (C[edge[u].to] == C[now]) f = 0;         } else {           if (C[edge[u].to] != C[now]) f = 0;         }       } else {         dfs(edge[u].to, col ^ edge[u].col);       }     }   }   void get() {     f = 1;     int _cnt = 0;     memset(vis, 0, sizeof vis);     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         sum = 0;         tmp = 0;         dfs(i, 0);         if (f && tmp <= sum - tmp) {           for (int i = 1; i <= sum; i++) {             if (C[ff[i]]) {               p[++_cnt] = ff[i];             }           }         }         if (f && sum - tmp < tmp) {           for (int i = 1; i <= sum; i++) {             if (!C[ff[i]]) {               p[++_cnt] = ff[i];             }           }         }       }     }     if (f) {       if (_cnt <= cnt) {         for (int i = 1; i <= _cnt; i++) {           ans[i] = p[i];         }         cnt = _cnt;         flag = true;       }     }   }   int main() {     splay(n), splay(m);     for (int i = 1; i <= m; i++) {       int x, y, z;       char op[5];       splay(x), splay(y), scanf( %s , op);       if (op[0] ==  B )         z = 0;       else         z = 1;       addedge(x, y, z), addedge(y, x, z);     }     get();     for (int i = 1; i <= size; i++) {       edge[i].col ^= 1;     }     get();     if (flag) {       printf( %d n , cnt);       for (int i = 1; i <= cnt; i++) {         printf( %d  , ans[i]);       }     } else       puts( -1 );   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int dp[N][310], c[N][310], lst[N], a[N], b[N];   vector<int> v[N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m, s, e;     cin >> n >> m >> s >> e;     for (int i = 0; i < n; i++) {       cin >> a[i + 1];     }     for (int i = 0; i < m; i++) {       cin >> b[i + 1];       v[b[i + 1]].push_back(i + 1);     }     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= 300; j++) {         dp[i][j] = -1;       }       lst[i] = -1;     }     lst[0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = min(300, n); j >= 1; j--) {         if (lst[j - 1] == -1 || v[a[i]].size() == 0) continue;         if (v[a[i]].back() <= lst[j - 1]) continue;         int ind = upper_bound(v[a[i]].begin(), v[a[i]].end(), lst[j - 1]) -                   v[a[i]].begin();         dp[i][j] = v[a[i]][ind];         c[i][j] = e * j + dp[i][j] + i;         if (lst[j] == -1)           lst[j] = v[a[i]][ind];         else           lst[j] = min(v[a[i]][ind], lst[j]);       }     }     int ans = n;     for (int i = n; i >= 0; i--) {       if (lst[i] != -1) {         ans = i;         break;       }     }     for (int j = ans; j >= 1; j--) {       int k = 1000000000;       for (int i = 1; i <= n; i++) {         if (dp[i][j] != -1) {           k = min(k, c[i][j]);         }       }       if (k <= s) {         cout << j;         return 0;       }     }     cout << 0;   }
#include <bits/stdc++.h>   using namespace std;   map<string, int> M;   map<int, string> MM;   vector<int> adj[20];   int n, m;   bool val(vector<int> &v) {     for (int i = 0; i < v.size(); i++) {       for (int j = 0; j < v.size(); j++) {         if (i == j) continue;         for (int k = 0; k < adj[v[i]].size(); k++) {           if (v[j] == adj[v[i]][k]) return 0;         }       }     }     return 1;   }   void solve() {     int maxi = 0;     vector<int> v, sol;     for (int i = 0; i < (1 << n); i++) {       v.clear();       for (int j = 0; j < n; j++) {         if (i & (1 << j)) v.push_back(j);       }       if (val(v)) {         if (v.size() > maxi) {           maxi = v.size();           sol = v;         }       }     }     cout << maxi << endl;     vector<string> name;     for (int i = 0; i < sol.size(); i++) name.push_back(MM[sol[i]]);     sort(name.begin(), name.end());     for (int i = 0; i < name.size(); i++) cout << name[i] << endl;   }   int main() {     cin >> n >> m;     string s, s1;     for (int i = 0; i < n; i++) {       cin >> s;       M[s] = i;       MM[i] = s;     }     for (int i = 0; i < m; i++) {       cin >> s >> s1;       adj[M[s]].push_back(M[s1]);     }     solve();   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e4, INF = int(1e9) + 1, mod = 1e9 + 7;   const long long BIG = 1e16;   int n, d, a[N + 5], dp[N + 5][250 * 2 + 5];   bool w[N + 5][250 * 2 + 5];   int rec(int v, int l) {     if (v > N) {       return 0;     }     int l1 = l - (d - 250);     if (w[v][l1]) return dp[v][l1];     int ans = 0;     for (int i = -1; i <= 1; i++) {       if (l + i) ans = max(ans, rec(v + l + i, l + i));     }     w[v][l1] = 1;     ans += a[v];     return dp[v][l1] = ans;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> d;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       a[x]++;     }     cout << rec(d, d);   }
#include <bits/stdc++.h>   using namespace std;   const int INF = (1 << 29);   const int INFF = 0x7fffffff;   const long long LINF = (1ll << 60);   const long long LINFF = 0x7fffffffffffffff;   const long double PI = 3.14159265359;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   long long rnd(long long a, long long b) {     return uniform_int_distribution<long long>(a, b)(rng);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string a, b;       cin >> a >> b;       bool ans = true;       bool change[300][300] = {};       for (int i = 0; i < n; i++) {         if (a[i] != b[i] && a[i] > b[i]) ans = false;         if (a[i] != b[i]) change[a[i]][b[i]] = true;       }       int cnt = 0;       for (int c =  a ; c <=  t ; c++) {         for (int d = c + 1; d <=  t ; d++) {           if (change[c][d] == 0) continue;           cnt += change[c][d];           for (int e = d + 1; e <=  t ; e++) {             change[d][e] += change[c][e];           }           break;         }       }       if (ans == false) cnt = -1;       cout << cnt <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   unordered_set<string> has_handle;   unordered_set<string> is_new;   unordered_map<string, string> handle;   vector<string> handles_new;   vector<string> handles_old;   void solve(string old) {     if (is_new.count(old)) return;     handles_old.push_back(old);     string temp;     while (true) {       temp = handle[old];       if (has_handle.count(temp) == 0) break;       old = temp;     }     handles_new.push_back(temp);   }   int main() {     int q;     cin >> q;     string old_handle[q], new_handle[q];     for (int i = 0; i < q; i++) {       cin >> old_handle[i] >> new_handle[i];       has_handle.insert(old_handle[i]);       is_new.insert(new_handle[i]);       handle[old_handle[i]] = new_handle[i];     }     for (int i = 0; i < q; i++) solve(old_handle[i]);     int cnt = handles_new.size();     cout << cnt << endl;     for (int i = 0; i < cnt; i++)       cout << handles_old[i] <<     << handles_new[i] << endl;     return 0;   }