QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#738042 | #9570. Binary Tree | wdw | WA | 7ms | 3612kb | C++20 | 2.7kb | 2024-11-12 17:33:17 | 2024-11-12 17:33:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<vector<int>> q;
vector<int> fa1;
int js(int a, int b) {
cout << "? " << a << " " << b << endl;
int x;
cin >> x;
return x;
}
vector<int> ma, vis, siz;
int ans1 = 0, ans2 = 0;
int id1, id2;
int num = 0;
void dfs(int u, int fa) {
num++;
siz[u] = 1;
ma[u] = 0;
for (auto j: q[u]) {
if (j == fa || vis[j])continue;
dfs(j, u);
siz[u] += siz[j];
ma[u] = max(siz[j], ma[u]);
}
}
void dfs2(int u, int fa) {
ma[u] = max(ma[u], num - siz[u]);
for (auto j: q[u]) {
if (j == fa || vis[j])continue;
dfs2(j, u);
}
if (ans1 > ma[u]) {
ans1 = ma[u];
id1 = u;
}
}
void solve() {
int n;
cin >> n;
siz = vis = ma = fa1 = vector<int>(n + 5);
q = vector<vector<int>>(n + 5);
for (int i = 1, a, b; i <= n; i++) {
cin >> a >> b;
if (a) {
q[i].push_back(a);
q[a].push_back(i);
}
if (b) {
q[i].push_back(b);
q[b].push_back(i);
}
}
ans1 = 0, ans2 = 0;
num = 0;
ans1 = 1e9;
dfs(1, 0);
dfs2(1, 0);
int cnt = 0;
int fa1 = id1;
for (int i = 0; i < q[id1].size(); i++) {
int y = q[id1][i];
if (vis[y])continue;
cnt++;
if (cnt == 1) {
id2 = y;
} else if (cnt == 2) {
id1 = y;
}
}
int q1 = js(id1, id2);
while (1) {
if (q1 == 0) {
if (cnt == 2) {
vis[fa1] = 1;
} else if (cnt == 1) {
vis[id2] = 1;
}
} else if (q1 == 2) {
if (cnt == 2) {
vis[fa1] = 1;
} else if (cnt == 1) {
vis[id1] = 1;
}
id1 = id2;
} else {
vis[id1] = 1, vis[id2] = 1;
id1 = fa1;
}
num = 0;
ans1 = 1e9;
dfs(id1, 0);
if (num == 1) {
cout << "!" << " " << id1 << endl;
return;
}
dfs2(id1, 0);
cnt = 0;
fa1 = id1;
for (int i = 0; i < q[id1].size(); i++) {
int y = q[id1][i];
if (vis[y])continue;
cnt++;
if (cnt == 1) {
id2 = y;
} else if (cnt == 2) {
id1 = y;
}
}
q1 = js(id1, id2);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3612kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 5 1 ? 4 2 ! 3 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 7ms
memory: 3556kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 2 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
output:
? 5 2 ? 7 2 ? 1 2 ! 1 ? 3 5 ? 4 1 ? 3 2 ! 2 ? 6 1 ? 6 1 ? 6 1 ? 6 1
result:
wrong answer Too many queries (test case 3)