QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#746778 | #9570. Binary Tree | ATM12345# | ML | 0ms | 0kb | C++17 | 1.7kb | 2024-11-14 15:31:29 | 2024-11-14 15:31:30 |
answer
#include <bits/stdc++.h>
//#define int long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
using namespace std;
const int MAX = 2e5+5;
#define ff first
#define ss second
int ask(int u, int v) {
cout << "? " << u << ' ' << v << endl;
int x;
cin >> x;
return x;
}
int res(int u) {
cout << "! " << u << endl;
}
int l[MAX], r[MAX];
int s[MAX];
int build(int x) {
s[x] = 1;
if (l[x]) {
s[x] += build(l[x]);
}
if (r[x]) {
s[x] += build(r[x]);
}
return s[x];
}
int vis[MAX];
void sol() {
int n;
cin >> n;
for (int x = 1; x <= n; x++) {
vis[x] = false;
}
for (int x = 1; x <= n; x++) {
cin >> l[x] >> r[x];
vis[l[x]] = true;
vis[r[x]] = true;
}
int init_root;
for (int x = 1; x <= n; x++) {
if (!vis[x]) {
init_root = x;
break;
}
}
int x = init_root;
while (true) {
int root = x;
int k = build(x);
if (k == 1) {
res(x);
break;
}
while (true) {
if (l[x] && s[l[x]] * 2 > k) {
x = l[x];
} else if (r[x] && s[r[x]] * 2 > k) {
x = r[x];
} else {
if (l[x] && r[x]) {
int t = ask(l[x], r[x]);
if (t == 0) {
x = l[x];
} else if (t == 2) {
x = r[x];
} else {
l[x] = 0;
r[x] = 0;
x = root;
}
} else if (l[x]) {
int t = ask(l[x], x);
if (t == 0) {
x = l[x];
} else {
l[x] = 0;
x = root;
}
} else if (r[x]) {
int t = ask(r[x], x);
if (t == 0) {
x = r[x];
} else {
r[x] = 0;
x = root;
}
}
break;
}
}
}
}
signed main()
{
//IOS
int tt=1;
cin>>tt;
while (tt--)
sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Interactor Memory Limit Exceeded
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0
output:
? 1 5 ? 2 4 ! 3 ...