malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   set<pair<int, int> > topo[2];   vector<int> G[maxn], R[maxn];   int n, m, code[maxn], in[maxn], out[maxn];   bool seen[maxn];   void dfs(int node) {     seen[node] = 1;     for (int son : G[node])       if (!seen[son]) dfs(son);     topo[code[node]].insert({out[node], node});   }   int process() {     int answer = 0;     while (!topo[1].empty()) {       answer++;       for (int i = 0; i < 2; i++) {         while (!topo[i].empty() && topo[i].begin()->first == 0) {           auto aux = *topo[i].begin();           topo[i].erase(topo[i].begin());           for (int son : R[aux.second]) {             topo[code[son]].erase({out[son], son});             out[son]--;             topo[code[son]].insert({out[son], son});           }         }       }     }     return answer;   }   int main() {     ios::sync_with_stdio(false);     int x, y;     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> code[i];     for (int i = 1; i <= m; i++) {       cin >> x >> y;       x++;       y++;       G[x].push_back(y);       R[y].push_back(x);       in[y]++;       out[x]++;     }     for (int i = 1; i <= n; i++)       if (in[i] == 0) dfs(i);     cout << process();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int a, b, c, n;     cin >> a >> b >> c >> n;     if ((min(min(a, b), c) >= n) || (n - (a + b - c) < 1) || (min(a, b) < c))       return cout << -1, 0;     cout << n - (a + b - c);   }
#include <bits/stdc++.h>   int main() {     int n, w, cnt1, cnt2;     scanf( %d , &n);     cnt1 = cnt2 = 0;     while (n-- > 0) {       scanf( %d , &w);       if (w == 100)         cnt1++;       else         cnt2++;     }     printf( %s n , cnt1 % 2 == 0 && (cnt2 % 2 == 0 || cnt1 >= 2) ?  YES  :  NO );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 20005;   const int INF = -1;   int n, m, p;   int a[N];   int b[N];   int F[2][N];   int Ra[N];   int Rb[N];   string Path;   inline void checkmax(int &t, const int x) {     if (x > t) t = x;   }   inline int w(int i, int j) {     int ret = a[i] + b[j];     if (ret >= p) ret -= p;     return ret;   }   long long Gao(int sx, int sy, int ex, int ey) {     if (sx == ex && sy == ey) return w(sx, sy);     int step = ex - sx + ey - sy;     if (step == 1) {       if (sx < ex)         Path +=  C ;       else         Path +=  S ;       return w(sx, sy) + w(ex, ey);     }     int s1 = step / 2;     int s2 = step - s1;     for (int j = 0; j + sy <= ey; j++) F[0][j] = F[1][j] = INF;     for (int j = sy; j <= ey; j++) Ra[j] = Rb[j] = INF;     F[0][0] = w(sx, sy);     for (int i = 1; i <= s1; i++) {       int p = i & 1;       for (int j = max(0, i + sx - ex); j <= i && j <= ey - sy; j++) {         F[p][j] = INF;         int nx = i - j + sx;         int ny = sy + j;         if (F[p ^ 1][j] != INF) checkmax(F[p][j], F[p ^ 1][j] + w(nx, ny));         if (j && F[p ^ 1][j - 1] != INF)           checkmax(F[p][j], F[p ^ 1][j - 1] + w(nx, ny));         if (i == s1) Ra[ny] = F[p][j];       }     }     for (int j = 0; j + sy <= ey; j++) F[0][j] = F[1][j] = INF;     F[0][0] = w(ex, ey);     for (int i = 1; i <= s2; i++) {       int p = i & 1;       for (int j = max(0, sx + i - ex); j <= i && j <= ey - sy; j++) {         F[p][j] = INF;         int nx = ex - (i - j);         int ny = ey - j;         if (F[p ^ 1][j] != INF) checkmax(F[p][j], F[p ^ 1][j] + w(nx, ny));         if (j && F[p ^ 1][j - 1] != INF)           checkmax(F[p][j], F[p ^ 1][j - 1] + w(nx, ny));         if (i == s2) Rb[ny] = F[p][j] - w(nx, ny);       }     }     int bx = -1;     int by = -1;     int ret = -1;     for (int j = sy; j <= ey; j++) {       if (Ra[j] != INF && Rb[j] != INF && Ra[j] + Rb[j] > ret) {         bx = sx + s1 - (j - sy);         by = j;         ret = Ra[j] + Rb[j];       }     }     Gao(sx, sy, bx, by);     Gao(bx, by, ex, ey);     return ret;   }   int main() {     scanf( %d%d%d , &n, &m, &p);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       a[i] %= p;     }     for (int i = 0; i < m; i++) {       scanf( %d , &b[i]);       b[i] %= p;     }     cout << Gao(0, 0, n - 1, m - 1) << endl;     cout << Path << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int INFint = 2e9;   const long long INF = 1e18;   const long long MOD = 1000000007ll;   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<long long> a(n), b;     for (auto &x : a) cin >> x;     b = a;     sort(b.begin(), b.end());     b.resize(unique(b.begin(), b.end()) - b.begin());     vector<long long> dp[2];     dp[0].resize(b.size(), INF);     dp[1].resize(b.size(), INF);     dp[0][0] = abs(a[0] - b[0]);     for (int i = 1; i < b.size(); i++) {       dp[0][i] = min(dp[0][i - 1], abs(b[i] - a[0]));     }     int e = 1;     for (int i = 1; i < n; i++) {       dp[e][0] = dp[e ^ 1][0] + abs(a[i] - b[0]);       for (int j = 1; j < b.size(); j++) {         dp[e][j] = min(dp[e][j - 1], dp[e ^ 1][j] + abs(a[i] - b[j]));       }       e ^= 1;     }     cout << dp[e ^ 1][b.size() - 1];     fprintf(stderr,   nTIME = %lf n , 1.0 * clock() / CLOCKS_PER_SEC);     ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 10, INF = 1e9;   int l[N], r[N], ps[N];   map<int, int> mp;   int cnt, seg[4 * N], lazy[4 * N];   inline void shift(int id) {     seg[2 * id] += lazy[id];     lazy[2 * id] += lazy[id];     seg[2 * id + 1] += lazy[id];     lazy[2 * id + 1] += lazy[id];     lazy[id] = 0;     return;   }   void add(int id, int L, int R, int s = 0, int e = cnt) {     if (L <= s && e <= R) {       seg[id]++;       lazy[id]++;       return;     }     if (L >= e || s >= R) return;     int mid = (s + e) / 2;     shift(id);     add(2 * id, L, R, s, mid);     add(2 * id + 1, L, R, mid, e);     seg[id] = min(seg[2 * id], seg[2 * id + 1]);     return;   }   int get(int id, int L, int R, int s = 0, int e = cnt) {     if (L <= s && e <= R) return seg[id];     if (L >= e || s >= R) return INF;     int mid = (s + e) / 2;     shift(id);     return min(get(2 * id, L, R, s, mid), get(2 * id + 1, L, R, mid, e));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> l[i] >> r[i];       r[i]++;       mp[l[i]] = mp[r[i]] = 0;     }     for (auto X : mp) mp[X.first] = cnt++;     for (int i = 0; i < n; i++) {       l[i] = mp[l[i]];       r[i] = mp[r[i]];     }     for (int i = 0; i < n; i++) add(1, l[i], r[i]);     for (int i = 0; i < n; i++) {       int mn = get(1, l[i], r[i]);       if (mn > 1) return cout << i + 1 << endl, 0;     }     return cout << -1 << endl, 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1500 + 10;   const int INF = 1e9;   int ans[MAX_N][26];   int main() {     ios::sync_with_stdio(0);     int n;     cin >> n;     string s;     cin >> s;     for (int it = 0; it < 26; ++it) {       char c = char(it +  a );       for (int i = 0; i < n; ++i) {         int cnt = 0;         for (int j = i; j < n; ++j) {           if (s[j] != c) cnt++;           ans[cnt][it] = max(ans[cnt][it], j - i + 1);         }       }       for (int i = 1; i <= n; ++i) {         if (ans[i][it] == 0) {           ans[i][it] = ans[i - 1][it];         }       }     }     int q;     cin >> q;     while (q--) {       int k;       char c;       cin >> k >> c;       cout << ans[k][(int)(c -  a )] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long w, m;     cin >> w >> m;     vector<int> a;     long long z = m;     while (z) {       a.push_back(z % w);       z /= w;     }     a.push_back(0);     int flag = 1;     for (int i = 0; i < a.size(); i++) {       if (a[i] == w || a[i] == w - 1) {         a[i] -= w;         a[i + 1] += 1;       }     }     for (int i = 0; i < a.size(); i++)       if (a[i] != 0 && a[i] != 1 && a[i] != -1) {         flag = 0;         break;       }     if (flag)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 7;   int n, a[N], f[N][21], pos[N], nxt[N], tmp[N], ans;   int query(int l, int r) {     int k = log2(r - l + 1);     return max(f[l][k], f[r - (1 << k) + 1][k]);   }   int main() {     scanf( %d , &n);     memset(pos, 0x3f, sizeof(pos));     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]), f[i][0] = a[i];       pos[tmp[a[i]]] = i;       tmp[a[i]] = i;     }     nxt[n] = n;     for (int i = n - 1; i >= 1; --i) nxt[i] = min(nxt[i + 1], pos[i] - 1);     for (int j = 1; j <= 20; ++j)       for (int i = 1; i + (1 << j) - 1 <= n; ++i)         f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);     for (int i = 1; i <= n; ++i) {       for (int j = i; j <= nxt[i];) {         int A = query(i, j);         if (A <= j - i + 1)           ans++, j++;         else           j = i + A - 1;       }     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const double PI = 3.141592653589793238463;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int q;     cin >> q;     while (q--) {       long long sum = 0, r = 0;       long long x, y;       cin >> x;       for (int i = 1; i <= x; i++) {         cin >> y;         sum += y;         r ^= y;       }       cout <<  2 n  << r <<     << sum + r <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string a, b, c, s;   map<string, long long> m[3];   long long f, q;   bool sach(string aa) {     if (s.size() >= aa.size() && s.substr(s.size() - aa.size(), aa.size()) == aa)       return true;     return false;   }   void solve(long long i) {     if (sach(a)) {       if (m[i].count(b) || m[i].count(c)) {         cout <<  NO ;         exit(0);       }       f = 1;       m[i][a]++;     }     if (sach(b)) {       if (m[i].count(b) || m[i].count(c)) {         cout <<  NO ;         exit(0);       }       f = 1;       q = 1;       m[i][b]++;     }     if (sach(c)) {       if (m[i].size() && m[i].count(b) == 0) {         cout <<  NO ;         exit(0);       }       m[i][c]++;       f = 1;     }     if (m[1].size() && m[2].size()) {       cout <<  NO ;       exit(0);     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     vector<string> ss;     while (cin >> s) {       f = 0;       ss.push_back(s);       a =  lios , b =  etr , c =  initis ;       solve(1);       a =  liala , b =  etra , c =  inites ;       solve(2);       if (f == 0) {         cout <<  NO ;         exit(0);       }     }     if (ss.size() == 1 || q)       cout <<  YES ;     else       cout <<  NO ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 300;   const long long mod = 1e9 + 7;   long long dp[N][N], mi[N], mi_1[N], C[N][N], n, k;   int main() {     cin >> n >> k;     for (int i = (0); i <= (n); i++) {       C[i][0] = 1;       for (int j = (1); j <= (i); j++)         C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;     }     mi[0] = mi_1[0] = 1;     for (int i = (1); i <= (n); i++)       mi[i] = mi[i - 1] * k % mod, mi_1[i] = mi_1[i - 1] * (k - 1) % mod;     for (int i = (1); i <= (n); i++) dp[1][i] = C[n][i] * mi_1[n - i] % mod;     for (int i = (2); i <= (n); i++)       for (int j = (0); j <= (n); j++)         for (int s = (0); s <= (j); s++) {           dp[i][j] = (dp[i][j] + dp[i - 1][s] * C[n - s][j - s] % mod * mi[s] %                                      mod * mi_1[n - j] % mod) %                      mod;           if (j == s)             dp[i][j] = (dp[i][j] - mi_1[n] * dp[i - 1][s] % mod + mod) % mod;         }     cout << dp[n][n];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int A[100];   int n;   int countNonZero = 0;   int sumElements(int size) {     int sum = 0;     for (int i = 0; i < size; i++) sum = sum + A[i];     return sum;   }   int findNonZero(int size) {     int ind = -1;     for (int i = 0; i < size; i++) {       if (A[i] != 0) {         ind = i;         break;       }     }     return ind;   }   void countZeros() {     for (int i = 0; i < n; i++) {       if (A[i] != 0) countNonZero++;     }   }   int main() {     int w;     cin >> n;     for (int i = 0; i < n; i++) cin >> A[i];     w = sumElements(n);     if (w != 0) {       cout <<  YES  << endl;       cout <<  1  << endl;       cout <<  1   << n << endl;       return 0;     } else {       int k = findNonZero(n);       if (k == -1 || countNonZero == 1) {         cout <<  NO  << endl;         return 0;       } else {         cout <<  YES  << endl;         cout <<  2  << endl;         cout <<  1   << k + 1 << endl;         cout << k + 2 <<     << n << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std;   int n, k, a[((int)(1e5) + 5)], x, y, bol[((int)(1e6 + 5))],       dp[((int)(1e6 + 5))], t, snr;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     cin >> x >> y;     sort(a + 1, a + n + 1);     n = unique(a + 1, a + n + 1) - a - 1;     for (int i = y; i <= x; i++) bol[i - y + 1] = 2;     for (int i = 1; i <= n; i++) {       t = a[i];       if (t < y)         if (!(y % t))           t = y;         else           t = ((y / t) + 1) * t;       if (t > x) continue;       for (; t <= x; t += a[i]) bol[t - y + 1] = a[i];     }     for (int i = y + 1; i <= x; i++) dp[i - y + 1] = ((int)(1e9 + 5));     for (int i = y; i <= x - 1; i++) {       t = -1;       snr = min(x, i + bol[i - y + 1] - 1);       for (int j = i + 1; j <= snr; j++) {         if (t == -1 or j + bol[j - y + 1] >= t + bol[t - y + 1]) t = j;         dp[j - y + 1] = min(dp[j - y + 1], dp[i - y + 1] + 1);       }       if (snr == x) break;       i = t - 1;     }     cout << dp[x - y + 1] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dp[31][31][51];   int solve(int i, int j, int k) {     if (i <= 0 || j <= 0) return 1e9;     if (i * j == k || k <= 0) return 0;     if (dp[i][j][k] != -1) return dp[i][j][k];     int op = 1e9;     for (int x = 1; x <= i; ++x)       for (int y = 1; y <= k; ++y)         op = min(op, j * j + solve(i - x, j, y) + solve(x, j, k - y));     for (int x = 1; x <= j; ++x)       for (int y = 1; y <= k; ++y)         op = min(op, i * i + solve(i, j - x, y) + solve(i, x, k - y));     return dp[i][j][k] = op;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     memset(dp, -1, sizeof(dp));     int t, n, m, k;     cin >> t;     while (t--) {       cin >> n >> m >> k;       cout << solve(n, m, k) <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e4 + 14;   const int INF = INT_MAX;   const int MAXC = 30;   const long long LLINF = 1e16 + 14;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       int d;       scanf( %d , &d);       int res;       scanf( %d , &res);       for (int i = 1; i < n; ++i) {         int x;         scanf( %d , &x);         int need = i;         int add = min(d / need, x);         d -= add * need;         res += add;       }       printf( %d n , res);     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   vector<vector<int>> g(1000);   bool removed[1000];   int cost[1000];   int n, d;   void input() {     cin >> n >> d;     for (int i = 0; i < n; i++) {       cin >> cost[i];     }   }   void solution() {     int sum = 0;     for (int i = 0; i < n; i++) sum += cost[i];     sum += 10 * (n - 1);     if (sum > d) {       cout << -1;     } else {       cout << (n - 1) * 2 + (d - sum) / 5;     }   }   int main() {     input();     solution();     return 0;   }
#include <algorithm>   #include <bitset>   #include <cmath>   #include <complex>   #include <cstdio>   #include <iostream>   #include <map>   #include <numeric>   #include <queue>   #include <random>   #include <set>   #include <stack>   #include <string>   #include <sys/types.h>   #include <unistd.h>   #include <unordered_map>   #include <unordered_set>   #include <vector>      #pragma region macros   #define _overload(_1, _2, _3, name, ...) name   #define _rep(i, n) _range(i, 0, n)   #define _range(i, a, b) for (int i = int(a); i < int(b); ++i)   #define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)   #define _rrep(i, n) _rrange(i, n, 0)   #define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)   #define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)   #pragma endregion macros      using namespace std;      template <class T> bool chmax(T &a, const T &b) {       return (a < b) ? (a = b, 1) : 0;   }   template <class T> bool chmin(T &a, const T &b) {       return (b < a) ? (a = b, 1) : 0;   }      using ll    = long long;   using R     = long double;   const R EPS = 1e-9L;  // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7   inline int sgn(const R &r) {       return (r > EPS) - (r < -EPS);   }   inline R sq(R x) {       return sqrt(max(x, 0.0L));   }      const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};   const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};      const pid_t pid = getpid();   // Problem Specific Parameter:      ll p[1010][1010];   ll psum[1010];   ll p2sum[1010];      int main(void) {       std::cin.tie(0);       std::ios::sync_with_stdio(false);          int m, k;       cin >> m >> k;          rep(i, k) {           rep(j, m) {               cin >> p[i][j];               psum[i] += p[i][j];               p2sum[i] += 1LL * p[i][j] * p[i][j];           }       }             const ll vsum = (psum[k - 1] - psum[0]) / (k - 1);       ll y          = -1;          rep(i, 1, k - 1) {           const ll v = (psum[i] - psum[i - 1]);           if (v != vsum) {               y = i;               break;           }       }          ll x;       rep(j, m) {           const ll v      = (psum[y] - psum[y - 1]);           const ll modify = vsum - v;              p2sum[y] -= p[y][j] * p[y][j];           p[y][j] += modify;           p2sum[y] += p[y][j] * p[y][j];              set<ll> s;           rep(i, 1, k - 1) {               const ll d2 = (p2sum[i - 1] + p2sum[i + 1]) - 2LL * p2sum[i];               s.insert(d2);           }              if (s.size() == 1) {               x = p[y][j];               break;           }              p2sum[y] -= p[y][j] * p[y][j];           p[y][j] -= modify;           p2sum[y] += p[y][j] * p[y][j];       }          cout << y <<     << x << endl;       return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long poww(long long a, long long b, long long md) {     return (!b ? 1                : (b & 1 ? a * poww(a * a % md, b / 2, md) % md                         : poww(a * a % md, b / 2, md) % md));   }   const long long MAXN = 1e6 + 10;   const long long INF = 8e18;   const long long MOD = 1e9 + 7;   pair<long long, long long> A[MAXN];   long long n;   set<pair<long long, long long> > st;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> A[i].first;       A[i].second = i;       st.insert({i, A[i].first});     }     sort(A, A + n);     long long ans = 0, val = 0;     long long rem = n;     for (int i = 0; i < n - 2; i++) {       auto it = st.find({A[i].second, A[i].first});       auto ite = st.end();       ite--;       if (it == st.begin() || it == ite) {         ans += (A[i].first - val) * (rem - 2);         val += (A[i].first - val);         st.erase(it);         rem--;       } else {         auto nxt = it, prv = it;         nxt++;         prv--;         ans += min(nxt->second, prv->second) - val;         st.erase(it);         rem--;       }     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct comp {     bool operator()(const vector<long long>& lhs,                     const vector<long long>& rhs) const {       return lhs[0] > rhs[0] || (lhs[0] == rhs[0] && lhs[2] > rhs[2]) ||              (lhs[0] == rhs[0] && lhs[2] == rhs[2] && lhs[1] > rhs[1]);     }   };   vector<long long> t;   vector<long long> ans;   priority_queue<vector<long long>, vector<vector<long long> >, comp> events;   queue<long long> q;   set<long long> inq;   priority_queue<long long, vector<long long>, greater<long long> > ready;   bool clear(long long index) {     auto it = inq.lower_bound(index);     if (it == inq.end()) return inq.empty();     return it == inq.begin();   }   int32_t main() {     ios::sync_with_stdio(false);     long long n, p;     cin >> n >> p;     t.resize(n);     ans.resize(n);     for (long long i = 0; i < n; i++) cin >> t[i];     for (long long i = 0; i < n; i++) events.push({t[i], i, 1});     while (!events.empty()) {       vector<long long> a = events.top();       events.pop();       long long status = a[2], index = a[1], time = a[0];       if (status == 1) {         if (clear(index)) {           if (q.empty()) events.push({time + p, index, 2});           q.push(index);           inq.insert(index);         } else           ready.push(index);       } else {         ans[index] = time;         q.pop();         inq.erase(index);         if (!ready.empty() && clear(ready.top())) {           long long nextIndex = ready.top();           ready.pop();           q.push(nextIndex);           inq.insert(nextIndex);         }         if (!q.empty()) {           long long nextIndex = q.front();           events.push({time + p, nextIndex, 2});         }       }     }     for (long long i = 0; i < n; i++) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int a[1000000], g[1000000], h[1000000];   int d[1000000];   int dp[1000000];   int up[1000000];   int h1[1000000];   int h2[1000000];   int t1[1000000];   int t2[1000000];   vector<int> v[1000000];   int x, y, n, m;   int maxy, one_kek, two_kek;   void dfs(int x, int pr = -1) {     h[x] = 1;     d[x] = g[x];     h1[x] = 0;     h2[x] = 0;     t1[x] = 0;     t2[x] = 0;     for (int i = 0; i < v[x].size(); i++) {       int to = v[x][i];       if (to == pr) continue;       dfs(to, x);       if (d[to] == 0) continue;       d[x] += d[to];       h[x] += h[to];       if (1 + h1[to] > h2[x]) {         h2[x] = 1 + h1[to];         t2[x] = to;         if (h2[x] > h1[x]) {           swap(h1[x], h2[x]);           swap(t1[x], t2[x]);         }       }     }     if (d[x] == 0) return;   }   void dfs1(int x, int pr = -1) {     if (d[x] == 0) return;     if (pr == -1)       dp[x] = h1[x];     else {       if (t1[pr] == x)         up[x] = 1 + max(up[pr], h2[pr]);       else         up[x] = 1 + max(up[pr], h1[pr]);       dp[x] = max(h1[x], up[x]);     }     for (int i = 0; i < v[x].size(); i++) {       int to = v[x][i];       if (to == pr) continue;       dfs1(to, x);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     for (int i = 1; i < n; i++) {       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     for (int i = 1; i <= m; i++) {       cin >> a[i];       g[a[i]]++;     }     if (m == 1) {       cout << a[1] <<   n  << 0;       return 0;     }     dfs(a[1]);     dfs1(a[1]);     for (int i = 1; i <= m; i++) {       if (dp[a[i]] > maxy) {         maxy = dp[a[i]];         one_kek = a[i];       } else if (dp[a[i]] == maxy)         one_kek = min(one_kek, a[i]);     }     cout << one_kek <<   n  << h[a[1]] * 2 - 2 - maxy <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int a[1003][1003];   int r[1003];   int c[1003];   char ch[5];   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %d , &a[i][j]);       }     }     for (int i = 1; i <= n; i++) {       r[i] = i;     }     for (int i = 1; i <= m; i++) {       c[i] = i;     }     for (int i = 1; i <= k; i++) {       scanf( %s , ch);       int x, y;       scanf( %d %d , &x, &y);       if (ch[0] ==  r ) {         swap(r[x], r[y]);       } else if (ch[0] ==  c ) {         swap(c[x], c[y]);       } else {         printf( %d n , a[r[x]][c[y]]);       }     }     return 0;   }