QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#746687 | #9570. Binary Tree | ATM12345# | ML | 0ms | 0kb | C++17 | 1.8kb | 2024-11-14 15:16:21 | 2024-11-14 15:16:21 |
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 root;
int s[MAX];
void build(int x) {
s[x] = 1;
if (l[x]) {
build(l[x]);
s[x] += s[l[x]];
}
if (r[x]) {
build(r[x]);
s[x] += s[r[x]];
}
}
int k;
void find(int x) {
build(x);
if (k == 1) {
res(x);
return;
}
if (l[x] && s[l[x]] * 2 > k) {
find(l[x]);
} else if (r[x] && s[r[x]] * 2 > k) {
find(r[x]);
} else {
if (l[x] && r[x]) {
int t = ask(l[x], r[x]);
if (t == 0) {
k = s[l[x]];
find(l[x]);
} else if (t == 2) {
k = s[r[x]];
find(r[x]);
} else {
k -= s[l[x]] + s[r[x]];
l[x] = 0;
r[x] = 0;
find(root);
}
} else if (l[x]) {
int t = ask(l[x], x);
if (t == 0) {
k = s[l[x]];
find(l[x]);
} else {
k -= s[l[x]];
l[x] = 0;
find(root);
}
} else if (r[x]) {
int t = ask(r[x], x);
if (t == 0) {
k = s[r[x]];
find(r[x]);
} else {
k -= s[r[x]];
r[x] = 0;
find(root);
}
}
}
}
int vis[MAX];
void sol() {
int n;
cin >> n;
k = 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;
}
for (int x = 1; x <= n; x++) {
if (!vis[x]) {
root = x;
break;
}
}
find(root);
}
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 ...