QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#779164 | #9570. Binary Tree | Clarus | RE | 1ms | 3788kb | C++20 | 3.5kb | 2024-11-24 17:45:32 | 2024-11-24 17:45:32 |
Judging History
answer
#include <bits/stdc++.h>
using LL = long long;
constexpr int inf = 1E9;
int ask(int a, int b)
{
std::cout << "? " << a + 1 << ' ' << b + 1 << std::endl;
int x;
std::cin >> x;
return x;
}
void put(int x)
{
std::cout << "! " << x + 1 << std::endl;
}
void solve()
{
int n;
std::cin >> n;
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < n; i++)
{
int v1, v2;
std::cin >> v1 >> v2;
v1--, v2--;
if (v1 >= 0)
{
adj[i].emplace_back(v1);
adj[v1].emplace_back(i);
}
if (v2 >= 0)
{
adj[i].emplace_back(v2);
adj[v2].emplace_back(i);
}
}
std::vector<int> vis(n), size(n);
int cent = -1, root = 0;
int m = -1;
auto dfs1 = [&](auto &&self, int x, int fx) -> int
{
size[x] = 1;
for (auto y : adj[x])
{
if (vis[y] || y == fx)
{
continue;
}
size[x] += self(self, y, x);
}
return size[x];
};
auto dfs2 = [&](auto &&self, int x, int fx) -> void
{
int val = 0;
for (auto y : adj[x])
{
if (vis[y] || y == fx)
{
continue;
}
self(self, y, x);
val = std::max(val, size[y]);
}
val = std::max(val, m - size[x]);
if (val <= m / 2)
{
cent = x;
}
};
while (true)
{
m = dfs1(dfs1, root, -1);
dfs2(dfs2, root, -1);
std::vector<int> e;
for (auto y : adj[cent])
{
if (vis[y])
{
continue;
}
e.emplace_back(y);
}
if (e.size() == 0)
{
put(root);
return;
}
else if (e.size() == 1)
{
int q = ask(cent, e[0]);
assert(q != 1);
if (q == 0)
{
root = cent;
vis[e[0]] = true;
}
else
{
root = e[0];
vis[cent] = true;
}
}
else
{
if (e.size() == 3)
{
std::array<int, 3> r = {0, 1, 2};
std::array<int, 3> s = {size[e[0]], size[e[1]], size[e[2]]};
int i = std::max_element(s.begin(), s.end()) - s.begin();
s[i] = m - s[i] + 1;
std::sort(r.begin(), r.end(),
[&](int i, int j)
{
return s[i] < s[j];
});
auto _e = e;
_e[0] = e[r[0]];
_e[1] = e[r[1]];
e = _e;
}
int q = ask(e[0], e[1]);
if (q == 0)
{
root = e[0];
vis[cent] = true;
}
else if (q == 2)
{
root = e[1];
vis[cent] = true;
}
else
{
root = cent;
vis[e[0]] = true;
vis[e[1]] = true;
}
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
while (T--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3788kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 1 5 ? 2 4 ! 3 ? 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 1 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 2 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 1 1 1 5 4 5 3 1 0 0 0 0 0 0 1 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 1 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 1 1 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 1 10 2 8 9 7 0 0 ...
output:
? 1 6 ? 2 5 ? 3 8 ! 4 ? 2 7 ? 7 8 ? 8 6 ! 8 ? 5 3 ? 4 2 ? 1 6 ! 8 ? 4 5 ? 3 1 ! 3 ? 5 6 ? 1 4 ! 5 ? 5 1 ? 5 4 ! 4 ? 1 2 ? 3 5 ! 4 ? 3 2 ! 2 ? 1 2 ! 2 ? 2 3 ! 1 ? 1 9 ? 2 5 ? 4 3 ? 6 7