QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#729036 | #9570. Binary Tree | ucup-team4975 | RE | 1ms | 3788kb | C++23 | 3.2kb | 2024-11-09 16:23:57 | 2024-11-09 16:23:59 |
Judging History
answer
#define LOCAL
#include <bits/stdc++.h>
#define fir first
#define sec second
#define el '\n'
#ifdef LOCAL
#define FINISH cerr << "FINISH" << endl;
#else
#define FINISH ;
#endif
#ifdef LOCAL
#define debug(x) cerr << setw(4) << #x << " == " << x << endl
#else
#define debug(x)
#endif
#ifdef LOCAL
#define debugv(x) \
cerr << setw(4) << #x << ":: "; \
for (auto i : x) \
cerr << i << " "; \
cerr << endl
#else
#define debugv(x)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ostream& operator<<(ostream& out, PII& x)
{
out << x.fir << " " << x.sec << endl;
return out;
}
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int N = 200020;
void solve()
{
int n;
cin >> n;
vector<vector<int>> edge(n + 1);
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
if (x != 0) {
edge[i].push_back(x);
edge[x].push_back(i);
}
if (y != 0) {
edge[y].push_back(i);
edge[i].push_back(y);
}
}
int rt = 1, lft = n;
// FINISH
while (1) {
vector<int> siz(n + 1, 0);
int newrt = -1, s = inf;
vector<int> w(n + 1, 0);
w[newrt = 0] = inf;
auto dfs = [&](auto& self, int x, int fa) -> void {
/*debug(x);
debug(fa);*/
siz[x] = 1;
for (auto to : edge[x]) {
if (to == fa)
continue;
self(self, to, x);
siz[x] += siz[to];
w[x] = max(w[x], siz[to]);
}
w[x] = max(w[x], lft - siz[x] + 1);
if (w[x] < w[newrt])
newrt = x;
};
// cout << rt << " " << lft << endl;
dfs(dfs, rt, rt);
// debug(newrt);
int q1 = 0, q2 = 0;
if (edge[newrt].size() == 0) {
cout << "! " << newrt << endl;
return;
}
if (edge[newrt].size() == 1) {
q1 = newrt, q2 = edge[newrt][0];
cout << "? " << q1 << " " << q2 << endl;
int x;
cin >> x;
if (x == 0) {
cout << "! " << q1 << endl;
}
if (x == 2) {
cout << "! " << q2 << endl;
}
return;
}
else {
vector<int> vis(n + 1, 0);
q1 = edge[newrt][0], q2 = edge[newrt][1];
cout << "? " << q1 << " " << q2 << endl;
int x;
cin >> x;
queue<int> q;
lft = 0;
if (x == 1) {
if (edge[newrt].size() == 2) {
cout << "! " << newrt << endl;
return;
}
q.push(edge[newrt][2]);
vis[q1] = vis[q2] = 1;
newrt = edge[newrt][2];
lft = -2;
}
if (x == 0) {
// q1
q.push(q1);
vis[newrt] = 1;
newrt = q1;
lft = -1;
}
if (x == 2) {
q.push(q2);
vis[newrt] = 1;
newrt = q2;
lft = -1;
}
vector<vector<int>> edge1(n + 1);
while (!q.empty()) {
int x = q.front();
// debug(x);
q.pop();
vis[x] = 1;
for (auto to : edge[x]) {
if (vis[to])
continue;
q.push(to);
edge1[x].push_back(to);
edge1[to].push_back(x);
}
}
edge.clear();
edge = edge1;
rt = newrt;
for (int i = 1; i <= n; i++) {
lft += vis[i];
}
}
// FINISH
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
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: 3788kb
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
Runtime Error
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 2 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 1 2 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 0 2 5 3 0 5 1 0 0 0 0 4 0 0 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 1 8 ? 5 4 ? 4 3 ! 4 ? 2 7 ? 7 8 ? 8 6 ! 8 ? 5 8 ? 8 4 ? 2 6 ! 6 ? 4 5 ? 3 1 ! 3 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 5 4 ! 5 ? 1 2 ? 3 5 ! 5 ? 3 2 ! 2 ? 1 2 ! 2 ? 2 3 ! 3 ? 1 9 ? 2 6 ? 4 3 ! 4 ? 1 2 ! 1 ? 5 9 ? 2 1 ? 2 6 ! 2 ? 3 10 ? 8 6 ? 8 10 ! 10 ? 3 4 ? 9 1 ? 7 2 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 4 8 ?...