QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#740244 | #9570. Binary Tree | fosov | WA | 1ms | 5744kb | C++14 | 3.1kb | 2024-11-13 06:54:58 | 2024-11-13 06:54:58 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3fll
#define MOD 998244353
#define pii pair<int, int>
#define N 100010
#define K 20
int l[N], r[N], vld[N], f[N], sz[N], ind[N];
void dfs(int u) {
if (!vld[u]) return;
sz[u] = 1;
dfs(l[u]);
dfs(r[u]);
sz[u] += sz[l[u]] + sz[r[u]];
}
void fg(int u, int flag) {
if (!vld[u]) return;
f[u] = flag;
fg(l[u], flag);
fg(r[u], flag);
}
int root(int n) {
for (int i = 1; i <= n; ++ i) ind[i] = 0;
for (int i = 1; i <= n; ++ i) {
if (vld[i] && vld[l[i]]) ind[l[i]] ++;
if (vld[i] && vld[r[i]]) ind[r[i]] ++;
}
for (int i = 1; i <= n; ++ i) {
if (vld[i] && ind[i] == 0) return i;
}
return -1;
}
int query(int u, int v) {
cout << "? " << u << ' ' << v << '\n';
cout.flush();
int res; cin >> res;
if (res == -1) {
cout << "ask " << u << ' ' << v << '\n';
exit(1);
}
return res;
}
int main() {
#ifdef TEST
freopen("zz.in", "r+", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t; cin >> t;
while (t --) {
int n; cin >> n;
for (int i = 1; i <= n; ++ i) cin >> l[i] >> r[i];
for (int i = 1; i <= n; ++ i) vld[i] = 1;
int rt = root(n);
for (int i = 1; i <= n; ++ i) sz[i] = 0;
dfs(rt);
while (sz[rt] > 1) {
int mn = INF, pt = -1;
for (int i = 1; i <= n; ++ i) {
if (!vld[i]) continue;
int szl = sz[l[i]];
int szr = sz[r[i]];
int szu = sz[rt] - szl - szr;
if ((szl || szr) && max(szl, max(szr, szu)) < mn) {
mn = max(szl, max(szr, szu));
pt = i;
}
}
int szl = sz[l[pt]];
int szr = sz[r[pt]];
assert(szl || szr);
fg(rt, 0);
if (szl && szr) {
// cout << "@1\n";
int x = query(l[pt], r[pt]);
if (x == 0) fg(l[pt], 1);
if (x == 1) fg(rt, 1), fg(l[pt], 0), fg(r[pt], 0);
if (x == 2) fg(r[pt], 1);
}
if (!szl) {
// cout << "@2\n";
int x = query(r[pt], pt);
if (x == 0) fg(r[pt], 1);
if (x == 1) assert(1 == 0);
if (x == 2) fg(rt, 1), fg(r[pt], 0);
}
if (!szr) {
// cout << "@3\n";
int x = query(l[pt], pt);
if (x == 0) fg(l[pt], 1);
if (x == 1) assert(2 == 0);
if (x == 2) fg(rt, 1), fg(l[pt], 0);
}
for (int i = 1; i <= n; ++ i) vld[i] &= f[i];
rt = root(n);
for (int i = 1; i <= n; ++ i) sz[i] = 0;
dfs(rt);
}
cout << "! " << rt << '\n';
cout.flush();
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5744kb
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
Wrong Answer
time: 0ms
memory: 3600kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 2 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 1 0 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 1 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 2 0 3 3 0 1 0 0 0 2
output:
? 8 6 ? 5 4 ? 3 4 ! 3 ? 3 7 ? 7 8 ? 6 8 ! 8 ? 5 8 ? 4 2 ? 6 8 ! 6 ? 4 5 ? 3 1 ! 3 ? 5 6 ? 1 4 ! 5 ? 5 1 ? 4 5 ! 5 ? 2 4 ? 3 4 ! 3 ? 3 1 ? 1 2
result:
wrong answer Too many queries (test case 8)