QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#758070 | #9570. Binary Tree | leafmaple# | WA | 1ms | 3572kb | C++20 | 2.6kb | 2024-11-17 15:33:06 | 2024-11-17 15:33:06 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+5;
vector<int>g[N];
bool del[N];
int mxs[N], sz[N];
void solve(int u, int siz){
int root = -1, ms = siz + 1;
if(siz == 1){
cout << "! " << u << endl;
return ;
}
function<void(int, int)> dfs1 = [&](int u, int fa){
sz[u] = 1; mxs[u] = 0;
for(auto v: g[u])if(v!=fa && !del[v]){
dfs1(v, u);
sz[u] += sz[v];
mxs[u] = max(mxs[u], sz[v]);
}
mxs[u] = max(mxs[u], siz - sz[u]);
if(mxs[u] < ms) ms = mxs[u], root = u;
};
dfs1(u, -1);
function<void(int, int)> dfs2 = [&](int u, int fa){
sz[u] = 1;
for(auto v: g[u])if(v!=fa && !del[v]){
dfs2(v, u);
sz[u] += sz[v];
}
};
dfs2(root, -1);
int du = 0;
for(auto v: g[root])if(!del[v])du++;
if(du == 3){
sort(g[root].begin(), g[root].end(), [&](int l, int r){
return sz[l] > sz[r];
});
int x = g[root][0], y = g[root][1];
cout << "? " << x << ' ' << y << endl;
int fk; cin >> fk;
if(fk == 1){
del[x] = del[y] = 1;
solve(g[root][2], sz[g[root][2]] + 1);
}else if(fk == 0){
del[root] = 1;
solve(x, sz[x]);
}else {
del[root] = 1;
solve(y, sz[y]);
}
return ;
}else if(du == 2){
int x = g[root][0], y = g[root][1];
cout << "? " << x << ' ' << y << endl;
int fk; cin >> fk;
if(fk == 1){
cout << "! " << root << endl;
return ;
}else if(fk == 0){
del[root] = 1;
solve(x, sz[x]);
}else {
del[root] = 1;
solve(y, sz[y]);
}
}else {
int x = g[root][0];
cout << "? " << root << ' ' << x << endl;
int fk; cin >> fk;
if(fk == 0){
cout << "! " << root << endl;
return ;
}else {
cout << "! " << x << endl;
return ;
}
}
}
void solve(){
int n; cin >> n;
for(int i=1; i<=n; i++) g[i].clear(), del[i] = 0;
for(int i=1; i<=n; i++){
int u, v; cin >> u >> v;
if(u){
g[i].push_back(u);
g[u].push_back(i);
}
if(v){
g[i].push_back(v);
g[v].push_back(i);
}
}
solve(1, n);
}
signed main (){
int t;
cin>>t;
while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3572kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0
output:
? 3 1 ? 2 3 ! 2
result:
wrong answer There are 2 candidates. (test case 1)