QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#753307 | #9570. Binary Tree | ucup-team4906# | WA | 0ms | 3796kb | C++20 | 2.6kb | 2024-11-16 12:16:40 | 2024-11-16 12:16:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using vi = vector<int>;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define Sz(x) ((int)(x).size())
typedef long long ll;
typedef unsigned long long ull;
int n;
int query(int x, int y)
{
cout << "? " << x << " " << y << endl;
int res;
cin >> res;
return res;
}
void ans(int x)
{
cout << "! " << x << endl;
}
const int N = 1e5 + 2;
vi e[N];
int sz[N], del[N], maxs[N];
void divn(int u, int s)
{
if (s == 1)
{
ans(u);
return;
}
int ms = s + 1, root = -1;
function<void(int, int)> center = [&](int u ,int fa) {
sz[u] = 1;
maxs[u] = 0;
for (auto v : e[u]) {
if (del[v] || v == fa) continue;
center(v, u);
maxs[u] = max(maxs[u], sz[v]);
sz[u] += sz[v];
}
maxs[u] = max(maxs[u], s - sz[u]);
if (maxs[u] < ms) ms = maxs[u], root = u;
};
center(u, -1);
cerr << root << "\n";
vector<array<int, 2>> son;
for (auto v : e[root]) {
if (del[v]) continue;
function<void(int, int)> dfs = [&](int u, int fa) {
sz[u] = 1;
for (auto v : e[u]) {
if (del[v] || v == fa) continue;
dfs(v, u);
sz[u] += sz[v];
}
};
dfs(v, root);
son.pb({sz[v], v});
}
sort(all(son), greater<>());
int m = Sz(son);
assert(m);
if (m == 1)
{
auto [cnt, v] = son[0];
assert(cnt == 1);
int res = query(root, v);
assert(res != 1);
if (res == 0) ans(root);
else ans(v);
}
else
{
auto [c1, v1] = son[0];
auto [c2, v2] = son[1];
int res = query(v1, v2);
if (res == 1)
{
del[v1] = del[v2] = 1;
divn(root, s - sz[v1] - sz[v2]);
}
else
{
del[root] = 1;
if (res == 0)
divn(v1, sz[v1]);
else
divn(v2, sz[v2]);
}
}
}
void sol()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
int x, y;
cin >> x >> y;
if (x)
{
e[i].pb(x);
e[x].pb(i);
}
if (y)
{
e[i].pb(y);
e[y].pb(i);
}
}
divn(1, n);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T --) sol();
return 0;
}
/*
1
5
0 0
1 5
2 4
0 0
0 0
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3796kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0
output:
? 3 5 ? 1 2 ! 1 ? 1 1
result:
wrong answer Query two identical vertices 1 (test case 2)