malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int get() {     int f = 0, v = 0;     char ch;     while (!isdigit(ch = getchar()))       if (ch ==  - ) break;     if (ch ==  - )       f = 1;     else       v = ch -  0 ;     while (isdigit(ch = getchar())) v = v * 10 + ch -  0 ;     if (f)       return -v;     else       return v;   }   const int maxn = 100005, inf = 314000000;   vector<int> rule[maxn], g[maxn], belong[maxn];   struct data {     int t, v;     data() {}     data(int _t, int _v) { t = _t, v = _v; }     inline bool operator<(const data &b) const { return v > b.v; }   };   priority_queue<data> Q;   int T[maxn], cnt[maxn], n, m, a[maxn], b[maxn], val[maxn], vis[maxn];   void init() {     m = get(), n = get();     memset(a, 60, sizeof(a));     for (int i = 1; i <= m; i++) {       int x = get(), y = get(), t;       g[x].push_back(i), rule[i].push_back(x);       while (y--)         if ((t = get()) < 0)           val[i]++;         else           rule[i].push_back(t), belong[t].push_back(i), T[i]++;       if (!T[i]) a[x] = min(a[x], val[i]);       cnt[i] = val[i];     }   }   int dfs(int x) {     if (vis[x]) return b[x];     vis[x] = 1;     for (int i = 0; i < g[x].size(); i++) {       int id = g[x][i], flag = 1, tot = cnt[id];       for (int j = 1; j < rule[id].size() && flag; j++)         if (a[rule[id][j]] > inf) flag = 0;       if (!flag) continue;       flag = 0;       for (int j = 1; j < rule[id].size(); j++) {         int p = rule[id][j], v;         if (vis[p] == 1) {           flag = 1;           break;         }         v = dfs(p);         if (v == -2) {           flag = 1;           break;         }         tot = min(tot + v, inf);       }       if (flag) {         b[x] = -2;         break;       }       b[x] = max(b[x], tot);     }     vis[x] = 2;     return b[x];   }   void work() {     for (int i = 1; i <= n; i++)       if (a[i] < 1e9) Q.push(data(i, a[i]));     while (!Q.empty()) {       data tp = Q.top();       Q.pop();       int t = tp.t, v = tp.v;       if (a[t] != v) continue;       for (int i = 0; i < belong[t].size(); i++) {         int p = belong[t][i], s = rule[p][0];         val[p] = min(val[p] + v, inf);         T[p]--;         if (!T[p] && a[s] > val[p]) Q.push(data(s, a[s] = val[p]));       }     }     for (int i = 1; i <= n; i++)       if (a[i] <= inf) dfs(i);     for (int i = 1; i <= n; i++)       if (a[i] > inf)         puts( -1 -1 );       else         printf( %d %d n , a[i], b[i]);   }   int main() {     init();     work();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int Set(int N, int pos) { return N |= (1LL << pos); }   int Reset(int N, int pos) { return N &= ~(1LL << pos); }   bool Check(int N, int pos) { return N & (1LL << pos); }   int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1};   inline void cn(long &n) {     n = 0;     long ch = getchar();     int sign = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) sign = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       n = (n << 3) + (n << 1) + ch -  0 , ch = getchar();     n = n * sign;   }   template <class T>   void cmin(T &a, T b) {     if (b < a) a = b;   }   template <class T>   void cmax(T &a, T b) {     if (b > a) a = b;   }   template <class T>   int len(const T &c) {     return (int)c.size();   }   template <class T>   int len(char c[]) {     return (int)strlen(c);   }   string itos(long n) {     string s;     while (n) {       s += (n % 10 + 48);       n /= 10;     }     reverse(s.begin(), s.end());     return s;   }   long stoi(string s) {     long n = 0;     for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48);     return n;   }   int ar[100005][3];   int main() {     string s, second;     int q, l, r, x, y, z, m;     cin >> s;     for (int i(0), _n(len(s)); i < _n; ++i) {       ar[i + 1][0] = ar[i][0];       ar[i + 1][1] = ar[i][1];       ar[i + 1][2] = ar[i][2];       ar[i + 1][s[i] -  x ]++;     }     cin >> q;     for (int i(0), _n(q); i < _n; ++i) {       cin >> l >> r;       x = ar[r][0] - ar[l - 1][0];       y = ar[r][1] - ar[l - 1][1];       z = ar[r][2] - ar[l - 1][2];       int sr[3];       sr[0] = x;       sr[1] = y;       sr[2] = z;       sort(sr, sr + 3);       if (sr[2] - sr[0] <= 1 || (r - l) < 2)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int mof = 1000000007;   const int inf = 1000000010;   const long long llinf = 1000000007000000007;   const double pi = 3.14159265359;   int a[211111];   int a2[211111];   int t[211111];   int t2[211111];   int n, k, A, B, q;   int sum(int r) {     int result = 0;     for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r];     return result;   }   void inc(int i, int delta) {     if (a[i] + delta > B) {       delta = B - a[i];       a[i] = B;     } else {       a[i] = a[i] + delta;     }     for (; i < n; i = (i | (i + 1))) t[i] += delta;   }   int sum(int l, int r) { return sum(r) - sum(l - 1); }   void init(int a[]) {     for (unsigned i = 0; i < n; i++) inc(i, a[i]);   }   int sum2(int r) {     int result = 0;     for (; r >= 0; r = (r & (r + 1)) - 1) result += t2[r];     return result;   }   void inc2(int i, int delta) {     if (a2[i] + delta > A) {       delta = A - a2[i];       a2[i] = A;     } else {       a2[i] = a2[i] + delta;     }     for (; i < n; i = (i | (i + 1))) t2[i] += delta;   }   int sum2(int l, int r) { return sum2(r) - sum2(l - 1); }   void init2(int a[]) {     for (unsigned i = 0; i < n; i++) inc2(i, a[i]);   }   int main() {     scanf( %d%d%d%d%d , &n, &k, &A, &B, &q);     while (q--) {       int type;       scanf( %d , &type);       if (type == 1) {         int d, a;         scanf( %d%d , &d, &a);         inc(d - 1, a);         inc2(d - 1, a);       } else {         int p;         scanf( %d , &p);         printf( %d n , sum(0, p - 2) + sum2(p + k - 1, n - 1));       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 210;   const double eps = 1e-9;   int n;   int lsh[maxn * 2], ln, K;   map<int, int> mt;   int To[maxn];   int li[maxn], ri[maxn];   double f[2][maxn][maxn];   double ans[maxn][maxn];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++)       scanf( %d%d , &li[i], &ri[i]), lsh[++ln] = li[i], lsh[++ln] = ri[i];     sort(lsh + 1, lsh + ln + 1);     lsh[0] = lsh[1] - 1;     K = 0;     for (int i = 1; i <= ln; i++)       if (lsh[i] != lsh[i - 1]) To[mt[lsh[i]] = ++K] = lsh[i];     for (int i = 1; i <= n; i++) li[i] = mt[li[i]], ri[i] = mt[ri[i]];     for (int i = 1; i <= n; i++) {       for (int x = li[i]; x < ri[i]; x++) {         double inow = (double)(To[x + 1] - To[x]) / (To[ri[i]] - To[li[i]]);         for (int x1 = 0; x1 <= n; x1++)           for (int x2 = 0; x2 <= n; x2++) f[0][x1][x2] = f[1][x1][x2] = 0;         int now = 1;         f[now][0][0] = 1.0;         for (int j = 1; j <= n; j++) {           if (i == j) continue;           int L = To[li[j]], R = To[ri[j]];           now = !now;           if (x < li[j]) {             for (int x1 = 0; x1 < j; x1++)               for (int x2 = 0; x2 < j - x1; x2++)                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1][x2] += tmp;                 }           } else if (ri[j] <= x) {             for (int x1 = 0; x1 < j; x1++)               for (int x2 = 0; x2 < j - x1; x2++)                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1 + 1][x2] += tmp;                 }           } else {             double p1 = (double)(To[x] - L) / (R - L),                    p2 = (double)(To[x + 1] - To[x]) / (R - L),                    p3 = (double)(R - To[x + 1]) / (R - L);             for (int x1 = 0; x1 < j; x1++)               for (int x2 = 0; x2 < j - x1; x2++)                 if (f[!now][x1][x2] > eps) {                   double tmp = f[!now][x1][x2];                   f[!now][x1][x2] = 0;                   f[now][x1 + 1][x2] += tmp * p1;                   f[now][x1][x2 + 1] += tmp * p2;                   f[now][x1][x2] += tmp * p3;                 }           }         }         for (int x1 = 0; x1 < n; x1++)           for (int x2 = 0; x2 < n - x1; x2++)             if (f[now][x1][x2] > eps) {               double tmp = f[now][x1][x2], temp = 1.0 / (x2 + 1.0);               for (int l = 0; l <= x2; l++)                 ans[i][x1 + l + 1] += inow * tmp * temp;             }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j < n; j++) printf( %.10lf  , ans[i][j]);       printf( %.10lf n , ans[i][n]);     }     return 0;   }
#include <bits/stdc++.h>   int ans[1000010], p[1000010], mem[1000010], q[1000010];   int main() {     int n, t, i, j;     memset(q, 0, sizeof(q));     scanf( %d , &n);     for (i = 0; i < n; i++) scanf( %d , &p[i]);     scanf( %d , &t);     for (i = 0; i < t; i++) {       scanf( %d , &j);       q[j - 1] = 1;     }     int cnt = 0, num = n - 1;     if (n % 2 == 1) {       printf( NO n );       return 0;     }     for (i = n - 1; i >= 0; i--) {       if (!cnt || q[i] == 1 || mem[cnt] != p[i]) {         mem[++cnt] = p[i];         ans[i] = -p[i];       } else {         ans[i] = p[i];         cnt--;       }     }     if (cnt != 0)       printf( NO n );     else {       printf( YES n );       for (i = 0; i < n - 1; i++) printf( %d  , ans[i]);       printf( %d n , ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t, n, k;   char s[4000];   int opx[4000];   int opy[4000];   int count2;   void Reverse(char *s, int a, int b) {     for (int i = a, j = b; i < j; i++, j--) {       char c = s[i];       s[i] = s[j];       s[j] = c;     }   }   int main() {     cin >> t;     while (t--) {       cin >> n >> k;       cin >> s;       int count1 = 0;       while (count1 < 2 * k - 3) {         if (s[count1] ==  ( ) {           for (int i = count1 + 1; i < n; i++) {             if (s[i] ==  ) ) {               count2++;               opx[count2] = count1 + 2;               opy[count2] = i + 1;               Reverse(s, count1 + 1, i);               break;             }           }         } else {           for (int i = count1 + 1; i < n; i++) {             if (s[i] ==  ( ) {               count2++;               opx[count2] = count1 + 1;               opy[count2] = i + 1;               Reverse(s, count1, i);               break;             }           }         }         count1 += 2;       }       while (count1 < n && s[count1] ==  ( ) count1++;       while (count1 < n) {         int count3 = count1;         while (count3 < n && s[count3] ==  ) ) count3++;         if (count3 < n) {           count2++;           opx[count2] = count1 + 1;           opy[count2] = count3 + 1;           Reverse(s, count1, count3);           count1++;         } else {           break;         }       }       cout << count2 << endl;       for (int i = 1; i <= count2; i++) {         cout << opx[i] <<     << opy[i] << endl;       }       count2 = 0;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5004;   int main() {     int a = 1, b = 1, n;     string s;     cin >> n;     while (n--) {       cin >> s;       if (s ==  UL  || s ==  DR )         ++a;       else if (s.size() == 2)         ++b;       else         ++a, ++b;     }     printf( %lld n , 1ll * a * b);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int k = 8;     int t = 4;     int p = 2;     int s = 6;     int n;     while (cin >> n) {       if (n == 0)         cout << 1 << endl;       else if (n % 4 == 1)         cout << k << endl;       else if (n % 4 == 2)         cout << t << endl;       else if (n % 4 == 3)         cout << p << endl;       else if (n % 4 == 0)         cout << s << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int NMAX = (int)(1e5) + 10;   int n, m, a, b;   vector<pair<int, pair<int, int> > > ed;   pair<int, int> res[NMAX];   int last;   int now[NMAX];   bool func(pair<int, pair<int, int> > i, pair<int, pair<int, int> > j) {     if (i.first == j.first)       return (i.second.first > j.second.first);     else       return (i.first < j.first);   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d %d , &a, &b);       ed.push_back(make_pair(a, make_pair(b, i)));     }     sort(ed.begin(), ed.end(), func);     int i = 1;     last = 1;     for (int kk = 0; kk <= (int)ed.size() - 1; kk++) {       int val = ed[kk].first;       int type = ed[kk].second.first;       int vt = ed[kk].second.second;       if (type == 1) {         res[vt] = make_pair(last, 1 + last);         last++;         now[last] = last;       } else {         bool co = 0;         while (!co) {           for (int j = min(i - 2, now[i] - 1); j >= 1; j--) {             res[vt] = make_pair(j, i);             co = 1;             now[i] = j;             break;           }           if (!co) i++;           if (i > last) {             cout << -1;             return 0;           }         }       }     }     if (last != n) {       cout << -1;       return 0;     }     for (int i = 1; i <= m; i++)       cout << res[i].first <<     << res[i].second << endl;   }
#include <bits/stdc++.h>   using namespace std;   bool poate[5020][510];   pair<int, int> from[5020][510];   int last[5020][510];   queue<pair<int, int>> de_calc;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int d, s;     cin >> d >> s;     for (int i = 1; i <= 9; i++) {       poate[i][i % d] = 1;       from[i][i % d] = {-1, -1};       last[i][i % d] = i;       de_calc.push({i, i % d});     }     bool found = 0;     while (!de_calc.empty() && !found) {       int i = de_calc.front().first, j = de_calc.front().second;       de_calc.pop();       if (i > s) continue;       for (int cif = 0; cif <= 9; cif++) {         int j2 = (10 * j + cif) % d, i2 = i + cif;         if (poate[i2][j2]) continue;         poate[i2][j2] = 1;         from[i2][j2] = {i, j};         last[i2][j2] = cif;         de_calc.push({i2, j2});         if (i2 == s && j2 == 0) {           found = 1;           break;         }       }     }     if (!poate[s][0]) {       cout <<  -1 n ;       return 0;     }     string ans =   ;     for (int i = s, j = 0;;) {       ans += last[i][j] +  0 ;       if (from[i][j].first == -1) break;       int j2 = from[i][j].second;       i = from[i][j].first;       j = j2;     }     reverse(ans.begin(), ans.end());     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 10;   signed main() {     ios::sync_with_stdio(false);     long long n, a, b;     cin >> n >> a >> b;     long long ans = 0;     vector<long long> x(n), y(n);     for (long long i = 0; i < n; i++) {       cin >> x[i] >> y[i];       for (long long j = 0; j < i; j++) {         for (long long _ = 2; _--;) {           for (long long __ = 2; __--;) {             for (long long ___ = 2; ___--;) {               if (x[i] + x[j] <= a && max(y[i], y[j]) <= b) {                 ans = max(ans, x[i] * y[i] + x[j] * y[j]);               }               swap(x[j], y[j]);             }             swap(x[i], y[i]);           }           swap(a, b);         }       }     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     if (n & (n - 1)) {       cout <<  NO  <<   n ;     } else {       cout <<  YES  <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int ttt;     cin >> ttt;     while (ttt--) {       int n, m;       cin >> n >> m;       vector<pair<int, int>> v(n * m);       for (int i = 0; i < n * m; i++) {         cin >> v[i].first;         v[i].second = i;       }       sort((v).begin(), (v).end());       for (int i = 0; i < n * m; i++) {         v[i].second *= -1;       }       int ans = 0;       for (int i = 0; i < n; i++) {         sort(v.begin() + i * m, v.begin() + (i + 1) * (m));         for (int l = 0; l < m; l++) {           for (int j = 0; j < l; j++) {             if (v[i * m + l].second < v[i * m + j].second) ans++;           }         }       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int read() {     int x = 0;     bool flg = false;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) flg = true;     for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);     return flg ? -x : x;   }   int n, m;   char a[64][64];   void solve() {     n = read(), m = read();     for (int i(1), _i(n); i <= _i; i++) scanf( %s , a[i] + 1);     int tot = 0;     for (int i(1), _i(n); i <= _i; i++)       for (int j(1), _j(m); j <= _j; j++) tot += a[i][j] ==  A ;     if (!tot) return puts( MORTAL ), void();     if (tot == n * m) return puts( 0 ), void();     {       bool g = 1;       for (int i(1), _i(n); i <= _i; i++) g = g && a[i][1] ==  A ;       if (g) return puts( 1 ), void();       g = 1;       for (int i(1), _i(n); i <= _i; i++) g = g && a[i][m] ==  A ;       if (g) return puts( 1 ), void();       g = 1;       for (int i(1), _i(m); i <= _i; i++) g = g && a[1][i] ==  A ;       if (g) return puts( 1 ), void();       g = 1;       for (int i(1), _i(m); i <= _i; i++) g = g && a[n][i] ==  A ;       if (g) return puts( 1 ), void();     }     {       for (int x : {1, n})         for (int y : {1, m})           if (a[x][y] ==  A ) return puts( 2 ), void();       for (int i(1), _i(n); i <= _i; i++) {         bool g = 1;         for (int j(1), _j(m); j <= _j; j++) g = g && a[i][j] ==  A ;         if (g) return puts( 2 ), void();       }       for (int j(1), _j(m); j <= _j; j++) {         bool g = 1;         for (int i(1), _i(n); i <= _i; i++) g = g && a[i][j] ==  A ;         if (g) return puts( 2 ), void();       }     }     {       for (int i(1), _i(n); i <= _i; i++)         for (int j(1), _j(m); j <= _j; j++) {           if (a[i][j] ==  A  && (i == 1 || i == n || j == 1 || j == m))             return puts( 3 ), void();         }     }     puts( 4 );   }   int main() {     for (int T = read(); T--;) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct per {     string name;     int score;   } store[55];   bool cmp(per a, per b) { return a.score > b.score; }   int main() {     int n, tmp;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> store[i].name;       cin >> tmp;       store[i].score = 0;       store[i].score += tmp * 100;       cin >> tmp;       store[i].score -= tmp * 50;       for (int j = 0; j < 5; j++) {         cin >> tmp;         store[i].score += tmp;       }     }     sort(store, store + n, cmp);     cout << store[0].name << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   int f[50005][65], a[50005], po[50005], cnt, tr[65][200005], la[65][200005];   void pushup(int num, int id) {     tr[num][id] = max(tr[num][id << 1], tr[num][id << 1 | 1]);   }   void pushdo(int num, int id) {     if (la[num][id] != 0) {       la[num][id << 1] += la[num][id];       la[num][id << 1 | 1] += la[num][id];       tr[num][id << 1] += la[num][id];       tr[num][id << 1 | 1] += la[num][id];       la[num][id] = 0;     }   }   void upda(int num, int id, int l, int r, int x, int y, int p) {     if (x <= l && r <= y) {       tr[num][id] += p;       la[num][id] += p;       return;     }     pushdo(num, id);     int m = (l + r) >> 1;     if (x <= m) upda(num, id << 1, l, m, x, y, p);     if (y > m) upda(num, id << 1 | 1, m + 1, r, x, y, p);     pushup(num, id);   }   int query(int num, int id, int l, int r, int x, int y) {     if (x <= l && r <= y) return tr[num][id];     pushdo(num, id);     int m = (l + r) >> 1, ans = 0;     if (x <= m) ans = max(ans, query(num, id << 1, l, m, x, y));     if (y > m) ans = max(ans, query(num, id << 1 | 1, m + 1, r, x, y));     return ans;   }   inline int min(int x, int y) {     if (x < y) return x;     return y;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     for (int i = 1; i <= k; i++) upda(0, 1, 0, n, i, i, -2e9);     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= min(i, k); j++) {         upda(j, 1, 0, n, po[a[i]], i - 1, 1);       }       po[a[i]] = i;       for (int j = 1; j <= min(i, k); j++) {         f[i][j] = query(j - 1, 1, 0, n, 0, i - 1);         upda(j, 1, 0, n, i, i, f[i][j]);       }     }     printf( %d n , f[n][k]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n], b[n], o = 0, z = 0, sorted = 1;       for (int i = 0; i < n; i++) {         cin >> a[i];         if (i > 0 && a[i] < a[i - 1]) sorted = 0;       }       for (int i = 0; i < n; i++) {         cin >> b[i];         if (b[i])           o++;         else           z++;       }       if ((z && o) || (sorted))         cout <<  Yes  << endl;       else         cout <<  No  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   void dfs(const vector<vector<int> >& edge_list, vector<bool>& visited, int i,            int& edges) {     visited[i] = true;     for (int j : edge_list[i]) {       if (!visited[j]) dfs(edge_list, visited, j, edges);       if (j >= i) ++edges;     }   }   int main() {     int n, m, u, v;     scanf( %d %d , &n, &m);     vector<int> deg_noloop(n, 0);     vector<vector<int> > edge_list(n);     vector<bool> visited(n, false);     long long nonloops = 0, loops = 0, good_paths = 0;     int start = 0, edges = 0;     for (int i = 0; i < m; i++) {       scanf( %d %d , &u, &v);       --u;       --v;       start = u;       if (u != v) {         edge_list[u].push_back(v);         edge_list[v].push_back(u);         ++deg_noloop[u];         ++deg_noloop[v];         ++nonloops;       } else {         edge_list[u].push_back(v);         ++loops;       }     }     dfs(edge_list, visited, start, edges);     for (int i = 0; i < n; i++) {       long long deg = deg_noloop[i];       if (deg >= 2) good_paths += (deg) * (deg - 1) / 2;     }     good_paths += loops * nonloops + (loops) * (loops - 1) / 2;     if (edges < m) good_paths = 0;     printf( %lld n , good_paths);   }
#include <bits/stdc++.h>   int main() {     int q;     std::cin >> q;     std::vector<int> V;     for (int i = 0; i < q; ++i) {       int n;       std::cin >> n;       std::vector<int> v1;       std::vector<int> v2(n);       for (int j = 0; j < n; ++j) {         std::cin >> v2[j];         v1.push_back(j + 1);       }       for (int k = 0; k < v1.size(); ++k) {         int count = 1;         int num = k;         while (v1[k] != v2[num]) {           num = v2[num] - 1;           ++count;         }         V.push_back(count);       }       V.push_back(0);     }     for (int i = 0; i < V.size(); ++i) {       if (V[i] == 0)         std::cout <<   n ;       else {         std::cout << V[i] <<    ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5001;   int n, tmp[N];   vector<int> g[N];   bool vst[N];   void dfs(int c, int p) {     bool dp[N];     fill(dp, dp + n + 1, false);     dp[0] = true;     tmp[c] = 1;     for (int ch : g[c]) {       if (ch != p) {         dfs(ch, c);         tmp[c] += tmp[ch];         for (int i = n - 1; i >= 0; --i) {           if (dp[i]) {             dp[i + tmp[ch]] = true;           }         }       }     }     for (int i = 1; i <= n; ++i) {       dp[n - i - 1] |= dp[i];     }     for (int i = 1; i <= n; ++i) {       vst[i] |= dp[i];     }   }   int main() {     scanf( %d , &n);     for (int i = (1), _i = (n - 1); i <= _i; ++i) {       int u, v;       scanf( %d %d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, 1);     int cnt = 0;     for (int i = (1), _i = (n - 1 - 1); i <= _i; ++i) {       if (vst[i]) {         cnt += 1;       }     }     printf( %d n , cnt);     for (int i = (1), _i = (n - 1 - 1); i <= _i; ++i) {       if (vst[i]) {         printf( %d %d n , i, n - i - 1);       }     }   }