malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   const int maxn = 100010;   const int INF = ~0U >> 1;   const double eps = 1e-9;   void getint(int &ret) {     char ch;     while (ch = getchar(), ch <  0  || ch >  9 )       ;     ret = ch -  0 ;     while (ch = getchar(), ch >=  0  && ch <=  9 ) ret = ret * 10 + ch -  0 ;   }   const int maxN = 210;   const int maxK = 42;   int nxt[maxN * 2], first[maxN], to[maxN * 2], vis[maxN], tot;   void add_edge(int x, int y) {     nxt[++tot] = first[x];     first[x] = tot;     to[tot] = y;   }   int dp[maxN][maxK * 2];   int f[maxK * 2][maxK * 2], g[maxK * 2][maxK * 2];   vector<int> son[maxN];   int n, k;   void dfs(int x) {     vis[x] = 1;     for (int i = first[x]; i; i = nxt[i])       if (!vis[to[i]]) {         son[x].push_back(to[i]);         dfs(to[i]);       }     if (son[x].empty()) {       dp[x][1 + k] = 1;       dp[x][-k + k] = 1;       return;     }     memset(f, 0, sizeof(f));     for (int i = -k; i <= k; i++) {       f[i + k][i + k] = (f[i + k][i + k] + dp[son[x][0]][i + k]) % mod;     }     for (int kt = 1; kt < son[x].size(); kt++) {       int Son = son[x][kt];       memset(g, 0, sizeof(g));       for (int Min = -k; Min <= k; Min++)         for (int Max = Min; Max <= k; Max++) {           if (f[Min + k][Max + k]) {             for (int L = -k; L <= k; L++) {               long long tmp = dp[Son][L + k];               g[min(Min, L) + k][max(Max, L) + k] =                   (g[min(Min, L) + k][max(Max, L) + k] +                    1ll * f[Min + k][Max + k] * tmp % mod) %                   mod;             }           }         }       for (int Min = -k; Min <= k; Min++)         for (int Max = Min; Max <= k; Max++)           f[Min + k][Max + k] = g[Min + k][Max + k];     }     for (int Min = -k; Min <= k; Min++)       for (int Max = Min; Max <= k; Max++) {         if (Min < k) {           if (Max + Min + 1 <= 0)             dp[x][Min + 1 + k] = (dp[x][Min + 1 + k] + f[Min + k][Max + k]) % mod;           else if (Max < k)             dp[x][Max + 1 + k] = (dp[x][Max + 1 + k] + f[Min + k][Max + k]) % mod;         }         dp[x][-k + k] = (dp[x][-k + k] + f[Min + k][Max + k]) % mod;       }   }   int main() {     cin >> n >> k;     if (k == 0) {       puts( 1 );       return 0;     }     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       add_edge(x, y);       add_edge(y, x);     }     int ans = 0;     dfs(1);     for (int i = -k; i <= 0; i++) ans = (ans + dp[1][i + k]) % mod;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long bin(long long x, long long n) {     long long res = 1;     x %= 1000000007;     while (n) {       if (n & 1) res = (res * x) % 1000000007;       x = (x * x) % 1000000007;       n >>= 1;     }     return res;   }   const long long MXN = 2e5 + 1;   vector<long long> adj[MXN];   long long n;   long long a[MXN], val[MXN], dp[MXN][2];   void dfs(long long u, long long p) {     vector<long long> chk0, chk1;     vector<pair<long long, long long> > in;     for (auto i : adj[u]) {       if (i == p) continue;       dfs(i, u);       in.push_back({dp[i][0] - dp[i][1], i});     }     sort(in.begin(), in.end());     chk1.push_back(0);     for (long long i = 0; i < in.size(); i++) {       chk1.push_back(dp[in[i].second][1]);       chk0.push_back(dp[in[i].second][0]);     }     long long y = in.size();     chk0.push_back(0);     for (long long i = chk0.size(); i >= 2; i--) {       chk0[i - 2] += chk0[i - 1];     }     for (long long i = 1; i < chk1.size(); i++) {       chk1[i] += chk1[i - 1];     }     long long odd = 0, eve = 0;     for (long long i = 0; i <= y; i += 2) {       eve = max(eve, chk1[i] + chk0[i]);     }     for (long long i = 1; i <= y; i += 2) {       odd = max(odd, chk1[i] + chk0[i]);     }     dp[u][0] = eve;     dp[u][1] = max(val[u] + eve, odd);   }   void solve() {     cin >> n;     for (long long i = 1; i <= n; i++) {       long long x, y;       cin >> x >> y;       if (x != -1) adj[i].push_back(x), adj[x].push_back(i);       val[i] = y;     }     dfs(1, -1);     cout << dp[1][1] << endl;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t = 1;     for (long long i = 1; i <= t; i++) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a;     vector<int> v;     for (int i = 0; i < n; ++i) {       cin >> a;       v.emplace_back(a);     }     int S = 0;     int D = 0;     int i = v.begin() - v.begin();     int j = v.end() - v.begin() - 1;     int chance = 0;     for (int ii = i, jj = j; jj >= ii; chance++) {       int score = max(v[ii], v[jj]);       if (chance % 2 == 0) {         S += score;       } else         D += score;       if (v[ii] > v[jj]) {         ii++;       } else         jj--;     }     cout << S <<     << D;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const long long INF = 1000000000;   template <class T>   inline void read(T &x) {     char c;     int flag = 1;     while ((c = getchar()) <  0  || c >  9 )       if (c ==  - ) flag *= -1;     x = c -  0 ;     while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ;     x *= flag;     return;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, q;     cin >> q;     map<long long, long long> dp;     while (q--) {       long long op, u, v;       cin >> op >> u >> v;       if (op == 1) {         long long w;         cin >> w;         while (u ^ v) {           if (u < v) swap(u, v);           dp[u] += w, u /= 2;         }       } else {         long long ans = 0;         while (u ^ v) {           if (u < v) swap(u, v);           ans += dp[u], u /= 2;         }         cout << ans << endl;       }     }   }
#include <bits/stdc++.h>   int main() {     int x, y, a, b, cnt = 0;     scanf( %d%d%d%d , &x, &y, &a, &b);     for (int i = a; i <= x; i++)       for (int j = b; j <= y && j < i; j++) cnt++;     printf( %d n , cnt);     for (int i = a; i <= x; i++)       for (int j = b; j <= y && j < i; j++) printf( %d %d n , i, j);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const long double pii = 3.14159265359;   const int MOD = 1000000007;   const char nl =   n ;   const int MX = 200001;   int N;   bool calc(int T, vector<int>& A, vector<int>& B) {     long long sum = 0;     for (int x = 0; x < (N); x++) {       if (A[x] > T) {         sum += B[x];         if (sum > T) {           return false;         }       }     }     return true;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int Test_case;     cin >> Test_case;     while (Test_case-- != 0) {       cin >> N;       vector<int> A(N), B(N);       for (auto& i : A) cin >> i;       for (auto& i : B) cin >> i;       int ans = INT_MAX;       int l = 1, r = 1000000000;       while (l <= r) {         int m = l + (r - l) / 2;         if (calc(m, A, B)) {           ckmin(ans, m);           r = m - 1;         } else           l = m + 1;       }       cout << ans << nl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int const maxn = 1001;   int n, m, cnt, H[maxn], father[maxn], d[maxn], Dep[maxn], dist[maxn],       dist2[maxn], middle_point[maxn], length[maxn];   int max_dep, max_dist;   struct Edge {     int next, to;   } E[maxn << 1];   inline void add(int a, int b) {     E[++cnt] = (Edge){H[a], b};     H[a] = cnt;   }   inline int find(int x) {     if (father[x] != x) father[x] = find(father[x]);     return father[x];   }   inline void merge(int x, int y) {     int r1 = find(x), r2 = find(y);     if (r1 != r2) father[r1] = r2;   }   void getdep(int x, int fa) {     Dep[x] = Dep[fa] + 1;     for (int i = H[x]; i; i = E[i].next)       if (E[i].to != fa) getdep(E[i].to, x);   }   void getdist(int x, int fa) {     dist[x] = dist[fa] + 1;     for (int i = H[x]; i; i = E[i].next)       if (E[i].to != fa) getdist(E[i].to, x);   }   void getdist2(int x, int fa) {     dist2[x] = dist2[fa] + 1;     for (int i = H[x]; i; i = E[i].next)       if (E[i].to != fa) getdist2(E[i].to, x);   }   inline void work(int x) {     getdep(x, 0);     int max_dep = 0;     for (int i = 1; i <= n; i++)       if (father[i] == x && Dep[i] > Dep[max_dep]) max_dep = i;     getdist(max_dep, 0);     int max_dist = 0;     for (int i = 1; i <= n; i++)       if (father[i] == x && dist[i] > dist[max_dist]) max_dist = i;     getdist2(max_dist, 0);     middle_point[x] = max_dep;     for (int i = 1; i <= n; i++)       if (father[i] == x && dist[i] + dist2[i] == dist[max_dist] &&           min(dist[i], dist2[i]) >               min(dist[middle_point[x]], dist2[middle_point[x]]))         middle_point[x] = i;     length[x] = dist[max_dist];   }   int main() {     Dep[0] = dist[0] = dist2[0] = -1;     for (int i = 1; i <= n; i++) middle_point[i] = i;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) father[i] = i;     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       add(x, y), add(y, x);       merge(x, y);     }     for (int i = 1; i <= n; i++) father[i] = find(father[i]);     for (int i = 1; i <= n; i++)       if (father[i] == i) work(i);     int first = 0;     for (int i = 1; i <= n; i++)       if (father[i] == i && length[i] >= length[first]) first = i;     int ans = length[first];     for (int i = 1; i <= n; i++) {       if (father[i] != i) continue;       if (i == first) continue;       if (ans == 1 && length[i] == 1)         ans += 2;       else if (abs(ans - length[i]) <= 1)         ans++;       else if ((ans & 1) && (length[i] & 1) && abs(ans - length[i] == 2))         ans++;     }     printf( %d n , ans);     for (int i = 1; i <= n; i++)       if (father[i] == i && i != first)         printf( %d %d n , middle_point[i], middle_point[first]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long intersect(long long l1, long long r1, long long l2, long long r2) {     long long l = max(l1, l2);     long long r = min(r1, r2);     return max(r - l, 0LL);   }   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int main() {     long long n, m;     double w;     cin >> n >> w >> m;     if (m > n * 2) {       cout <<  NO n ;       return 0;     }     if (n >= m) {       cout <<  YES n ;       for (int i = 0, j = 0; j < m;) {         while ((i + 1) * m <= j * n) i++;         if (i < n) {           long long z = intersect(i * m, (i + 1) * m, j * n, (j + 1) * n);           if (z > 0) printf( %d %f  , i + 1, w * z / m);         }         if ((i + 1) * m <= (j + 1) * n)           i++;         else {           cout <<   n ;           j++;         }       }       return 0;     }     int d = gcd(n, m);     n /= d, m /= d;     if (m != n + 1) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     for (int h = 0; h < d; h++)       for (int j = 0; j < m; j++) {         if (j > 0) printf( %lld %f  , h * n + j, w * j / m);         if (j < m - 1) printf( %lld %f  , h * n + j + 1, w * (m - 1 - j) / m);         printf(  n );       }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     pair<int, int> k[3];     char cell[9][9];     int n;     cin >> n;     char c;     int z = 0;     while (n) {       for (int i = 1; i < 9; i++) {         for (int j = 1; j < 9; j++) {           cin >> c;           if (c ==  K ) {             k[z].first = i;             k[z].second = j;             z++;           }         }       }       if (abs(k[0].first - k[1].first) == 4 ||           abs(k[0].first - k[1].first) == 0) {         if (abs(k[0].second - k[1].second) == 4 ||             abs(k[0].second - k[1].second) == 0) {           cout <<  YES n ;           z = 0;           n--;           continue;         }       }       cout <<  NO n ;       z = 0;       n--;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, i, j, m, k, l, h, g, ye, a, b, rez;   int p[300005], f[300005], ct[300005];   int v[300005], nv;   bool comp(int x, int y) { return f[x] < f[y]; }   int main() {     cin >> n >> m;     for (i = 1; i <= n; i++) cin >> p[i], f[p[i]] = i;     ye = p[n];     for (i = 1; i <= m; i++) {       cin >> a >> b;       if (f[a] < f[b]) ct[a]++;       if (b == p[n]) v[++nv] = a;     }     sort(v + 1, v + nv + 1, comp);     for (i = nv; i >= 1; i--) {       if (ct[v[i]] >= (n - f[v[i]]) - rez) rez++;     }     cout << rez <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   long long arr[200000 + 10] = {0};   long long dp[200000 + 10] = {0};   int main() {     int t;     cin >> t;     while (t--) {       long long n, k, p;       cin >> n >> p >> k;       for (int i = 1; i <= n; i++) {         cin >> arr[i];       }       sort(arr + 1, arr + n + 1);       long long sum = 0;       for (int i = 1; i <= n; i++) {         if (i < k) {           sum += arr[i];           dp[i] = sum;           continue;         }         sum = arr[i] + dp[i - k];         dp[i] = sum;       }       long long ans = 0;       for (int i = 1; i <= n; i++) {         if (dp[i] <= p) {           ans = i;         }       }       cout << ans << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int q;     cin >> q;     string s;     int n, k;     string r =   , g =   , b =   ;     vector<string> v;     for (int i = 0; i < 1500; i++) {       r +=  RGB ;       b +=  BRG ;       g +=  GBR ;     }     for (int m = 0; m < q; m++) {       v.clear();       cin >> n >> k;       cin >> s;       string d;       for (int i = 0; i < s.length(); i++) {         d = s.substr(i, k);         if (d.length() == k) v.push_back(d);       }       long long minn = 1e9;       for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {         int cntr = 0;         int cntb = 0;         int cntg = 0;         string s1 = (*it);         for (int i = 0; i < s1.length(); i++) {           if (s1[i] != r[i]) {             cntr++;           }           if (s1[i] != b[i]) {             cntb++;           }           if (s1[i] != g[i]) {             cntg++;           }         }         int mio = min(cntr, min(cntb, cntg));         if (mio < minn) minn = mio;       }       cout << minn << endl;     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const int INF = 1e9 + 7;   const int mod = 1e9 + 9;   const int maxn = 3e5 + 7;   const int lim = 800667;   int tmp;   void dfs(int a, int b) {     if (b == 0) {       tmp = INF;       return;     } else if (b == 1) {       tmp += a - 1;       return;     }     tmp += a / b;     dfs(b, a % b);   }   int main() {     int n;     scanf( %d , &n);     int res = n - 1;     for (int i = 1; i < n; ++i) {       tmp = 0;       dfs(n, i);       res = min(res, tmp);     }     printf( %d n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 7;   const int INF = 1e9 + 7;   const int MOD = 1e9 + 7;   struct as {     int id, x;   } a[maxn];   int vis[maxn];   bool cmp(as a, as b) { return a.x < b.x; }   vector<int> g[maxn];   vector<int> ans;   set<int> tmp;   bool check(as a) {     int i, j;     for (i = 0; i < g[a.id].size(); i++) {       if (vis[g[a.id][i]] == a.x) return false;       if (vis[g[a.id][i]]) tmp.insert(vis[g[a.id][i]]);     }     if (tmp.size() != a.x - 1) return false;     tmp.clear();     vis[a.id] = a.x;     return true;   }   int main() {     int i, j, n, m, t, z;     scanf( %d%d , &n, &m);     for (i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       g[x].push_back(y);       g[y].push_back(x);     }     for (i = 1; i <= n; i++) {       scanf( %d , &a[i].x);       a[i].id = i;     }     sort(a + 1, a + 1 + n, cmp);     for (i = 1; i <= n; i++) {       if (!check(a[i])) break;       ans.push_back(a[i].id);     }     if (i <= n) {       printf( -1 n );     } else {       for (i = 0; i < n; i++) {         printf( %d%c , ans[i], i == n - 1 ?   n  :    );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 5010;   char s[maxn];   int dp[maxn][maxn];   int lnk[maxn][26];   int n;   const int mod = (int)1e9 + 7;   int go(int pos, int x) {     if (dp[pos][x] != -1) {       return dp[pos][x];     }     if (pos == n) {       return dp[pos][x] = 1;     }     dp[pos][x] = go(pos + 1, x);     for (int i = 0; i < 26; i++) {       if (i != s[x] -  a  && lnk[x][i] != n) {         dp[pos][x] += go(pos + 1, lnk[x][i]);         if (dp[pos][x] >= mod) {           dp[pos][x] -= mod;         }       }     }     return dp[pos][x];   }   int now[maxn];   int main() {     scanf( %d , &n);     scanf( %s , s);     for (int i = 0; i < 26; i++) {       lnk[n][i] = n;     }     for (int i = 0; i < n; i++) {       dp[n][i] = 1;     }     for (int i = n - 1; i >= 0; i--) {       for (int j = 0; j < 26; j++) {         lnk[i][j] = lnk[i + 1][j];       }       int c = s[i] -  a ;       int f = lnk[i][c];       if (f != n) {         for (int j = 0; j < n; j++) {           now[j] -= dp[j + 1][f];           if (now[j] < 0) {             now[j] += mod;           }         }       }       lnk[i][c] = i;       for (int j = n - 1; j >= 0; j--) {         dp[j][i] = now[j] + dp[j + 1][i];         if (dp[j][i] >= mod) {           dp[j][i] -= mod;         }       }       for (int j = 0; j < n; j++) {         now[j] += dp[j + 1][i];         if (now[j] >= mod) {           now[j] -= mod;         }       }     }     cout << dp[0][0] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200035;   long long a[N];   long long b[N];   long long c[N];   const long long INF = 1e17;   bool can[N];   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n, k;     cin >> n >> k;     ++n;     for (int i = 0; i < n; ++i) cin >> a[i], c[i] = a[i];     for (int i = 0; i < N; ++i) {       if (a[i] % 2 == 0) {         a[i + 1] += a[i] / 2;         a[i] = 0;       }     }     int ans = 0;     b[N - 1] = -a[N - 1];     can[N - 1] = 1;     for (int i = N - 2; i >= 0; --i) {       b[i] = (b[i + 1] * 2) - a[i];       can[i] = 1;       if (abs(b[i]) > INF) break;     }     for (int i = 0; i < n; ++i) {       if (can[i]) {         b[i] += c[i];         ans += abs(b[i]) <= k && (i != n - 1 or b[i]);       }       if (a[i]) break;     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int n, a, b, k, cnt = 0, ans;   string s;   vector<int> arr;   int main(void) {     ios ::sync_with_stdio(0);     cin.tie(0);     cin >> n >> a >> b >> k >> s;     for (int i = 1; i <= n; ++i) {       if (s[i - 1] ==  0 ) {         ++cnt;       } else {         cnt = 0;       }       if (cnt == b) {         arr.emplace_back(i);         cnt = 0;       }     }     ans = int(arr.size()) - (a - 1);     cout << ans <<   n ;     for (int i = 0; i <= ans - 1; ++i) {       cout << arr[i] <<    ;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long Ins, Del, Rep, Exc;   string A;   string B;   int lenA, lenB;   long long DP[4001][4001];   int posA[4001][26];   int posB[4001][26];   int main() {     ios::sync_with_stdio(false);     while (cin >> Ins >> Del >> Rep >> Exc) {       cin >> A >> B;       A =  .  + A;       B =  .  + B;       lenA = A.length() - 1;       lenB = B.length() - 1;       memset(posA, 0xff, sizeof(posA));       memset(posB, 0xff, sizeof(posB));       for (int i = 0; i < 26; i++) {         for (int j = 1; j <= lenB; j++)           if (B[j] == i +  a )             posB[j][i] = j;           else             posB[j][i] = posB[j - 1][i];         for (int j = 1; j <= lenA; j++)           if (A[j] == i +  a )             posA[j][i] = j;           else             posA[j][i] = posA[j - 1][i];       }       DP[0][0] = 0;       for (int i = 0; i <= lenA; i++)         for (int j = 0; j <= lenB; j++)           if (i + j > 0) {             DP[i][j] = 1000000000000000000LL;             if (A[i] == B[j]) DP[i][j] = DP[i - 1][j - 1];             if (i >= 1 && j >= 1)               DP[i][j] = min(DP[i][j], DP[i - 1][j - 1] + Rep);             if (i >= 1) DP[i][j] = min(DP[i][j], DP[i - 1][j] + Del);             if (j >= 1) DP[i][j] = min(DP[i][j], DP[i][j - 1] + Ins);             int u = posA[i][B[j] -  a ];             int v = posB[j][A[i] -  a ];             if (u > 0 && v > 0 && u < i && v < j)               DP[i][j] = min(DP[i][j], DP[u - 1][v - 1] + Exc +                                            Ins * (j - v - 1) + Del * (i - u - 1));           }       cout << DP[lenA][lenB] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int inf = 1e9 + 7;   const long long int mod = 1e9 + 9;   long long int mini(long long int a, long long int b) {     if (a >= b) return b;     return a;   }   long long int maxi(long long int a, long long int b) {     if (a >= b) return a;     return b;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long int n;     cin >> n;     map<long long int, long long int> cnt;     long long int ans = 0;     vector<long long int> A;     for (long long int i = 0; i <= n - 1; i++) {       string s;       cin >> s;       long long int x = 0;       for (long long int j = 0; j <= s.length() - 1; j++) {         long long int mask = (1LL << (s[j] -  a ));         x ^= mask;       }       ans += cnt[x];       cnt[x]++;       A.push_back(x);     }     for (long long int i = 0; i <= n - 1; i++) {       long long int x = A[i];       for (long long int j = 0; j <= 25; j++) {         long long int mask = (1LL << j);         if (mask & x) {           x ^= mask;           ans += cnt[x];           x ^= mask;         }       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e3;   int N, p[MAXN + 1];   set<int> adj[MAXN + 1];   void dfs(int v, int padre) {     p[v] = padre;     for (auto x : adj[v]) {       if (x == padre || p[x]) continue;       p[x] = padre;       dfs(x, v);     }     adj[v].clear();   }   int main() {     ios_base::sync_with_stdio(false);     cin >> N;     for (int i = 0; i < N - 1; i++) {       int u, v;       cin >> u >> v;       adj[u].insert(v);       adj[v].insert(u);     }     while (1) {       int cnt = 0;       int Max = 0, pos = 0;       for (int i = 1; i <= N; i++) {         if (!p[i]) {           cnt++;           int sz = adj[i].size();           if (sz > Max) {             Max = sz;             pos = i;           }         }       }       if (cnt == 1) {         for (int i = 1; i <= N; i++) {           if (!p[i]) {             cout <<  !   << i << endl;             break;           }         }         break;       } else if (cnt == 2) {         int u = -1, v = -1;         for (int i = 1; i <= N; i++) {           if (!p[i]) {             if (u == -1)               u = i;             else               v = i;           }         }         cout <<  ?   << u <<     << v << endl;         int lca;         cin >> lca;         cout <<  !   << lca << endl;         break;       }       int u = -1, v = -1;       vector<int> del;       for (auto x : adj[pos]) {         if (u == -1)           u = x;         else           v = x;         if (u != -1 && v != -1) {           cout <<  ?   << u <<     << v << endl;           int lca;           cin >> lca;           if (lca == u || lca == v) {             dfs(pos, lca);             u = v = -1;             del.clear();             break;           } else {             dfs(u, pos);             dfs(v, pos);             del.push_back(u);             del.push_back(v);             u = v = -1;           }         }       }       for (auto x : del) adj[pos].erase(x);     }   }