malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int M = 3000;   int n, m;   pair<pair<int, int>, int> v[M];   map<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> > > Q;   int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }   pair<pair<int, int>, int> get(int x1, int y1, int x2, int y2) {     int x = x2 - x1, y = y2 - y1;     if (x == 0)       y = 1;     else if (y == 0)       x = 1;     else {       int d = gcd(x, y);       x /= d, y /= d;     }     if (x < 0) x = -x, y = -y;     return make_pair(make_pair(x, y), y1 * x - x1 * y);   }   int sign(double x) {     if (fabs(x) < 1E-6) return 0;     return (x > 0) ? 1 : -1;   }   double dist(int x1, int y1, int x2, int y2) {     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));   }   bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {     if (a.first != b.first) return a.first < b.first;     return a.second > b.second;   }   int main() {     Q.clear();     scanf( %d%d , &n, &m);     for (int i = 0; i < (n); i++) {       int x1, y1, x2, y2;       scanf( %d%d%d%d , &x1, &y1, &x2, &y2);       x1 *= 2, y1 *= 2, x2 *= 2, y2 *= 2;       if (make_pair(x1, y1) > make_pair(x2, y2)) {         swap(x1, x2);         swap(y1, y2);       }       pair<pair<int, int>, int> tmp = get(x1, y1, x2, y2);       Q[tmp].push_back(make_pair(make_pair(x1, y1), 1));       Q[tmp].push_back(make_pair(make_pair(x2, y2), -1));     }     for (int i = 0; i < (m); i++) {       scanf( %d%d%d , &v[i].first.first, &v[i].first.second, &v[i].second);       for (int j = 0; j < (i); j++) {         if (v[i].second != v[j].second) continue;         int x1 = v[i].first.first, y1 = v[i].first.second, r = v[i].second;         int x2 = v[j].first.first, y2 = v[j].first.second;         if (sign(2 * r - dist(x1, y1, x2, y2)) >= 0) continue;         int x0 = x1 + x2, y0 = y1 + y2;         int x = x2 * 2 - x0, y = y2 * 2 - y0;         pair<pair<int, int>, int> tmp = get(x0, y0, x0 - y, y0 + x);         if (Q.find(tmp) == Q.end()) continue;         Q[tmp].push_back(make_pair(make_pair(x0, y0), 0));       }     }     int ret = 0;     for (__typeof(Q.begin()) it = Q.begin(); it != Q.end(); it++) {       sort(it->second.begin(), it->second.end(), cmp);       int now = 0;       for (__typeof(it->second.begin()) t = it->second.begin();            t != it->second.end(); t++) {         if (t->second == 0) ret += now;         now += t->second;       }     }     printf( %d n , ret);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mpow(int base, int exp);   void solve() {     long double n;     cin >> n;     long double sum = 0;     while (n) {       long double d;       d = (1) * 1.0 / n;       sum += d;       n--;     }     cout << sum <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     solve();     return 0;   }   int mpow(int base, int exp) {     base %= 1000000007;     int result = 1;     while (exp > 0) {       if (exp & 1) result = ((long long int)result * base) % 1000000007;       base = ((long long int)base * base) % 1000000007;       exp >>= 1;     }     return result;   }
#include <bits/stdc++.h>   using namespace std;   long long unsigned int countDivisors(long long unsigned int n) {     long long unsigned int cnt = 0;     for (long long unsigned int i = 1; i <= sqrt(n); i++) {       if (n % i == 0) {         if (n / i == i)           ++cnt;         else           cnt += 2;       }     }     return cnt;   }   int Search(string pat, string txt) {     int n = txt.size();     int m = pat.size();     for (int i = 0; i <= n - m; i++) {       int j;       for (j = 0; j < m; j++) {         if (txt[i + j] != pat[j]) {           break;         }       }       if (j == m) {         return i;       }     }     return -1;   }   int findFirst(const std::vector<long long int> &array, int key, int low,                 int high) {     int ans = -1;     while (low <= high) {       int mid = low + (high - low) / 2;       if (key == array[mid]) {         ans = mid;         high = mid - 1;       } else if (key > array[mid])         low = mid + 1;       else         high = mid - 1;     }     return ans;   }   int findLast(const std::vector<long long int> &array, int key, int low,                int high) {     int ans = -1;     while (low <= high) {       int mid = low + (high - low) / 2;       if (key == array[mid]) {         ans = mid;         low = mid + 1;       } else if (key > array[mid])         low = mid + 1;       else         high = mid - 1;     }     return ans;   }   int d, x, y;   void extendedEuclid(int a, int b) {     if (b == 0) {       d = 1;       x = 1;       y = 0;     } else {       extendedEuclid(b, a % b);       int temp = x;       x = y;       y = temp - (a / b) * y;     }   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t;     t = 1;     for (int tc = 0; tc < t; tc++) {       int n;       cin >> n;       vector<int> arr(n), brr(n, 0), v;       for (int i1 = 0; i1 < n; i1++) cin >> arr[i1];       int pos = 0;       for (int i = 0; i < n; i++) {         int indx = n - arr[i];         brr[indx] = 1;         while (brr[pos] == 1 && pos < n) {           cout << (n - pos) <<    ;           pos++;         }         cout << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mp[1005][1005];   long long n, a, b;   void output() {     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= n; j++) {         if (i == j)           printf( 0 );         else           printf( %lld , a < b ? !mp[i][j] : mp[i][j]);       }       printf(  n );     }   }   int main() {     scanf( %lld%lld%lld , &n, &a, &b);     long long root = 2;     if ((a != 1 && b != 1)) {       printf( NO n );       return 0;     }     if (n == 2 && a == 1 && b == 1) {       printf( NO n );       return 0;     }     if (n == 3 && a == 1 && b == 1) {       printf( NO n );       return 0;     }     printf( YES n );     if (a == 1 && b == 1)       for (long long i = 1; i < n; i++) mp[i][i + 1] = mp[i + 1][i] = true;     else       for (long long i = 0; i < n - max(a, b); i++, root++)         mp[1][root] = mp[root][1] = true;     output();   }
#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 % 1000000007) * (a % 1000000007)) % 1000000007;       b >>= 1;     }     return ans;   }   void sieve(int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p]) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }     for (int p = 2; p <= n; p++) {       if (prime[p]) cout << p <<    ;     }   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t = 1;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       vector<vector<int>> v;       for (int i = 0; i < k; i++) {         int c;         cin >> c;         vector<int> temp;         for (int j = 0; j < c; j++) {           int z;           cin >> z;           temp.push_back(z);         }         v.push_back(temp);       }       int l = 1, h = n;       int ma;       cout <<  ?             <<     << n <<    ;       for (int i = 1; i <= n; i++) cout << i <<    ;       cout << endl;       cin >> ma;       for (int i = 0; i < 10; i++) {         int mid = (h + l) / 2;         int temp;         cout <<  ?               <<     << mid - l + 1 <<    ;         for (int kha = l; kha <= mid; kha++) cout << kha <<    ;         cout << endl;         cin >> temp;         if (temp == ma)           h = mid;         else           l = mid;       }       int ind = h;       bool flag = false;       vector<int> ans;       for (int i = 0; i < v.size(); i++) {         sort(v[i].begin(), v[i].end());         for (int j = 0; j < v[i].size(); j++) {           if (v[i][j] == ind) {             flag = true;             cout <<  ?                   <<    ;             vector<int> loa;             for (int lo = 1; lo <= n; lo++) {               if (binary_search(v[i].begin(), v[i].end(), lo) == false) {                 loa.push_back(lo);               }             }             cout << loa.size() <<    ;             for (auto lo : loa) cout << lo <<    ;             cout << endl;             int kha;             cin >> kha;             ans.push_back(kha);           }         }         if (flag == false) {           ans.push_back(ma);         } else {           flag = false;         }       }       cout <<  !             <<    ;       for (auto i : ans) cout << i <<    ;       cout << endl;       string sa;       cin >> sa;       if (sa ==  Correct ) {         continue;       } else {         return 0;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const int maxn = 2e6 + 10;   int a[maxn], cnt[maxn];   long long sum[maxn];   int main(void) {     int n, x, y;     scanf( %d %d %d , &n, &x, &y);     int z = x / y;     for (int i = 1; i <= n; i++)       scanf( %d , a + i), cnt[a[i]]++, sum[a[i]] += a[i];     for (int i = 1; i <= 2000000; i++) cnt[i] += cnt[i - 1], sum[i] += sum[i - 1];     long long ans = INF;     for (int i = 2; i <= 1000000; i++) {       long long tmp = 0;       int zz = min(z, i - 1);       for (int j = 1; (j - 1) * i < 1000000; j++) {         if (i - zz - 2 >= 0)           tmp += (long long)(cnt[j * i - zz - 1] - cnt[j * i - i]) * x;         if (zz)           tmp += ((long long)i * j * (cnt[j * i - 1] - cnt[j * i - zz - 1]) -                   (sum[j * i - 1] - sum[j * i - zz - 1])) *                  y;       }       ans = min(ans, tmp);     }     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int md = 1e9 + 7;   int n;   char s[111];   int f[111][3333];   int main() {     int t;     f[0][0] = 1;     for (int i = 1; i <= 100; ++i) {       for (int j = 0; j <= 3000; ++j) {         for (int c = 0; c <= min(j, 25); ++c) {           f[i][j] += f[i - 1][j - c];           f[i][j] %= md;         }       }     }     scanf( %d , &t);     while (t--) {       scanf( %s , s);       int sum = 0;       for (int i = 0; i < strlen(s); ++i) sum += s[i] -  a ;       printf( %d n , (f[strlen(s)][sum] + md - 1) % md);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long k, b, n, t;   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cin >> k >> b >> n >> t;     long long num = 1;     long long step = n;     while (t >= num) {       num = num * k + b;       step--;     }     if (step < 0) {       cout << 0;       return 0;     }     cout << step + 1;     return 0;   }
#include <bits/stdc++.h>   const long double pi = 3.14159265359;   const long long N = 1e9 + 7;   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, m, arr[200005];     cin >> n >> m;     long long cntne = 0, cntpo = 0;     for (int i = 0; i < n; i++) {       cin >> arr[i];       if (arr[i] == -1)         cntne++;       else         cntpo++;     }     vector<long long> v;     for (int i = 0; i < m; i++) {       long long l, r;       cin >> l >> r;       r++;       long long ans = abs(l - r);       if ((ans % 2 == 0) && (cntne >= ans / 2) && (cntpo >= ans / 2))         v.push_back(1);       else         v.push_back(0);     }     for (int i = 0; i < ((long long)((v).size())); i++) cout << v[i] << endl;     return 0;   }
#include <iostream>   #include <vector>   #include <algorithm>   #include <string>      #define ll long long      using namespace std;      int main() {       ll qua;       cin >> qua;       for (int i = 0; i < qua; ++i) {           ll n;           cin >> n;           vector<int> vec(n);           for (int j = 0; j < n; ++j) {               cin >> vec[j];           }           cout << 0 <<    ;           for (int j = 1; j < n; ++j) {               ll r = (vec[j] | vec[j - 1]) - vec[j];               cout << r <<    ;               vec[j] += r;           }           cout <<   n ;       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 100005;   const int MaxM = MaxN * 2;   const int oo = 0x3F3F3F3F;   int ax[MaxM], K;   int ret[MaxN];   set<pair<int, int> > tr[MaxM];   struct event {     int l, r, v, tp;     event() {}     event(int l, int r, int v, int tp) : l(l), r(r), v(v), tp(tp) {}     bool operator<(const event& e) const {       if (r != e.r)         return r > e.r;       else         return tp < e.tp;     }   } e[MaxM];   int getId(int v) { return lower_bound(ax, ax + K, v) - ax + 1; }   void add(int k, pair<int, int> v) {     while (k <= K) {       tr[k].insert(v);       k += k & -k;     }   }   int read(int k, int v) {     pair<int, int> ret = make_pair(oo, -1);     while (k) {       set<pair<int, int> >::iterator it = tr[k].lower_bound(make_pair(v, 0));       if (it != tr[k].end()) ret = min(ret, *it);       k -= k & -k;     }     return ret.second;   }   int main() {     int N, M, en;     int l, r, t;     scanf( %d%d , &N, &M);     K = en = 0;     for (int i = 0; i < N; i++) {       scanf( %d%d%d , &l, &r, &t);       e[en++] = event(l, r, t, -i - 1);       ax[K++] = l;     }     for (int i = 0; i < M; i++) {       scanf( %d%d%d , &l, &r, &t);       e[en++] = event(l, r, t, i);       ax[K++] = l;     }     sort(ax, ax + K);     K = unique(ax, ax + K) - ax;     sort(e, e + en);     for (int i = 0; i < en; i++) {       if (e[i].tp < 0) {         add(getId(e[i].l), make_pair(e[i].v, -e[i].tp));       } else {         ret[e[i].tp] = read(getId(e[i].l), e[i].v);       }     }     for (int i = 0; i < M; i++) {       printf( %d  , ret[i]);     }     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e3 + 5;   int dw[N], up[N], E[N][2], Mn;   vector<pair<int, int> > g[N];   void dfs_down(int v, int p) {     dw[v] = 0;     for (pair<int, int> u : g[v])       if (u.first != p) dfs_down(u.first, v), dw[v] += dw[u.first] + u.second;   }   void dfs_up(int v, int p, int f = -1) {     if (f != -1) {       up[v] = up[p] + dw[p] - dw[v] + f - (f == 0);     } else       up[v] = 0;     Mn = min(Mn, dw[v] + up[v]);     for (pair<int, int> u : g[v])       if (u.first != p) dfs_up(u.first, v, u.second ^ 1);   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n;     cin >> n;     if (n == 1) return cout <<  0 n , 0;     for (int i = 0; i < n - 1; i++) {       cin >> E[i][0] >> E[i][1];       g[E[i][0]].push_back({E[i][1], 0});       g[E[i][1]].push_back({E[i][0], 1});     }     int ans = n;     for (int i = 0; i < n - 1; i++) {       Mn = n, dfs_down(E[i][0], E[i][1]), dfs_up(E[i][0], E[i][1]);       int cur = Mn;       Mn = n, dfs_down(E[i][1], E[i][0]), dfs_up(E[i][1], E[i][0]);       cur += Mn;       ans = min(ans, cur);     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int y[N], pos[N];   int main() {     int n, m, t = 0, ans = 0, cnt = 0;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &y[i]);     for (int i = 1; i <= m; i++) {       int y1, y2, x;       scanf( %d %d %d , &y1, &y2, &x);       if (y1 == 1) {         pos[++t] = y2;       }       if (y2 == 1e9) {         ans++;       }     }     sort(y + 1, y + 1 + n);     sort(pos + 1, pos + 1 + t);     int i = 1, j = 1;     cnt = t, ans += n;     while (i <= n) {       while (j <= t) {         if (pos[j] < y[i]) {           j++, cnt--;         } else           break;       }       ans = min(ans, cnt + i - 1);       i++;     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^  0 ), c = getchar();     return x * f;   }   const int N = 505, mod = 1e9 + 7;   inline int inc(int x, int y) {     x += y - mod;     return x + (x >> 31 & mod);   }   inline int mul(int x, int y) { return 1ll * x * y % mod; }   inline void Inc(int &x, int y) { x = inc(x, y); }   int n, a[N], f[N][N];   int main() {     n = read();     for (int i = 1; i <= n; i++) a[i] = read(), f[i][i] = 1;     for (int i = 1; i <= n + 1; i++) f[i][i - 1] = 1;     for (int len = 2; len < n; len++)       for (int l = 1; l <= n - len + 1; l++) {         int r = l + len - 1;         f[l][r] = f[l + 1][r];         for (int k = l + 1; k <= r; k++)           if (a[k] > a[l]) Inc(f[l][r], mul(f[l + 1][k - 1], f[k][r]));       }     printf( %d n , f[2][n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2001;   char a[MAXN][MAXN];   int n, v[MAXN], r[MAXN], c[MAXN];   int gao() {     int ret = 0;     memset(r, 0, sizeof(int) * n);     memset(c, 0, sizeof(int) * n);     for (int i = 0; i < n - 1; ++i) {       for (int j = n - 1; j > i; --j) {         if (a[i][j] ^ r[i] ^ c[j]) {           ++ret;           r[i] ^= 1;           c[j] ^= 1;         }       }     }     for (int i = 0; i < n; ++i) v[i] ^= (r[i] ^ c[i]);     memset(r, 0, sizeof(int) * n);     memset(c, 0, sizeof(int) * n);     for (int i = n - 1; i > 0; --i) {       for (int j = 0; j < i; ++j) {         if (a[i][j] ^ r[i] ^ c[j]) {           ++ret;           r[i] ^= 1;           c[j] ^= 1;         }       }     }     for (int i = 0; i < n; ++i) v[i] ^= (r[i] ^ c[i]);     for (int i = 0; i < n; ++i) ret += v[i];     return ret;   }   int main() {     while (scanf( %d , &n) != EOF) {       memset(v, 0, sizeof(int) * n);       for (int i = 0; i < n; ++i) {         scanf( %s , a[i]);         for (int j = 0; j < n; ++j) a[i][j] -=  0 ;         v[i] ^= (int)a[i][i];       }       printf( %d n , gao());     }     return 0;   }
#include<bits/stdc++.h>   using namespace std;   typedef double db;   typedef uint32_t u32;   typedef uint64_t u64;   typedef pair<int, int> pii;   typedef pair<int, int> point;   #define fi first   #define se second   #define pb(x) push_back(x)   #define SZ(x) ((int) (x).size())   #define all(x) (x).begin(), (x).end()   #define rep(i, l, r) for(int i = l; i < r; i++)   typedef long long ll;   const int mod = 998244353;            int main() {   #ifdef local    freopen( in.txt ,  r , stdin);   #endif    ios::sync_with_stdio(false);    cin.tie(0), cout.tie(0);    const int mod = 1e9 + 7;    int n;    cin >> n;    vector<int> a(n);    ll tot = 0;    for(auto &e : a) {     cin >> e;     tot += e;    }    if(tot % n) {     cout <<  0 n ;     return 0;    }    vector<int> fac(n + 1, 1);    vector<int> ifac(n + 1, 1);    for(int i = 2; i <= n; i++) {     fac[i] = (ll) fac[i - 1] * i % mod;     ifac[i] = (ll) ifac[mod % i] * (mod - mod / i) % mod;    }    for(int i = 2; i <= n; i++) {     ifac[i] = (ll) ifac[i] * ifac[i - 1] % mod;    }    auto C = [&](int n, int m) {     if(n < m || m < 0) return 0ll;     return (ll) fac[n] * ifac[m] % mod * ifac[n - m] % mod;    };    ll avg = tot / n;    vector<int> l, r;    int eq = 0;    for(auto &e : a) {     if(e > avg) {      r.push_back(e);     } else if(e < avg) {      l.push_back(e);     } else {      eq++;     }    }    int ans = 1;    if(l.size() > 1 && r.size() > 1) {     ans = 2;    }    if(l.size() == 1) {     r.push_back(l[0]);     l.clear();    }    if(r.size() == 1) {     l.push_back(r[0]);     r.clear();    }    sort(all(l));    sort(all(r));    int a1 = fac[l.size()];    int a2 = fac[r.size()];    for(int i = 0, j = 0; i < (int) l.size(); i = j) {     while(j < (int) l.size() && l[j] == l[i]) {      j++;     }     a1 = (ll) a1 * ifac[j - i] % mod;    }    for(int i = 0, j = 0; i < (int) r.size(); i = j) {     while(j < (int) r.size() && r[j] == r[i]) {      j++;     }     a2 = (ll) a2 * ifac[j - i] % mod;    }    int a3 = C(n, eq);    ans = (ll) ans * a1 % mod;    ans = (ll) ans * a2 % mod;    ans = (ll) ans * a3 % mod;    cout << ans <<   n ;    return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n;     cin >> n;     set<int> a, b;     vector<pair<int, int> > ans(n);     for (int i = 0; i < n; ++i) {       cin >> ans[i].first >> ans[i].second;       --ans[i].first;       --ans[i].second;       if (ans[i].first == -1) a.insert(i);       if (ans[i].second == -1) b.insert(i);     }     while (a.size() > 1) {       int node = *a.begin();       a.erase(a.begin());       int last = node;       while (ans[last].second != -1) last = ans[last].second;       auto nxt = b.begin();       if (*nxt == last) ++nxt;       ans[*nxt].second = node;       ans[node].first = *nxt;       b.erase(nxt);     }     for (int i = 0; i < n; ++i)       cout << ans[i].first + 1 <<     << ans[i].second + 1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n], sm = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       sm += a[i];     }     vector<int> ind, r;     int need = sm / 2;     if (a[0] > need) {       cout << 1 << endl;       cout << 1;       return 0;     }     for (int i = 1; i < n; i++) {       if ((2 * a[i]) <= a[0]) {         ind.push_back(i + 1);         r.push_back(a[i]);       }     }     bool ok;     int u = a[0];     for (auto x : r) {       u += x;     }     if (u > need) {       ok = true;     } else {       ok = false;     }     if (ok) {       cout << ind.size() + 1 << endl;       cout << 1 <<    ;       for (auto x : ind) {         cout << x <<    ;       }     } else {       cout << 0;     }   }
#include <bits/stdc++.h>   using namespace std;   int nbVoyages, coutSimple, coutLiaison, maxCartes, coutCarte;   map<pair<string, string>, int> routes;   int main() {     ios_base::sync_with_stdio(false);     cin >> nbVoyages >> coutSimple >> coutLiaison >> maxCartes >> coutCarte;     long long total = 0;     string derFin =   ;     for (int iVoyage = 0; iVoyage < nbVoyages; iVoyage++) {       string a, b;       cin >> a >> b;       string der = b, pre = a;       if (b < a) swap(a, b);       if (routes.find({a, b}) == routes.end()) routes[{a, b}] = 0;       if (derFin == pre) {         routes[{a, b}] += coutLiaison;         total += coutLiaison;       } else {         routes[{a, b}] += coutSimple;         total += coutSimple;       }       derFin = der;     }     vector<int> couts;     for (pair<pair<string, string>, int> route : routes) {       couts.push_back(route.second);     }     sort(couts.begin(), couts.end());     for (int iFois = 0; iFois < maxCartes; iFois++) {       if (!couts.empty() && couts.back() > coutCarte) {         total += coutCarte - couts.back();         couts.pop_back();       }     }     cout << total << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const double eps = 1e-6;   const int mod = 1000000007;   int modpow(int x, int y, int md) {     if (y == 0) return 1;     int ret = modpow(x, y >> 1, md);     ret = (long long)ret * ret % md;     if (y & 1) ret = (long long)ret * x % md;     return ret;   }   int n;   int sum[5555];   vector<int> g[5555];   set<pair<int, int> > ans;   void dfs(int x, int par = 0) {     sum[x] = 1;     for (auto to : g[x])       if (to != par) dfs(to, x);     for (auto to : g[x])       if (to != par) sum[x] += sum[to];     vector<bool> ok(5555, false);     ok[0] = true;     for (auto to : g[x])       if (to != par) {         for (int i = 5005; i >= 0; i--)           if (ok[i] && i + sum[to] <= 5005) ok[i + sum[to]] = true;       }     for (int i = 1; i <= 5005; i++)       if (ok[i] && n - i - 1 >= 1)         ans.insert(make_pair(i, n - i - 1)), ans.insert(make_pair(n - i - 1, i));   }   int main() {     cin >> n;     for (int i = 1; i < n; i++) {       int a, b;       cin >> a >> b;       g[a].push_back(b);       g[b].push_back(a);     }     dfs(1);     cout << ans.size() << endl;     for (auto p : ans) cout << p.first <<     << p.second << endl;     return 0;   }