QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#811819 | #9570. Binary Tree | Insert_Username_Here | WA | 2ms | 5692kb | C++20 | 2.1kb | 2024-12-13 02:46:15 | 2024-12-13 02:46:15 |
Judging History
answer
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
// const ll mod = 1e9 + 7;
// #include <brawlstars>
// FOR PAIN OR FOR GLORYYY ELLL PRIMOOOOOO
const int N = 1e5 + 1;
pii ch[N];
int sz[N], p[N];
int dfs(int i) {
if(i < 0) return 0;
sz[i] = 1 + dfs(ch[i].f) + dfs(ch[i].s);
return sz[i];
}
int qry(int i, int j) {
cout << "? " << i + 1 << " " << j + 1 << "\n";
cout.flush();
int ans;
cin >> ans;
return ans;
}
int bk(int i, int r) {
if(sz[r] == 3) {
int c1, c2, c3;
if(ch[r].f >= 0 && ch[r].s >= 0) c1 = ch[r].f, c2 = r, c3 = ch[r].s;
else c1 = r, c2 = (ch[r].f >= 0 ? ch[r].f : ch[r].s), c3 = (ch[c2].f >= 0 ? ch[c2].f : ch[c2].s);
int ans = qry(c1, c3);
if(ans == 0) return c1;
if(ans == 1) return c2;
return c3;
}
if(ch[i].f < 0 && ch[i].s < 0 && i == r) return i;
if(ch[i].f < 0 || ch[i].s < 0) {
if(ch[i].f < 0) swap(ch[i].f, ch[i].s);
if(sz[r] - sz[ch[i].f] < sz[ch[i].f]) return bk(ch[i].f, r);
if(qry(i, ch[i].f)) return bk(ch[i].f, ch[i].f);
int u = i;
while(p[u] >= 0) sz[u] -= sz[ch[i].f], u = p[u];
sz[u] -= sz[ch[i].f], ch[i].f = -1;
return bk(r, r);
}
if(sz[ch[i].f] < sz[ch[i].s]) swap(ch[i].f, ch[i].s);
int num = ceil(sz[r] / 3.0);
if(sz[r] - sz[i] + 1 < num && sz[ch[i].s] < num) return bk(ch[i].f, r);
int ans = qry(ch[i].f, ch[i].s);
if(ans == 0) return bk(ch[i].f, ch[i].f);
if(ans == 2) return bk(ch[i].s, ch[i].s);
int u = i;
while(p[u] >= 0) sz[u] -= sz[ch[i].f] + sz[ch[i].s], u = p[u];
sz[u] -= sz[ch[i].f] + sz[ch[i].s], ch[i] = {-1, -1};
return bk(r, r);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
for(int i = 0; i < n; i++) p[i] = -1;
for(int i = 0; i < n; i++) {
cin >> ch[i].f >> ch[i].s;
ch[i].f--, ch[i].s--;
if(ch[i].f >= 0) p[ch[i].f] = i;
if(ch[i].s >= 0) p[ch[i].s] = i;
}
int r = 0;
while(p[r] >= 0) r = p[r];
dfs(r);
int ans = bk(r, r);
cout << "! " << ans + 1 << "\n";
cout.flush();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5692kb
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 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 3592kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 1 1 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 1 2 5 4 5 3 1 0 0 0 0 0 0 1 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 2 5 3 0 5 1 0 0 0 0 4 0 0 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 4 5 ? 6 8 ? 1 2 ! 1 ? 7 3 ? 1 4 ? 3 2 ! 3 ? 8 5 ? 4 2 ? 8 6 ! 6 ? 4 5 ? 1 3 ! 1 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 5 4 ! 5 ? 4 2 ? 1 5 ! 5 ? 2 3 ! 3 ? 1 2 ! 2 ? 2 3 ! 3 ? 6 5 ? 9 7 ? 2 8 ! 2 ? 1 2 ! 1 ? 7 3 ? 2 1 ? 7 9 ! 7 ? 10 3 ? 6 2 ? 10 4 ! 4 ? 8 2 ? 6 5 ? 3 4 ! 4 ? 2 1 ! 1 ? 3 6 ? 5 1 ? 7 4
result:
wrong answer Too many queries (test case 17)