QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#758064 | #9570. Binary Tree | leafmaple# | ML | 1ms | 3564kb | C++20 | 2.3kb | 2024-11-17 15:28:37 | 2024-11-17 15:28:38 |
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);
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();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3564kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 1 3 ? 4 3 ! 4 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Memory Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 1
output:
? 2 4 ? 1 8 ! 2