QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#791856 | #9570. Binary Tree | Young_Cloud | WA | 2ms | 5964kb | C++17 | 3.0kb | 2024-11-28 21:23:28 | 2024-11-28 21:23:28 |
Judging History
answer
#include<bits/stdc++.h>
#ifdef LOCAL_MACHINE
#define debug(...) printf(__VA_ARGS__)
#define sp() system("pause")
#define IOS
#else
#define debug(...)
#define sp()
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#endif
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
constexpr i64 Mod = 998244353;
constexpr int N = 1e5, M = 1e5, Inf = 1e9;
std::vector<int> g[N + 5], siz(N + 5);
bool vis[N + 5];
int pa;
void init(int n) {
for (int i = 1; i <= n; i++) {
g[i].clear();
vis[i] = false;
}
}
// 找到包括cur在内的那颗树的重心
void find(int cur, int fa, int &gra, int &n) {
siz[cur] = 1;
int mx = 0;
for (auto &to : g[cur]) {
if (to == fa || vis[to]) {
continue;
}
find(to, cur, gra, n);
siz[cur] += siz[to];
mx = std::max(mx, siz[to]);
}
// 最后还要考虑以 fa 为根的子树的大小
mx = std::max(mx, n - siz[cur]);
if (mx * 2 <= n && gra == 0) {
gra = cur;
pa = fa;
}
}
int ask(int u, int v) {
std::cout << "? " << u << ' ' << v << std::endl;
int t = -1;
std::cin >> t;
return t;
}
void solve()
{
int n = 0;
std::cin >> n;
init(n);
for (int i = 1; i <= n; i++) {
int u = 0, v = 0;
std::cin >> u >> v;
if (u != 0) {
g[i].push_back(u);
g[u].push_back(i);
}
if (v != 0) {
g[v].push_back(i);
g[i].push_back(v);
}
}
int ver = 1, gra = 0;
int ans = 0;
while (ans == 0) {
find(ver, 0, gra, n);
std::vector<int> son;
for (auto &to : g[gra]) {
if (not vis[to]) {
son.push_back(to);
}
}
if (son.size() == 0) {
ans = gra;
}
else if (son.size() == 1) {
ans = ask(son[0], gra) == 0 ? son[0] : gra;
}
else {
siz[pa] = n - siz[gra];
// std::sort(son.begin(), son.end(), [&](int &x, int &y) {
// return siz[x] > siz[y];
// });
int t = ask(son[0], son[1]);
if (t == 1) {
ver = gra;
vis[son[0]] = true;
vis[son[1]] = true;
if (son.size() == 2) {
ans = gra;
}
else {
n = siz[son[2]] + 1;
}
}
else {
ver = son[t != 0];
n = siz[son[t != 0]];
vis[gra] = true;
}
gra = 0;
}
pa = 0;
}
std::cout << "! " << ans << std::endl;
}
int main()
{
IOS;
int _t = 1;
std::cin >> _t;
while (_t--)
{
solve();
}
sp();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5964kb
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
Wrong Answer
time: 0ms
memory: 5944kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 0 2 5 4 5 3 1 0 0 0 0 0 0 1 2 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0 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 0 3 3 0 1 0 0 0 2 2 2 0 0 0 0 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 2 5 ? 2 7 ? 2 1 ! 1 ? 5 3 ? 1 4 ? 2 3 ! 2 ? 1 6 ? 1 7 ? 1 5 ! 5 ? 4 5 ? 3 1 ! 1 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 5 4 ! 4 ? 1 2 ? 3 5 ! 3 ? 3 2 ! 2 ? 1 2 ! 1 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 9 10 ! 10 ? 1 2 ! 1 ? 5 9 ? 4 8 ? 3 5 ! 3 ? 5 8 ? 7 1 ? 3 5 ! 3 ? 3 4 ? 1 7 ? 8 2 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 2 3 ? ...
result:
wrong answer Too many queries (test case 90)