QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#735725 | #9570. Binary Tree | yzhx# | RE | 0ms | 3860kb | C++17 | 2.6kb | 2024-11-11 21:26:27 | 2024-11-11 21:26:27 |
Judging History
answer
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
const int N = 1e5;
int head[N + 2], nxt[N * 2 + 2], ver[N * 2 + 2], tot;
int n, v[N + 2], sz[N + 2], mx[N + 2], S, root;
void add(int x, int y) {
tot++;
nxt[tot] = head[x];
ver[tot] = y;
head[x] = tot;
}
void find(int x, int fa) {
sz[x] = 1, mx[x] = 0;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (y == fa || v[y]) continue;
find(y, x);
sz[x] += sz[y];
mx[x] = std::max(mx[x], sz[y]);
}
mx[x] = std::max(mx[x], S - sz[x]);
if (mx[x] < mx[root]) root = x;
}
void deal(int x) {
v[x] = 1;
std::vector<int> q;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (v[y]) continue;
q.push_back(y);
}
if (q.size() == 0) {
std::cout << "! " << x << std::endl;
std::cout.flush();
return;
} else if (q.size() == 1) { // ?
std::cout << "? "<<q[0] << " " << x << std::endl;
std::cout.flush();
int a;
std::cin >> a;
if (a == 0)
std::cout << "! " << q[0] << std::endl;
else
std::cout << "! " << x << std::endl;
std::cout.flush();
return;
} else {
std::cout <<"? "<< q[0] << " " << q[1] << std::endl;
std::cout.flush();
int a;
std::cin >> a;
if (a == 1) {
v[q[0]] = 1, v[q[1]] = 1, v[x] = 0;
find(x, q[0]);
S = sz[x], root = 0, mx[0] = 1e9;
find(x, q[0]);
deal(root);
} else if (a == 0) {
find(q[0], x);
S = sz[q[0]], root = 0, mx[0] = 1e9;
find(q[0], x);
deal(root);
} else {
find(q[1], x);
S = sz[q[1]], root = 0, mx[0] = 1e9;
find(q[1], x);
deal(root);
}
}
std::cout.flush();
}
void solve() {
std::cin >> n;
tot = 0;
for (int i = 1; i <= n + 1; i++) head[i] = v[i] = sz[i] = mx[i] = 0;
for (int i = 1; i <= n; i++) {
int a, b;
std::cin >> a >> b;
if (a) add(i, a), add(a, i);
if (b) add(i, b), add(b, i);
}
// add(n, n + 1), add(n + 1, n);
// root = n + 1;
find(1, 0);
S = sz[1], root = 0, mx[0] = 1e9;
find(1, 0);
deal(root);
}
int main() {
int times;
std::cin >> times;
while (times--) {
solve();
}
return 0;
}
/*
1
5
4 0
1 3
0 5
0 0
0 0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3860kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 5 ? 2 1 ! 2 ? 1 2 ! 2
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 1 1 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 2 2 5 4 5 3 1 0 0 0 0 0 0 0 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 1 1 0 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 0 0 3 3 0 1 0 0 0 2 2 2 0 0 0 0 3 2 3 0 0 0 0 2 10 2 8 9 7 0 ...
output:
? 4 5 ? 6 8 ? 2 1 ! 2 ? 3 5 ? 3 4 ? 2 1 ! 1 ? 6 1 ? 7 1 ? 1 5 ! 5 ? 2 5 ? 2 3 ! 2 ? 7 6 ? 4 1 ? 5 3 ! 5 ? 1 5 ? 1 3 ! 3 ? 4 2 ? 4 3 ! 4 ? 2 3 ! 3 ? 1 2 ! 1 ? 3 2 ! 2 ? 5 6 ? 7 9 ? 8 2 ! 2 ? 1 2 ! 1 ? 9 5 ? 9 6 ? 9 1 ! 9 ? 8 5 ? 5 1 ? 9 3 ! 7 ? 9 4 ? 9 7 ? 2 1 ! 1 ? 1 2 ! 2 ? 6 3 ? 7 1 ? 4 5