QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#748682 | #9570. Binary Tree | SkyEyeController | WA | 2ms | 3632kb | C++23 | 3.4kb | 2024-11-14 21:03:59 | 2024-11-14 21:04:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 mod = 998244353;
vector<vector<int>> con;
vector<int> ctr; // 重心
const int maxn = 2e5 + 9;
int n, m, allsz;
int sz[maxn], mss[maxn], fa[maxn];
int disjfa[maxn];
void init(int n)
{
for (int i = 1; i <= n; i++)
disjfa[i] = i;
}
int find(int x)
{
return disjfa[x] == x ? x : disjfa[x] = find(disjfa[x]);
}
void merge(int x, int y)
{
x = find(x), y = find(y);
if (x != y)
disjfa[x] = y;
}
void dfs(int u, int f, bool tag)
{
fa[u] = f;
sz[u] = 1, mss[u] = 0;
if (tag)
{
auto it = find(con[u].begin(), con[u].end(), f);
if (it != con[u].end())
con[u].erase(it);
}
if (!con[u].empty())
for (auto v : con[u])
{
dfs(v, u, tag);
sz[u] += sz[v];
mss[u] = max(mss[u], sz[v]);
}
mss[u] = max(mss[u], allsz - sz[u]);
if (mss[u] <= allsz / 2)
{
ctr.push_back(u);
}
} // 搜重心
void solve()
{
cin >> n;
init(n);
con.assign(n + 1, vector<int>());
ctr.clear();
int rt = -1;
for (int i = 1; i <= n; i++)
{
int l, r;
cin >> l >> r;
if (l)
{
con[i].push_back(l);
merge(l, i);
}
if (r)
{
con[i].push_back(r);
merge(r, i);
}
}
for (int i = 1; i <= n; i++)
if (find(i) == i)
{
rt = i;
break;
}
function<int(int, int)> ask = [&](int l, int r) -> int
{
cout << "? " << l << " " << r << '\n';
cout.flush();
int x;
cin >> x;
return x;
};
allsz = n;
dfs(rt, 0, 1);
int mid = ctr[0];
while (sz[rt] > 1)
{
if (con[mid].size() == 2)
{
int l = con[mid][0], r = con[mid][1];
int sig = ask(l, r);
if (sig == 0)
{
ctr.clear();
allsz = sz[l];
dfs(rt = l, 0, 0);
mid = ctr[0];
}
else if (sig == 2)
{
ctr.clear();
allsz = sz[r];
dfs(rt = r, 0, 0);
mid = ctr[0];
}
else
{
allsz -= (sz[l] + sz[r]);
ctr.clear();
con[mid].clear();
dfs(rt, 0, 0);
mid = ctr[0];
}
}
else
{
int l = mid, r = fa[mid];
int sig = ask(l, r);
if (sig == 0)
{
ctr.clear();
allsz = sz[l];
dfs(rt = l, 0, 0);
mid = ctr[0];
}
else
{
auto it = find(con[r].begin(), con[r].end(), l);
if (it != con[r].end())
con[r].erase(it);
ctr.clear();
allsz -= sz[l];
dfs(rt, 0, 0);
mid = ctr[0];
}
}
}
cout << "! " << rt << " " << '\n';
cout.flush();
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3632kb
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 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 3616kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 1 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 0 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 1 0 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 1 0 5 3 0 5 1 0 0 0 0 4 0 2 2 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 0
output:
? 5 4 ? 8 6 ? 7 6 ! 7 ? 3 7 ? 8 5 ? 6 8 ! 8 ? 8 1 ? 4 2 ? 6 8 ! 6 ? 4 5 ? 3 1 ! 3 ? 5 6 ? 3 8 ! 3 ? 5 1 ? 3 1 ! 1 ? 2 4 ? 5 1 ! 1 ? 1 2 ? 3 1
result:
wrong answer Too many queries (test case 8)