QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#734575 | #9570. Binary Tree | _LSA_ | TL | 0ms | 8300kb | C++14 | 2.8kb | 2024-11-11 12:53:04 | 2024-11-11 12:53:10 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define fi first
#define se second
#define mk make_pair
using namespace std;
ll read(){
ll X = 0 ,r = 1;
char ch = getchar();
while(!isdigit(ch) && ch != '-') ch = getchar();
if(ch == '-') r = -1,ch = getchar();
while(isdigit(ch)) X = X*10+ch-'0',ch = getchar();
return X*r;
}
bool flag;
int ask(int x,int y){
cout << "? " << x << " " << y << endl;
return read();
}
void answer(int x){
cout << "! " << x << endl;
}
const int N = 1e5+10;
int n,TTT;
vector<int> G[N],E[N];
int siz[N],rt,S,mn;
bool vis[N];
void getroot(int x,int ft){
siz[x] = 1;
int mxpart = 0;
for(int y : E[x]){
if(y == ft || vis[y]) continue;
getroot(y,x);
siz[x] += siz[y];
mxpart = max(mxpart,siz[y]);
}
mxpart = max(mxpart,S-siz[x]);
if(mxpart <= mn) rt = x,mn = mxpart;
}
void rebuild(){
for(int i=1;i<=n;i++){
E[i].clear();
if(vis[i]) continue;
for(int y : G[i]) if(!vis[y]) E[i].push_back(y);
}
}
void solve(int x){
// cout << x << "\n";
rebuild();
vis[x] = true;
vector<int> v;
sort(E[x].begin(),E[x].end(),[](int x,int y){return siz[x] < siz[y];});
for(int y : E[x]){
rt = 0; S = mn = siz[y];
getroot(y,x);
assert(rt);
// cout << rt << " ";
v.push_back(rt);
}
// cout << "\n";
assert(v.size() <= 3);
if(v.size() == 3){
int a = E[x][2],b = E[x][1];
int t = ask(a,b);
if(t == 0) return vis[b] = vis[E[x][0]] = true,solve(v[2]);
if(t == 2) return vis[a] = vis[E[x][0]] = true,solve(v[1]);
vis[x] = false;
vis[a] = vis[b] = true;
return solve(v[0]);
}
if(v.size() == 1){
int y = E[x][0];
if(siz[y] == 1){
int t = ask(x,y);
if(t == 0) answer(x);
else answer(y);
return ;
}
vis[x] = false;
return solve(v[0]);
}
if(v.size() == 2){
int a = E[x][0],b = E[x][1];
int t = ask(a,b);
if(t == 0) return vis[b] = true,solve(v[0]);
if(t == 2) return vis[a] = true,solve(v[1]);
}
return answer(x);
}
void sol(){
n = read();
for(int i=1;i<=n;i++) G[i].clear(),vis[i] = false;
for(int i=1;i<=n;i++){
int u = read(),v = read();
if(u) G[i].push_back(u),G[u].push_back(i);
if(v) G[i].push_back(v),G[v].push_back(i);
}
rebuild();
mn = S = n; rt = 0;
getroot(1,0);
solve(rt);
}
int main(){
int T = read();
while(T--) sol();
return 0;
}
/*
1
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8300kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 1 3 ? 3 4 ! 3 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Time Limit Exceeded
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 0 0 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 1 0 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 0 5 3 0 5 1 0 0 0 0 4 0 2 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 1 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 ? 7 2 ? 8 7 ? 8 6 ! 6 ? 8 3 ? 8 2 ? 4 6 ! 4 ? 2 5 ? 4 1 ! 4 ? 5 7 ? 1 4 ! 1 ? 5 1 ? 1 3 ! 1 ? 1 4 ? 2 5 ! 5 ? 3 2 ! 1 ? 1 2 ! 2 ? 2 3 ! 3 ? 1 7 ? 7 3 ? 4 6 ! 4 ? 1 2 ! 1 ? 5 9 ? 5 8 ? 4 3 ! 3 ? 10 3 ? 8 2 ? 8 10 ! 8 ? 9 3 ? 9 7 ? 1 2 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 9 4 ? 8 4 ? ...