malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   unsigned *G[222222], Ga[222222], Gi[222222];   void add(unsigned i, unsigned j) {     if (Ga[i] == Gi[i]) {       if (!Ga[i])         G[i] = (unsigned *)calloc(Ga[i] = 1, sizeof(unsigned));       else         G[i] = (unsigned *)realloc(G[i], (Ga[i] <<= 1) * sizeof(unsigned));     }     G[i][Gi[i]++] = j;     return;   }   unsigned P[222222], D[222222], IL[222222], mi, ma;   void F(unsigned n, unsigned p, unsigned d) {     if (!d) {       mi = -1;       ma = 0;     }     unsigned i, j;     P[n] = p;     D[n] = d;     if (IL[n]) {       if (mi > d) mi = d;       if (ma < d) ma = d;     }     for (i = Gi[n]; i--;)       if ((j = G[n][i]) != p) F(j, n, d + 1);     return;   }   unsigned L[222222], Li, Q[9], Qi;   int main() {     unsigned n, i, j, k, x;     scanf( %u , &n);     for (k = n; --k;) {       scanf( %u%u , &i, &j);       add(i, j);       add(j, i);     }     for (i = 0; ++i <= n;)       if (Gi[i] == 1) IL[L[Li++] = i] = 1;     F(L[0], 0, 0);     for (i = j = k = 0; ++i <= n;)       if (D[i] > k) k = D[j = i];     F(j, 0, 0);     for (i = j = k = 0; ++i <= n;)       if (D[i] > k) k = D[j = i];     for (i = k >> 1; i--;) j = P[j];     if (k & 1) {       F(i = P[j], j, 0);       if (mi != ma) goto NO;       F(j, i, 0);       if (mi != ma) goto NO;     } else {       for (i = Gi[j]; i--;) {         F(G[j][i], j, 0);         if (mi != ma) goto NO;         for (x = -1; ++x < Qi;)           if (Q[x] == mi) goto tebak;         Q[Qi++] = mi;         if (Qi > 2) goto NO;       tebak:;       }       for (k = 0, x = -1; ++x < Qi;) k += Q[x] + 1;     }     while (!(k & 1)) k >>= 1;     printf( %u n , k);     return 0;   NO:;     printf( -1 n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int const MAXN = 2e5 + 10;   int n;   int a[MAXN], L[MAXN * 30], R[MAXN * 30];   struct Trie {     int tot = 1;     int trie[MAXN * 30][2];     void insert(int pos) {       int x = 1;       for (int i = 30;; i--) {         int dir = (a[pos] >> i) & 1;         L[x] = min(L[x], pos), R[x] = max(R[x], pos);         if (i < 0) break;         if (!trie[x][dir]) trie[x][dir] = ++tot;         x = trie[x][dir];       }       return;     }     long long query(int x, int now, int dep) {       if (dep < 0) return 0ll;       int dir = (x >> dep) & 1;       if (trie[now][dir])         return query(x, trie[now][dir], dep - 1);       else         return query(x, trie[now][dir ^ 1], dep - 1) + (1ll << dep);     }     long long dfs(int now, int dep) {       if (dep < 0) return 0;       if (trie[now][0] && trie[now][1]) {         long long ans = 1ll << 30;         for (int i = L[trie[now][0]]; i <= R[trie[now][0]]; i++) {           ans = min(ans, (long long)query(a[i], trie[now][1], dep - 1));         }         ans += dfs(trie[now][0], dep - 1);         ans += dfs(trie[now][1], dep - 1);         ans += (1 << dep);         return ans;       } else if (trie[now][0]) {         return dfs(trie[now][0], dep - 1);       } else if (trie[now][1]) {         return dfs(trie[now][1], dep - 1);       }       return 0ll;     }   } trie;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     memset(L, 0x3f, sizeof(L));     for (int i = 1; i <= n; i++) trie.insert(i);     printf( %lld n , trie.dfs(1, 30));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a1, a2, a3, a4;     scanf( %d %d %d %d , &a1, &a2, &a3, &a4);     int n = min(min(a1, a4), a3);     long long ans = 0;     ans += n * 256;     a1 -= n;     a3 -= n;     a4 -= n;     int m = min(a1, a2);     ans += m * 32;     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long long inf = 1e14;   long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, ans = 0;     cin >> n;     for (long long i = 2; i <= n; i++) {       for (long long j = i + i; j <= n; j += i) {         ans += (j / i);       }     }     cout << ans * 4;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int rem = 1000000007;   vector<long long> v;   vector<long long> d(21);   vector<long long> down(21);   long long n, s;   long long binpow(long long a, long long n) {     if (n == 0) return 1;     long long tmp = binpow(a, n / 2);     if (n & 1) return tmp * tmp % rem * a % rem;     return tmp * tmp % rem;   }   long long one = 1;   long long getc(long long m, long long n) {     if (n < m) return 0;     long long ans = 1;     for (int i = 1; i <= m; ++i)       ans = (n - i + 1) % rem * ans % rem * down[i] % rem;     return ans;   }   void fn(long long indx, long long numnumbers, long long sum) {     if (sum > s) return;     if (indx == n) {       sum = s - sum;       d[numnumbers] += getc(n - one, sum + n - one);       return;     }     fn(indx + one, numnumbers, sum);     fn(indx + one, numnumbers + one, sum + v[indx] + one);   }   int main() {     cin >> n >> s;     for (int i = 1; i <= 20; i++) {       down[i] = binpow(i, rem - 2);     }     v.resize(n);     for (int i = 0; i < n; i++) {       scanf( %I64d , &v[i]);     }     long long rest = getc(n - one, n + s - one);     fn(0, 0, 0);     for (int i = 1; i <= n; i++) {       if (i & 1)         rest -= d[i];       else         rest += d[i];       rest += rem;       rest %= rem;     }     cout << rest;     return 0;   }
#include<bits/stdc++.h>   using namespace std;   int a[305];   int main()   {       int t;       cin>>t;       while(t--)       {           int n,x;           cin>>n;           string s;           cin>>s;           bool flag=false;           if(s== 2020 )               flag=true;           else           {               for(int i=0; i<n; ++i)               {                   string tmp=s;                   if(n-4>=0)                       tmp.erase(i,n-4);                   if(tmp== 2020 )                       flag=true;               }           }           if(flag)               cout<< YES ;           else               cout<< NO ;           cout<<endl;       }       //getchar();       //getchar();       return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> adj[100005];   bitset<100005> vis;   int nodes = 0, edges = 0;   void dfs(int v) {     nodes++;     vis[v] = true;     for (auto nv : adj[v]) {       if (!vis[nv]) dfs(nv);       edges++;     }   }   int main() {     int n, k;     scanf( %d%d , &n, &k);     int x[k], y[k];     for (int i = 0; i < k; i++) {       scanf( %d%d , &x[i], &y[i]);       adj[--x[i]].push_back(--y[i]);       adj[y[i]].push_back(x[i]);     }     int sad = 0;     for (int i = 0; i < n; i++) {       if (!vis[i]) {         nodes = 0;         edges = 0;         dfs(i);         edges >>= 1;         sad += edges - nodes + 1;       }     }     printf( %d n , sad);   }
#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long b) {     long long ans = 1;     while (b > 0) {       if (b % 2 != 0) {         ans = (ans * a) % 1000000007;       }       a = (a * a) % 1000000007;       b >>= 1;     }     return ans;   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t = 1;     cin >> t;     while (t--) {       long long d, m;       cin >> d >> m;       vector<int> v, p, q;       long long z = 1, s = 1;       v.push_back(1);       for (int i = 2; i < 65; i++) {         long long k = pow(2, i - 1);         if (s + k < d) {           v.push_back(k);           s += k;         } else {           v.push_back(d - s);           break;         }       }       vector<long long> ha;       ha.push_back(1);       ha.push_back(0);       for (int i = 0; i < v.size(); i++) {         vector<long long> u;         u.push_back(1);         for (int j = 0; j < ha.size() - 1; j++) {           u.push_back((ha[j] * v[i] + ha[j + 1]) % m);         }         u.push_back(0);         ha = u;       }       long long sum = 0;       for (int i = 1; i < ha.size(); i++) {         sum += ha[i];         sum %= m;       }       cout << sum << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   void read(long long &x) {     x = 0;     char ch = getchar();     long long pd = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) {         pd = -pd;       }       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     x *= pd;   }   long long gcd(long long x, long long y) { return !x ? y : gcd(y % x, x); }   long long l, n, m, aa[10010];   long long a[N], b[N];   long long ans, lcm, x, y, bg;   long long mul(long long a, long long b, long long P) {     long long s = 0;     for (; b; b >>= 1, a = (a + a) % P)       if (b & 1) s = (s + a) % P;     return s;   }   long long exgcd(long long a, long long b, long long &x, long long &y) {     if (!b) {       x = 1, y = 0;       return a;     }     long long gd = exgcd(b, a % b, y, x);     y -= a / b * x;     return gd;   }   long long excrt() {     ans = a[1], lcm = b[1], x, y;     for (long long i = 2; i <= l; i++) {       long long B = b[i], C = (a[i] - ans % B + B) % B;       long long d = exgcd(lcm, B, x, y);       if (C % d) return -1;       B = B / d;       C = C / d;       x = (x + B) % B;       x = mul(x, C, B);       ans += x * lcm;       lcm *= B;       ans %= lcm;     }     return ans == 0 ? lcm : ans;   }   signed main() {     scanf( %lld%lld%lld , &n, &m, &l);     if (l > m) {       puts( NO );       return 0;     }     for (register long long i = 1; i <= l; ++i) {       scanf( %lld , &aa[i]);       b[i] = aa[i];     }     for (register long long i = 1; i <= l; ++i) {       a[i] = (1 - i);       while (a[i] < aa[i]) {         a[i] += aa[i];       }       a[i] %= aa[i];     }     long long yy = excrt();     if (yy == -1) {       puts( NO );       return 0;     }     if (yy + l - 1 > m) {       puts( NO );       return 0;     }     if (lcm > n) {       puts( NO );       return 0;     }     for (register long long i = 1; i <= l; ++i) {       if (gcd(lcm, yy + i - 1) != aa[i]) {         puts( NO );         return 0;       }     }     puts( YES );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long nax = 1e5 + 100;   long long n, m;   signed main() {     cin >> n >> m;     long long dp[n][m], a[n][m];     for (long long i = 0; i < n; i++)       for (long long j = 0; j < m; j++) {         cin >> a[i][j];         dp[i][j] = 1;       }     for (long long j = 0; j < m; j++)       for (long long i = 1; i < n; i++)         if (a[i][j] >= a[i - 1][j]) dp[i][j] = dp[i - 1][j] + 1;     for (long long i = 0; i < n; i++) sort(dp[i], dp[i] + m);     long long q;     cin >> q;     while (q--) {       long long l, r;       cin >> l >> r;       --l;       --r;       if (dp[r][m - 1] >= (r - l + 1))         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dp[105][105][12];   int main() {     int n, C, q;     scanf( %d%d%d , &n, &q, &C);     for (int i = 1; i <= n; i++) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       dp[a][b][c]++;     }     for (int i = 1; i <= 100; i++)       for (int j = 1; j <= 100; j++)         for (int k = 0; k <= 10; k++)           dp[i][j][k] += dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k];     while (q--) {       int t, x1, x2, y1, y2;       scanf( %d%d%d%d%d , &t, &x1, &y1, &x2, &y2);       long long ans = 0;       for (int i = 0; i <= C; i++) {         int l = (i + t) % (C + 1);         ans += l * (dp[x2][y2][i] - dp[x2][y1 - 1][i] - dp[x1 - 1][y2][i] +                     dp[x1 - 1][y1 - 1][i]);       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<string, int> fr;   int main() {     ios_base::sync_with_stdio(false);     string s;     cin >> s;     for (int k = 0; k <= 9; k++) {       string a;       cin >> a;       fr[a] = k;     }     int id = 0;     string act =   ;     for (int k = 0; k < 80; k++) {       act += s[k];       id++;       if (id == 10) {         cout << fr[act];         id = 0;         act =   ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mxn = 1e5 + 10;   int n, m;   bool jest[410][410];   bool vis_sam[mxn], vis_poc[mxn];   int sam[mxn], poc[mxn];   void wczytaj() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int a, b;       scanf( %d%d , &a, &b);       jest[a][b] = true;       jest[b][a] = true;     }   }   void bfs_sam() {     queue<int> q;     q.push(1);     vis_sam[1] = true;     while (!q.empty()) {       int v = q.front();       q.pop();       for (int i = 1; i <= n; i++) {         if (i == v) continue;         if (vis_sam[i]) continue;         if (!jest[i][v]) continue;         sam[i] = sam[v] + 1;         q.push(i);         vis_sam[i] = true;       }     }   }   void bfs_poc() {     queue<int> q;     q.push(1);     vis_poc[1] = true;     while (!q.empty()) {       int v = q.front();       q.pop();       for (int i = 1; i <= n; i++) {         if (i == v) continue;         if (vis_poc[i]) continue;         if (jest[i][v]) continue;         poc[i] = poc[v] + 1;         q.push(i);         vis_poc[i] = true;       }     }   }   int main() {     wczytaj();     bfs_poc();     bfs_sam();     if (vis_poc[n] == false || vis_sam[n] == false) {       printf( -1 n );       return 0;     }     printf( %d n , max(poc[n], sam[n]));   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5050, b = 53, mod = 1073676287;   int n, q, pre[N], suf[N], ans[N], binv[N], dp[N][N];   int mul(int x, int y) { return (x * 1ll * y) % mod; }   int add(int x, int y) { return ((x + y) % mod + mod) % mod; }   int pw(int b, int p) {     if (!p) return 1;     int ret = pw(b, p / 2);     ret = mul(ret, ret);     if (p % 2) ret = mul(ret, b);     return ret;   }   int getl(int l, int r) {     int f = add(pre[r], -pre[l - 1]);     return mul(f, binv[l]);   }   int getr(int l, int r) {     int f = add(suf[l], -suf[r + 1]);     return mul(f, binv[n - r + 1]);   }   int check(int l, int r) { return getl(l, r) == getr(l, r); }   int solve(int i, int j) {     if (i > n || j <= 0 || j < i) return 0;     int &x = dp[i][j];     if (x != -1) return x;     return x = check(i, j) + solve(i, j - 1) + solve(i + 1, j) -                solve(i + 1, j - 1);   }   string s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> s;     memset(dp, -1, sizeof dp);     n = s.size();     s =  @  + s;     int bb = 1;     for (int i = 1; i <= n; i++) {       binv[i] = pw(bb, mod - 2);       pre[i] = add(pre[i - 1], mul((s[i] -  a ), bb));       bb = mul(bb, b);     }     bb = 1;     for (int i = n; i >= 1; i--) {       suf[i] = add(suf[i + 1], mul((s[i] -  a ), bb));       bb = mul(bb, b);     }     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       cout << solve(l, r) <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[20];   int main() {     cin >> n >> m;     for (int t, i = 1; i <= n; i++) {       cin >> t;       a[t]++;     }     int ans = 0;     for (int i = 1; i <= m; i++)       for (int j = i + 1; j <= m; j++) ans += a[i] * a[j];     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > ke[105];   int d[105], x[105], y[105], a[105];   int n, m, s, t, module;   void dijkstra() {     set<pair<int, int> > q;     for (int u = 1; u <= n; ++u) d[u] = 1000000007;     d[1] = 0;     q.insert(make_pair(d[1], 1));     while (!q.empty()) {       pair<int, int> top = *(q.begin());       q.erase(top);       int u = top.second;       if (u == n) return;       for (int i = 0; i < ke[u].size(); ++i) {         int v = ke[u][i].first;         int w = ke[u][i].second;         if (d[v] > d[u] + w) {           q.erase(make_pair(d[v], v));           d[v] = d[u] + w;           q.insert(make_pair(d[v], v));         }       }     }   }   int main() {     cin >> n >> module;     for (int i = 2; i <= n - 1; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) scanf( %d%d , &x[i], &y[i]);     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         if (i != j) {           ke[i].push_back(make_pair(               j, module * (abs(x[i] - x[j]) + abs(y[i] - y[j])) - a[j]));           ke[j].push_back(make_pair(               i, module * (abs(x[i] - x[j]) + abs(y[i] - y[j])) - a[i]));         } else {           ke[i].push_back(make_pair(j, 1000000007));           ke[j].push_back(make_pair(i, 1000000007));         }       }     }     s = 1;     t = n;     dijkstra();     if (d[t] == 1000000007)       printf( -1 );     else       printf( %d n , d[t]);     return 0;   }
#include <bits/stdc++.h>   const long long MOD = 1e9 + 7;   const long long MAXN = 1e6 + 1;   using namespace std;   long long readInt() {     bool minus1 = false;     long long result = 0;     char ch;     ch = getchar();     while (true) {       if (ch ==  - ) break;       if (ch >=  0  && ch <=  9 ) break;       ch = getchar();     }     if (ch ==  - )       minus1 = true;     else       result = ch -  0 ;     while (true) {       ch = getchar();       if (ch <  0  || ch >  9 ) break;       result = result * 10 + (ch -  0 );     }     if (minus1)       return -result;     else       return result;   }   long long a[MAXN];   long long b[MAXN];   long long pref_a[MAXN];   long long pref_b[MAXN];   long long mn[MAXN];   long long n, m;   long long x;   long long GGGG(long long kek) {     int l = 1, r = m;     while (r - l > 1) {       int mid = (l + r) / 2;       if (kek * mn[mid] <= x) {         l = mid;       } else {         r = mid;       }     }     return (kek * mn[r] <= x ? r : kek * mn[l] <= x ? l : 0);   }   int main() {     n = readInt(), m = readInt();     for (int i = 1; i <= n; i++) {       a[i] = readInt();     }     for (int j = 1; j <= m; j++) {       b[j] = readInt();     }     x = readInt();     for (int i = 1; i <= n; i++) {       pref_a[i] = pref_a[i - 1] + a[i];     }     for (int i = 1; i <= m; i++) {       pref_b[i] = pref_b[i - 1] + b[i];     }     multiset<pair<int, int> > second;     for (int i = 1; i <= m; i++) {       mn[i] = MOD;     }     for (int i = 1; i <= m; i++) {       for (int j = i; j <= m; j++) {         mn[j - i + 1] = min(mn[j - i + 1], pref_b[j] - pref_b[i - 1]);       }     }     long long mx = 0;     for (int i = 1; i <= n; i++) {       for (int j = i; j <= n; j++) {         mx = max(mx, (j - i + 1) * GGGG(pref_a[j] - pref_a[i - 1]));       }     }     cout << mx;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int mod = 1000000007;   long long int max(long long int a, long long int b) {     if (a > b)       return a;     else       return b;   }   long long int min(long long int a, long long int b) {     if (a < b)       return a;     else       return b;   }   long long int binpow(long long a, long long b, long long m) {     a %= m;     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   long long binpow(long long a, long long b) {     if (b == 0) return 1;     long long res = binpow(a, b / 2);     if (b % 2)       return res * res * a;     else       return res * res;   }   vector<vector<pair<int, int>>> adj;   vector<int> dp;   vector<int> ans;   int dfs(int x, int p) {     for (auto c : adj[x]) {       if (c.first == p) continue;       int val = dfs(c.first, x);       if (val == 0 && c.second == 2) {         dp[x] += val + 1;         ans.push_back(c.first);       } else {         dp[x] += val;       }     }     return dp[x];   }   void solve() {     int n;     cin >> n;     adj.resize(n + 1);     for (int i = 0; i < n - 1; i++) {       int x, y, t;       cin >> x >> y >> t;       adj[x].push_back({y, t});       adj[y].push_back({x, t});     }     dp.resize(n + 1);     dfs(1, 0);     cout << dp[1] << endl;     for (auto c : ans) {       cout << c <<    ;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     long long l = s.size();     for (long long i = 1; i < l - 1; i++) {       if (s[i] == s[i - 1]) {         if (s[i - 1] !=  a  && s[i + 1] !=  a )           s[i] =  a ;         else if (s[i - 1] !=  b  && s[i + 1] !=  b )           s[i] =  b ;         else           s[i] =  c ;       }     }     if (s[l - 1] == s[l - 2]) {       if (s[l - 2] !=  a )         s[l - 1] =  a ;       else if (s[l - 2] !=  b )         s[l - 1] =  b ;       else         s[l - 1] =  c ;     }     cout << s;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     unsigned long long int ops = 0;     long long int x, y, m;     cin >> x >> y >> m;     if (x >= m || y >= m) {       cout << 0 << endl;       return 0;     }     if (x <= 0 && y <= 0) {       cout << -1 << endl;       return 0;     }     long long int mi = min(x, y), ma = max(x, y);     long long int dif = ma - mi;     ops = dif / ma;     if (dif % ma != 0) ops++;     mi += ops * ma;     swap(mi, ma);     while (mi < m && ma < m) {       mi += ma;       swap(mi, ma);       ops++;     }     cout << ops << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int mod2 = 1e9 + 9;   const int maxN = 5e5 + 100;   const int INF = 1e9 + 7;   const int maxedge = 1e6 + 100;   const long long LL_INF = 1e18 + 11;   using namespace std;   long long n, ans;   map<long long, long long> ma;   void file() {     freopen( pluses.in ,  r , stdin);     freopen( pluses.out ,  w , stdout);   }   void boost() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   int main() {     cin >> n;     for (int long long i = 1; i * i <= n; i++) {       if (n % i == 0) {         if (ma[i] == 0) {           ans++;           ma[i]++;         }         if (ma[n / i] == 0) {           ans++;           ma[n / i]++;         }       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   char s[1010][3][50];   bool p[1010];   int main() {     int n, i, j, c;     cin >> n;     for (i = 1; i <= n; i++) cin >> s[i][1] >> s[i][2];     memset(p, true, sizeof(p));     for (i = 1; i <= n; i++) {       for (j = 1; j <= n; j++) {         if (i != j) {           if (strcmp(s[i][2], s[j][1]) == 0) {             strcpy(s[i][2], s[j][2]);             p[j] = false;           }         }       }     }     for (i = 1, c = 0; i <= n; i++)       if (p[i]) c++;     cout << c << endl;     for (i = 1; i <= n; i++) {       if (p[i]) cout << s[i][1] <<     << s[i][2] << endl;     }     return 0;   }