QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#735755 | #9570. Binary Tree | ucup-team5799 | WA | 1ms | 3656kb | C++20 | 2.5kb | 2024-11-11 21:33:26 | 2024-11-11 21:33:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using LL = long long;
void RES(int x) {
cout << "! " << x << endl;
}
void ASK(int u, int v) {
cout << "? " << u << " " << v << endl;
}
int GET() {
int op; cin >> op;
return op;
}
void solve() {
int n; cin >> n;
vector<vector<int>> G(n + 1);
for (int i = 1;i <= n;i++) {
int a, b; cin >> a >> b;
if (a) {
G[a].push_back(i);
G[i].push_back(a);
}
if (b) {
G[b].push_back(i);
G[i].push_back(b);
}
}
auto removex = [&](vector<int>& arr, int x) {
int pos = -1;
for (int i = 0;i < arr.size();i++) {
if (arr[i] == x) {
pos = i; break;
}
}
if (pos != -1) arr.erase(arr.begin() + pos);
};
int ok = 0;
auto dfs = [&](auto&&dfs, int u, int fa) -> void {
if (ok) return;
if (G[u].size() == 0) {
RES(u);
ok = 1;
return;
}
else if (G[u].size() == 1) {
int v = G[u][0];
// ASK(u, v);
// int op = GET();
// if (op == 0) {
// RES(u);
// ok = 1;
// return;
// }
removex(G[v], u);
dfs(dfs, v, u);
}
else if (G[u].size() == 2) {
ASK(G[u][0], G[u][1]);
int op = GET();
removex(G[u], G[u][0]);
removex(G[u], G[u][1]);
removex(G[G[u][0]], u);
removex(G[G[u][1]], u);
if (op == 1) {
RES(u);
ok = 1;
return;
}
else if (op == 0) {
dfs(dfs, G[u][0], u);
}
else dfs(dfs, G[u][1], u);
}
else {
int v1 = G[u][0], v2 = G[u][1], v3 = G[u][2];
removex(G[u], v1);
removex(G[u], v2);
removex(G[v1], u);
removex(G[v2], u);
ASK(v1, v2);
int op = GET();
if (op == 1) {
dfs(dfs, v3, u);
}
else if (op == 0) {
dfs(dfs, v1, u);
}
else dfs(dfs, v2, u);
}
};
dfs(dfs, 1, 1);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3656kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2
output:
? 5 3 ! 4
result:
wrong answer There are 2 candidates. (test case 1)