QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#728795 | #9570. Binary Tree | ucup-team4975 | RE | 1ms | 3612kb | C++23 | 3.2kb | 2024-11-09 15:56:35 | 2024-11-09 15:56:35 |
Judging History
answer
#define LOCAL
#include <bits/stdc++.h>
#define fir first
#define sec second
#define el '\n'
#ifdef LOCAL
#define FINISH cerr << "FINISH" << endl;
#else
#define FINISH ;
#endif
#ifdef LOCAL
#define debug(x) cerr << setw(4) << #x << " == " << x << endl
#else
#define debug(x)
#endif
#ifdef LOCAL
#define debugv(x) \
cerr << setw(4) << #x << ":: "; \
for (auto i : x) \
cerr << i << " "; \
cerr << endl
#else
#define debugv(x)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ostream& operator<<(ostream& out, PII& x)
{
out << x.fir << " " << x.sec << endl;
return out;
}
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int N = 200020;
void solve()
{
int n;
cin >> n;
int tim = 0;
vector<vector<int>> edge(n + 1);
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
if (x != 0) {
edge[i].push_back(x);
edge[x].push_back(i);
}
if (y != 0) {
edge[y].push_back(i);
edge[i].push_back(y);
}
}
int rt = 1, lft = n;
while (1) {
vector<int> siz(n + 1, 0);
int newrt = -1, s = inf;
auto dfs = [&](auto& self, int x, int fa) -> void {
siz[x] = 1;
int now = 0;
for (auto to : edge[x]) {
if (to == fa)
continue;
self(self, to, x);
siz[x] += siz[to];
now = max(now, siz[to]);
}
if (x != rt) {
now = max(now, lft - siz[x]);
}
if (now < s) {
s = now, newrt = x;
}
};
dfs(dfs, rt, rt);
rt = newrt;
int q1 = 0, q2 = 0;
if (edge[newrt].size() == 0) {
cout << "! " << newrt << endl;
return;
}
else if (edge[newrt].size() == 1) {
q1 = newrt, q2 = edge[newrt][0];
tim++;
cout << "? " << q1 << " " << q2 << endl;
int x;
cin >> x;
if (x == 0) {
cout << "! " << q1 << endl;
}
else if (x == 2) {
cout << "! " << q2 << endl;
}
return;
}
else {
vector<int> vis(n + 1, 0);
q1 = edge[newrt][0], q2 = edge[newrt][1];
tim++;
cout << "? " << q1 << " " << q2 << endl;
int x;
cin >> x;
queue<int> q;
lft = 0;
if (x == 1) {
if (edge[newrt].size() == 2) {
cout << "! " << newrt << endl;
return;
}
q.push(edge[newrt][2]);
vis[q1] = vis[q2] = 1;
newrt = edge[newrt][2];
lft = -2;
}
if (x == 0) {
// q1
q.push(q1);
vis[newrt] = 1;
newrt = q1;
lft = -1;
}
if (x == 2) {
q.push(q2);
vis[newrt] = 1;
newrt = q2;
lft = -1;
}
vector<vector<int>> edge1(n + 1);
while (!q.empty()) {
int rr = q.front();
// debug(x);
q.pop();
vis[rr] = 1;
for (auto to : edge[rr]) {
if (vis[to])
continue;
q.push(to);
edge1[rr].push_back(to);
edge1[to].push_back(rr);
}
}
for (int i = 1; i <= n; i++) {
edge[i].clear();
edge[i] = edge1[i];
}
for (int i = 1; i <= n; i++) {
lft += vis[i];
}
edge1.clear();
edge1.shrink_to_fit();
}
// FINISH
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3612kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 1 5 ? 2 4 ! 3 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Runtime Error
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0
output:
? 2 5 ! 8