QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#755673 | #9570. Binary Tree | Magical_Kingdom# | WA | 2ms | 8516kb | C++17 | 3.1kb | 2024-11-16 17:52:17 | 2024-11-16 17:52:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int n;
vector<int> to[maxn];
bool del[maxn];
void dfs(int now, int fa, int dep, int& max_dep, int& v) {
if (dep > max_dep) {
max_dep = dep;
v = now;
}
for (auto x : to[now]) {
if (del[x] == true) {
continue;
}
if (x == fa) {
continue;
}
dfs(x, now, dep + 1, max_dep, v);
}
}
void get_line(int root, int& u, int& v) {
int max_dep = 0, w = 0;
dfs(root, 0, 1, max_dep, w);
int c = 0;
max_dep = 0;
dfs(w, 0, 1, max_dep, c);
u = w, v = c;
}
int dis1[maxn], dis2[maxn];
bool vis[maxn];
void bfs(int start, int dis[]) {
memset(vis, 0, sizeof(vis));
// memset(dis, 0x7f, sizeof(dis));
for (int i = 1; i <= n; i++) {
dis[i] = 2e9;
}
dis[start] = 0;
vis[start] = 1;
queue<int> q;
q.push(start);
while (!q.empty()) {
int x = q.front();
q.pop();
for (auto y : to[x]) {
if (del[y] == true || vis[y]) {
continue;
}
dis[y] = dis[x] + 1;
vis[y] = true;
q.push(y);
}
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
if (x != 0) {
to[i].push_back(x);
to[x].push_back(i);
}
if (y != 0) {
to[i].push_back(y);
to[y].push_back(i);
}
}
int times = floor(log2(n));
while (times--) {
int u = 0, v = 0;
int root = 0;
for (int i = 1; i <= n; i++) {
if (del[i] == false) {
root = i;
get_line(root, u, v);
break;
}
}
cout << "? " << u << ' ' << v << '\n';
cout.flush();
int in;
cin >> in;
bfs(u, dis1);
bfs(v, dis2);
if (in == 0) {
for (int i = 1; i <= n; i++) {
if (del[i] == false) {
if (dis2[i] <= dis1[i]) {
del[i] = true;
}
}
}
} else if (in == 1) {
for (int i = 1; i <= n; i++) {
if (del[i] == false) {
if (dis2[i] != dis1[i]) {
del[i] = true;
}
}
}
} else {
for (int i = 1; i <= n; i++) {
if (del[i] == false) {
if (dis2[i] >= dis1[i]) {
del[i] = true;
}
}
}
}
}
for (int i = 1; i <= n; i++) {
if (del[i] == false) {
cout << "! " << i << '\n';
cout.flush();
}
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 8516kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 1 2 0 2 0 0
output:
? 4 1 ? 5 1 ! 2 ? 2 2
result:
wrong answer Query two identical vertices 2 (test case 2)