rewrite this code in Python: #include <bits/stdc++.h> using namespace std; const int INF…
- User
rewrite this code in Python: #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7, MD = 998244353, MAX = 200007, R = 1 << 19, MOD = 1040015701, MOD2 = 1e9 + 9, LG = 18, B = 40; int rt, s; int sub[MAX], heavy[MAX], used[MAX], side_s[2]; vector g[MAX], query, side_v[2];
bool ask(vector & a, int v) { cout << "? " << (int)(a.size()) << ' ' << v; for (auto& u : a) cout << ' ' << u; cout << endl; bool f; cin >> f; return f; }
void dfs(int v, int p = 0) { sub[v] = 1, heavy[v] = 0; for (auto& u : g[v]) if (!used[u] && u != p) { dfs(u, v); sub[v] += sub[u], heavy[v] = max(heavy[v], sub[u]); } heavy[v] = max(heavy[v], s - sub[v]); if (!rt || heavy[v] < heavy[rt]) rt = v; }
signed main() { int n; cin >> n, s = n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].push_back(v), g[v].push_back(u); } int where = 1; while (true) { side_s[0] = side_s[1] = rt = 0; side_v[0].clear(), side_v[1].clear(), query.clear(); dfs(where); where = rt; dfs(where); for (auto& v : g[where]) if (!used[v]) query.push_back(v); if ((int)(query.size()) == 1) { bool f = ask(query, where); return !(cout << "! " << (f ? where : query[0]) << endl); } sort(query.begin(), query.end(), [&sub](int u, int v) { return sub[u] > sub[v]; }); for (auto& v : query) side_v[side_s[0] >= side_s[1]].push_back(v), side_s[side_s[0] >= side_s[1]] += sub[v]; bool f = !ask(side_v[0], where); s -= side_s[f]; for (auto& v : side_v[f]) used[v] = 1; } }
- Assistant