QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#747587 | #9570. Binary Tree | ucup-team1769# | TL | 1ms | 3500kb | C++14 | 3.8kb | 2024-11-14 17:36:12 | 2024-11-14 17:36:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
vector<int> fa(n + 1), siz(n + 1), ans(n + 1), weight(n + 1);
vector<set<int>> g(n + 10, set<int>());
set<int> st;
for (int i = 1; i <= n; i++)
st.insert(i);
for (int i = 1; i <= n; i++)
{
int l, r;
cin >> l >> r;
if (l)
{
fa[l] = i;
g[i].insert(l);
g[l].insert(i);
st.erase(l);
}
if (r)
{
fa[r] = i;
g[i].insert(r);
g[r].insert(i);
st.erase(r);
}
}
auto dfs = [&](auto dfs, int u) -> void
{
siz[u] = 1, ans[u] = u;
for (int v : g[u])
{
if (v == fa[u])
continue;
dfs(dfs, v);
siz[u] += siz[v];
weight[u] = max(weight[u], siz[v]);
}
for (int v : g[u])
{
if (v == fa[u])
continue;
int p = ans[v];
while (p != u)
{
if (max(weight[p], siz[u] - siz[p]) <= siz[u] / 2)
{
ans[u] = p;
break;
}
else
p = fa[p];
}
}
};
int root = *st.begin();
dfs(dfs, root);
root = ans[root];
auto dfs2 = [&](auto dfs2, int u) -> void
{
siz[u] = 1, ans[u] = u;
for (int v : g[u])
{
if (v == fa[u])
continue;
dfs2(dfs2, v);
siz[u] += siz[v];
weight[u] = max(weight[u], siz[v]);
}
};
dfs2(dfs2, root);
int u = root, v = *g[root].begin();
for (auto tmp : g[u])
{
if (siz[tmp] > siz[v])
v = tmp;
}
// for (int i = 1; i <= n; i++)
// cout << g[i].size() << ' ';
// cout << endl;
int father = -1, flag = false;
while (1)
{
cout << "? " << u << ' ' << v << '\n';
fflush(stdout);
int t;
cin >> t;
if (t == 0)
{
if (flag)
{
v = father;
}
g[u].erase(v);
g[v].erase(u);
if (g[u].size() == 2)
{
father = u;
v = *g[u].rbegin();
u = *g[u].begin();
flag = true;
}
else if (g[u].size() == 1)
{
v = *g[u].begin();
flag = false;
}
else
{
cout << "! " << u << '\n';
fflush(stdout);
return;
}
}
else if (t == 1)
{
cout << "! " << father << '\n';
fflush(stdout);
return;
}
else if (t == 2)
{
swap(u, v);
if (flag)
{
v = father;
}
g[u].erase(v);
g[v].erase(u);
if (g[u].size() == 2)
{
father = u;
v = *g[u].rbegin();
u = *g[u].begin();
flag = true;
}
else if (g[u].size() == 1)
{
v = *g[u].begin();
flag = false;
}
else
{
cout << "! " << u << '\n';
fflush(stdout);
return;
}
}
}
}
int main()
{
// cin.tie(0)->ios::sync_with_stdio(false);
int t = 1;
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: 3500kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 0 1 2 0 2 0 0 2
output:
? 2 3 ? 1 5 ! 2 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 2 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 2 1 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 2 0 5 4 5 3 1 0 0 0 0 0 0 0 1 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0 2 5 3 0 5 1 0 0 0 0 4 0 0 2
output:
? 8 2 ? 1 6 ? 6 7 ! 7 ? 3 7 ? 3 2 ? 1 4 ! 2 ? 8 1 ? 8 6 ? 2 4 ! 2 ? 1 2 ? 4 5 ! 1 ? 3 7 ? 5 6 ? 1 4 ! 4 ? 2 1 ? 2 5 ? 5 4