QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#735505 | #9570. Binary Tree | wifi32767 | RE | 0ms | 0kb | C++20 | 2.7kb | 2024-11-11 20:25:35 | 2024-11-11 20:25:36 |
answer
#include<bits/stdc++.h>
#include <exception>
using namespace std;
// #define endl '\n'
using ll = long long;
const int MAX = 3e5 + 5;
void solve(){
int n; cin >> n;
vector<set<int>> graph(n + 1);
for (int i = 1; i <= n; i ++){
int x, y; cin >> x >> y;
if (x) graph[x].insert(i), graph[i].insert(x);
if (y) graph[y].insert(i), graph[i].insert(y);
}
int t = n, cur = 1;
vector<int> siz(n + 1);
function<void(int, int)> dfs1 = [&](int u, int p){
siz[u] = 1;
for (int v : graph[u]){
if (v == p) continue;
dfs1(v, u);
siz[u] += siz[v];
}
};
int p = 0;
function<void(int, int)> dfs2 = [&](int u, int p){
int maxx = t - siz[u];
for (int v : graph[u]){
maxx = max(maxx, siz[v]);
if (v == p) continue;
dfs2(v, u);
}
if (maxx <= t / 2) p = u;
};
dfs1(1, 0);
while (t > 1){
dfs2(cur, 0);
if (graph[cur].size() == 3){
array<int, 3> a;
auto it = graph[cur].begin();
for (int j = 0; j < 3; j ++){
a[j] = *it;
it ++;
}
cout << "? " << a[0] << ' ' << a[2] << endl;
int x; cin >> x;
graph[cur].erase(a[0]);
graph[cur].erase(a[2]);
graph[a[0]].erase(cur);
graph[a[2]].erase(cur);
cur = a[x];
}
else if (graph[cur].size() == 2){
array<int, 2> a;
auto it = graph[cur].begin();
for (int j = 0; j < 2; j ++){
a[j] = *it;
it ++;
}
cout << "? " << a[0] << ' ' << a[1] << endl;
int x; cin >> x;
graph[cur].erase(a[0]);
graph[cur].erase(a[1]);
graph[a[0]].erase(cur);
graph[a[1]].erase(cur);
if (x == 0) cur = a[0];
else if (x == 2) cur = a[1];
else{
cout << "! " << cur << endl;
return;
}
}
else{
auto i = *graph[cur].begin();
cout << "? " << i << ' ' << cur << endl;
int x; cin >> x;
// assert(x != 1);
if (x == 0) cout << "! " << i << endl;
else cout << "! " << cur << endl;
return;
}
dfs1(cur, 0);
t = siz[cur];
}
cout << "! " << cur << endl;
}
signed main(){
// ios::sync_with_stdio(0);cin.tie(0);
// cout << fixed << setprecision(10);
int T;cin>>T;while (T--)
solve();
}
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 0
output:
? 2 1 ! 2