QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#883786 | #9570. Binary Tree | xueman | RE | 0ms | 3584kb | C++23 | 3.2kb | 2025-02-05 18:52:12 | 2025-02-05 18:52:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define endl '\n'
#define ll long long
const int N = 5e5 + 10;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
struct edge
{
int to, ne;
} e[N];
int head[maxn], ecnt = 1;
void add(int x, int y)
{
e[++ecnt].to = y;
e[ecnt].ne = head[x];
head[x] = ecnt;
}
int n, sum;
int sz[maxn];
int ans, pos;
void dfs1(int x, int fa, int d1, int d2)
{
sz[x] = 1;
int res = 0;
for (int i = head[x]; i; i = e[i].ne)
{
int y = e[i].to;
if (y != fa && y != d1 && y != d2)
{
dfs1(y, x, d1, d2);
sz[x] += sz[y];
res = max(res, sz[y]);
}
res = max(res, n - sz[x]);
// cout << res << ' ';
if (res < ans || (ans == res && x < pos))
ans = res, pos = x;
}
}
void dfs2(int x, int fa, int d1, int d2)
{
sz[x] = 1;
for (int i = head[x]; i; i = e[i].ne)
{
int y = e[i].to;
if (y == fa || y == d1 || y == d2)
continue;
dfs2(y, x, d1, d2);
sz[x] += sz[y];
}
}
int ask(int x, int y)
{
cout << "? " << x << ' ' << y << endl;
int tmp;
cin >> tmp;
return tmp;
}
void solve(int now, int d1, int d2 = 0)
{
ans = inf, pos = 0;
dfs1(now, 0, d1, d2);
// cout << '(' << pos << ')' << sz[pos] << endl;
dfs2(pos, 0, d1, d2);
vector<pair<int, int>> v;
for (int i = head[pos]; i; i = e[i].ne)
{
int y = e[i].to;
if (y == d1 || y == d2)
continue;
v.push_back({sz[y], y});
}
sort(v.begin(), v.end(), greater<pair<int, int>>());
if (v.size() == 3)
{
int op = ask(v[0].second, v[1].second);
if (op == 0)
solve(v[0].second, pos);
else if (op == 1)
solve(v[2].second, v[0].second, v[1].second);
else
solve(v[1].second, pos);
}
else if (v.size() == 2)
{
int op = ask(v[0].second, v[1].second);
if (op == 0)
solve(v[0].second, pos);
else if (op == 1)
{
sum = pos;
return;
}
else
solve(v[1].second, pos);
}
else if (v.size() == 1)
{
int op = ask(pos, v[0].second);
if (op == 0)
{
sum = pos;
return;
}
else
{
sum = v[0].second;
return;
}
}
else
{
cout << "RE" << endl;
assert(false);
}
}
int main()
{
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int T;
cin >> T;
while (T--)
{
cin >> n;
for (int i = 0; i <= n; i++)
head[i] = 0;
ecnt = 1;
for (int i = 1; i <= n; i++)
{
int x, y;
cin >> x >> y;
if (x)
add(x, i), add(i, x);
if (y)
add(y, i), add(i, y);
}
if (n == 1)
{
cout << "! 1" << endl;
continue;
}
sum = 0;
solve(1, 0);
cout << "! " << sum << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 5 ? 1 2 ! 1 ? 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 0 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 0 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 0 2 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0
output:
? 8 6 ? 4 5 ? 3 4 ! 4 ? 7 2 ? 8 7 ? 8 6 ! 8 ? 1 6 ? 3 5 ? 3 7 ! 7 ? 2 5 ? 2 3 ! 3 ? 5 7 ? 4 1 RE