QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#765071 | #9570. Binary Tree | jdyt11 | WA | 7ms | 7700kb | C++23 | 2.5kb | 2024-11-20 11:54:37 | 2024-11-20 11:54:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
#define pll pair<ll,ll>
#define ls d*2
#define rs d*2+1
#define mid (l+r)/2
#define lowbit(x) (x&(-x))
//#define endl "\n"
#define all(x) x.begin(),x.end()
//#define int long long
//mt19937 seed;
//uniform_int_distribution<int>num(0,2e9);
const int N=1e6+10;
const int M=33;
int q(int u,int v){
cout<<"? "<<u<<" "<<v<<endl;
int x;
cin>>x;
return x;
}
int top=1;
vector<int>e[N];
int sz[N],f[N],dep[N];
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
sz[u]=1;
f[top]+=dep[u]-dep[top];
for(int v:e[u])if(v!=fa)dfs(v,u),sz[u]+=sz[v];
}
void dfs2(int u,int fa){
for(int v:e[u])if(v!=fa)f[v]=f[u]+sz[top]-2*sz[v],dfs2(v,u);
}
bool cmp(int u,int v){
return sz[u]>sz[v];
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int _=1;cin>>_;
while(_--){
int n;
cin>>n;
for(int i=1;i<=n;i++)e[i].clear(),f[i]=sz[i]=dep[i]=0;
for(int i=1;i<=n;i++){
int l,r;
cin>>l>>r;
if(l)e[i].push_back(l),e[l].push_back(i);
if(r)e[i].push_back(r),e[r].push_back(i);
}
int tot=n;
top=1;
while(tot>1){
if(tot==2){
int u=top,v=e[top][0];
int res=q(u,v);
if(res==0)top=u;
else top=v;
break;
}
dfs(top,0);
dfs2(top,0);
int tt=1;
for(int i=2;i<=n;i++)if(f[i]<f[tt])tt=i;
dfs(tt,0);
sort(all(e[tt]),cmp);
int u=e[tt][0],v=e[tt][1];
int res=q(u,v);
if(res==0){
vector<int>temp;
for(int a:e[u])if(a!=tt)temp.push_back(a);
swap(temp,e[u]);
top=u;
tot=sz[u];
}
else if(res==1){
vector<int>temp;
for(int a:e[tt])if(a!=u&&a!=v)temp.push_back(a);
swap(temp,e[tt]);
tot=sz[tt]-sz[u]-sz[v];
top=tt;
}
else{
vector<int>temp;
for(int a:e[v])if(a!=tt)temp.push_back(a);
swap(temp,e[v]);
tot=sz[v];
top=v;
}
for(int i=1;i<=n;i++)sz[i]=f[i]=dep[i]=0;
}
cout<<"! "<<top<<endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 7700kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 1 ? 2 5 ! 2 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 7ms
memory: 7660kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0
output:
? 8 6 ? 2 0
result:
wrong answer Integer parameter [name=v] equals to 0, violates the range [1, 8] (test case 1)