QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#747191 | #9570. Binary Tree | ucup-team1769# | RE | 0ms | 0kb | C++14 | 1.3kb | 2024-11-14 16:32:46 | 2024-11-14 16:32:47 |
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n;
cin >> n;
set<int> st;
for (int i = 1; i <= n; i++)
st.insert(i);
vector<set<int>> g(n + 10, set<int>());
for (int i = 1; i <= n; i++)
{
int l, r;
cin >> l >> r;
if (l)
{
g[i].insert(l);
g[l].insert(i);
st.erase(l);
}
if (r)
{
g[i].insert(r);
g[r].insert(i);
st.erase(r);
}
}
if (st.empty())
{
cout << "! 1" << endl;
return;
}
int root = *st.begin();
if (g[root].empty())
{
cout << "! 1" << endl;
return;
}
int u = root, v = *(g[root].begin());
while (1)
{
cout << "? " << u << ' ' << v << endl;
int t;
cin >> t;
if (t == 2)
swap(u, v);
g[u].erase(v);
g[v].erase(u);
if (g[u].empty())
{
cout << "! " << u << endl;
return;
}
v = *(g[u].begin());
}
}
signed 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: 0
Runtime Error
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0
output:
? 3 2 ? 2 1 ? 2 5