QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#765089 | #9570. Binary Tree | jdyt11 | RE | 202ms | 19056kb | C++23 | 2.5kb | 2024-11-20 12:03:10 | 2024-11-20 12:03:11 |
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];
int tt=1;
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){
if(f[u]<f[tt])tt=u;
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=tt=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);
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;
tt=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;
tt=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: 1ms
memory: 7664kb
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: 0
Accepted
time: 60ms
memory: 8304kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 0 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 1 2 5 4 5 3 1 0 0 0 0 0 0 0 2 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 0 2 5 5 0 0 0 0 0 3 0 2 4 0 0 3 3 0 1 0 0 0 2 2 2 0 0 0 0 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 8 6 ? 4 5 ? 4 3 ! 3 ? 7 2 ? 8 7 ? 8 6 ! 8 ? 8 3 ? 4 2 ? 6 8 ! 8 ? 2 4 ? 2 3 ! 3 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 5 4 ! 4 ? 4 1 ? 4 3 ! 4 ? 3 2 ! 2 ? 1 2 ! 1 ? 2 3 ! 3 ? 7 1 ? 7 4 ? 6 3 ! 3 ? 1 2 ! 1 ? 5 9 ? 4 8 ? 3 5 ! 3 ? 10 3 ? 8 6 ? 4 2 ! 4 ? 9 3 ? 1 7 ? 2 9 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 8 4 ? 8...
result:
ok OK (5555 test cases)
Test #3:
score: 0
Accepted
time: 37ms
memory: 7708kb
input:
600 2 2 0 0 0 2 3 2 0 3 0 0 0 2 4 4 0 1 0 0 0 3 0 0 0 5 4 0 0 0 1 0 2 0 3 0 2 0 6 4 0 6 0 2 0 5 0 0 0 1 0 0 0 7 7 0 3 0 6 0 5 0 2 0 1 0 0 0 0 1 8 7 0 0 0 2 0 8 0 1 0 5 0 3 0 6 0 0 0 0 9 7 0 4 0 2 0 1 0 0 0 8 0 9 0 5 0 6 0 2 0 2 10 9 0 6 0 8 0 7 0 0 0 10 0 2 0 4 0 5 0 1 0 0 0 2 11 2 0 10 0 6 0 9 0 0 ...
output:
? 1 2 ! 2 ? 1 3 ! 3 ? 4 2 ? 4 3 ! 4 ? 4 3 ? 3 5 ! 3 ? 6 4 ? 6 3 ! 6 ? 2 6 ? 4 2 ! 5 ? 5 7 ? 8 5 ? 8 4 ! 8 ? 1 9 ? 8 9 ? 8 5 ! 5 ? 2 10 ? 7 8 ? 7 2 ! 2 ? 2 9 ? 8 4 ? 4 9 ! 9 ? 6 1 ? 4 2 ? 4 11 ! 4 ? 2 3 ? 5 9 ? 5 11 ! 6 ? 12 9 ? 11 8 ? 11 12 ! 12 ? 2 14 ? 15 8 ? 8 13 ! 13 ? 13 8 ? 14 10 ? 12 14 ? 12 ...
result:
ok OK (600 test cases)
Test #4:
score: 0
Accepted
time: 202ms
memory: 19056kb
input:
2 99999 21832 0 77205 0 62668 0 58313 0 14640 0 76941 0 62678 0 8464 0 43145 0 26195 0 46140 0 83205 0 40047 0 81645 0 27077 0 92036 0 14236 0 3576 0 15430 0 75654 0 29049 0 62218 0 83318 0 1116 0 77861 0 9755 0 49236 0 70959 0 62295 0 33580 0 88208 0 55840 0 71061 0 24695 0 88831 0 1891 0 57285 0 9...
output:
? 70790 43991 ? 36882 98065 ? 17626 87676 ? 23816 44703 ? 44123 980 ? 61196 90504 ? 54094 11034 ? 34051 63621 ? 79154 26980 ? 55857 22501 ? 15747 28077 ? 35270 76840 ? 97062 8937 ? 61940 22539 ? 70074 43281 ? 70074 79154 ! 79154 ? 5676 85780 ? 39704 57748 ? 58489 42043 ? 30188 50842 ? 36012 24131 ? ...
result:
ok OK (2 test cases)
Test #5:
score: 0
Accepted
time: 89ms
memory: 13712kb
input:
15 3 0 0 1 0 2 0 1 7 6 0 3 0 5 0 0 0 7 0 4 0 1 0 2 2 15 6 0 5 0 1 0 7 0 14 0 11 0 15 0 12 0 2 0 4 0 9 0 13 0 0 0 8 0 3 0 0 0 0 31 3 0 31 0 17 0 23 0 4 0 13 0 1 0 12 0 6 0 0 0 20 0 26 0 14 0 29 0 8 0 25 0 21 0 19 0 5 0 15 0 18 0 10 0 22 0 7 0 28 0 2 0 24 0 30 0 27 0 9 0 16 0 2 0 0 2 63 15 0 62 0 5 0 ...
output:
? 1 3 ! 2 ? 5 1 ? 1 4 ! 4 ? 6 9 ? 7 3 ? 7 10 ! 7 ? 13 29 ? 17 18 ? 1 24 ? 1 17 ! 17 ? 37 8 ? 30 14 ? 55 56 ? 22 19 ? 19 56 ! 31 ? 36 89 ? 96 110 ? 20 79 ? 62 106 ? 82 86 ? 61 82 ! 82 ? 64 233 ? 148 51 ? 1 176 ? 126 78 ? 251 252 ? 200 224 ? 176 200 ! 176 ? 439 48 ? 144 457 ? 376 142 ? 193 427 ? 173 2...
result:
ok OK (15 test cases)
Test #6:
score: 0
Accepted
time: 81ms
memory: 15864kb
input:
16 2 2 0 0 0 2 4 4 0 3 0 1 0 0 0 0 0 8 5 0 0 0 4 0 8 0 2 0 3 0 6 0 1 0 0 0 2 16 2 0 5 0 1 0 11 0 13 0 14 0 8 0 6 0 0 0 4 0 3 0 7 0 15 0 10 0 16 0 9 0 0 0 0 0 32 15 0 0 0 14 0 18 0 26 0 17 0 25 0 27 0 6 0 9 0 4 0 13 0 23 0 30 0 32 0 12 0 11 0 31 0 28 0 3 0 19 0 10 0 22 0 7 0 5 0 29 0 24 0 20 0 21 0 1...
output:
? 1 2 ! 2 ? 3 4 ? 3 2 ! 3 ? 4 1 ? 6 4 ? 6 7 ! 7 ? 11 1 ? 6 10 ? 7 6 ? 7 12 ! 7 ? 16 13 ? 29 19 ? 7 5 ? 27 7 ? 27 8 ! 8 ? 8 24 ? 6 20 ? 13 10 ? 25 32 ? 7 25 ? 7 29 ! 7 ? 80 113 ? 16 115 ? 63 112 ? 25 50 ? 81 89 ? 11 81 ? 11 114 ! 11 ? 106 3 ? 82 72 ? 78 224 ? 13 105 ? 44 156 ? 184 54 ? 212 184 ? 212 ...
result:
ok OK (16 test cases)
Test #7:
score: 0
Accepted
time: 86ms
memory: 13892kb
input:
15 2 2 0 0 0 2 6 5 0 1 0 6 0 2 0 3 0 0 0 0 2 14 12 0 0 0 11 0 5 0 7 0 1 0 8 0 10 0 14 0 13 0 6 0 9 0 2 0 4 0 0 0 1 30 10 0 29 0 23 0 28 0 9 0 14 0 2 0 30 0 19 0 0 0 15 0 1 0 22 0 8 0 18 0 27 0 7 0 24 0 26 0 3 0 20 0 25 0 6 0 17 0 4 0 12 0 21 0 16 0 13 0 5 0 0 0 0 2 62 24 0 22 0 18 0 17 0 49 0 53 0 3...
output:
? 1 2 ! 2 ? 5 2 ? 6 5 ! 5 ? 4 9 ? 7 10 ? 4 7 ! 5 ? 27 20 ? 2 13 ? 18 17 ? 11 18 ! 18 ? 33 30 ? 4 32 ? 11 31 ? 19 59 ? 11 59 ! 37 ? 94 9 ? 59 69 ? 17 52 ? 111 44 ? 46 51 ? 52 51 ! 52 ? 12 100 ? 71 118 ? 62 146 ? 36 61 ? 48 247 ? 155 179 ? 146 179 ! 179 ? 68 449 ? 190 319 ? 239 141 ? 62 353 ? 265 488 ...
result:
ok OK (15 test cases)
Test #8:
score: 0
Accepted
time: 36ms
memory: 7988kb
input:
600 2 2 0 0 0 2 3 3 2 0 0 0 0 2 4 3 0 0 0 0 0 1 2 0 0 5 0 0 3 1 4 5 0 0 0 0 1 0 6 3 5 1 4 0 0 6 0 0 0 0 0 0 0 7 3 7 0 0 0 0 2 5 0 0 1 4 0 0 0 1 8 0 0 3 7 1 0 2 5 6 8 0 0 0 0 0 0 0 1 0 9 9 8 0 0 7 2 0 0 0 0 0 0 0 0 4 5 3 6 0 1 2 10 3 6 8 0 4 2 5 7 0 0 10 9 0 0 0 0 0 0 0 0 0 1 2 11 0 0 4 9 5 8 6 3 0 0...
output:
? 1 2 ! 2 ? 3 2 ! 2 ? 4 3 ? 4 2 ! 4 ? 2 4 ? 3 5 ! 3 ? 2 3 ? 2 6 ! 2 ? 1 4 ? 3 7 ! 1 ? 4 3 ? 4 6 ? 5 8 ! 5 ? 1 3 ? 1 4 ? 8 5 ! 5 ? 1 4 ? 1 10 ? 6 9 ! 9 ? 2 6 ? 2 10 ? 9 11 ! 11 ? 1 11 ? 4 1 ? 7 8 ! 8 ? 13 7 ? 9 11 ? 6 5 ! 9 ? 12 11 ? 5 10 ? 4 13 ! 13 ? 8 14 ? 2 5 ? 11 6 ! 6 ? 10 9 ? 8 2 ? 16 12 ! 12 ...
result:
ok OK (600 test cases)
Test #9:
score: 0
Accepted
time: 96ms
memory: 15872kb
input:
2 99999 0 0 7999 97267 75750 37659 0 0 0 0 33761 92098 90707 18838 13602 27569 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14586 86647 1519 23132 0 0 3430 14643 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47066 36968 95308 38482 34100 25297 0 0 0 0 0 0 0 0 88902 58991 0 0 0 0 66315 68538 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
? 50379 69076 ? 79924 11838 ? 18079 15463 ? 72017 29994 ? 80147 27856 ? 80763 26264 ? 39876 84186 ? 73287 34615 ? 43462 43070 ? 38721 85806 ? 84940 93114 ? 3443 79116 ? 49016 68555 ? 56289 87545 ? 32426 3887 ! 3887 ? 72481 78976 ? 96633 84675 ? 2124 81852 ? 13836 79494 ? 80643 24965 ? 38932 5573 ? 5...
result:
ok OK (2 test cases)
Test #10:
score: 0
Accepted
time: 57ms
memory: 11568kb
input:
15 3 3 2 0 0 0 0 1 7 0 0 3 6 0 0 7 2 0 0 0 0 5 1 2 2 15 14 12 0 0 0 0 0 0 8 6 10 11 0 0 3 7 2 4 0 0 0 0 0 0 15 5 0 0 9 1 0 0 0 31 4 9 0 0 29 17 0 0 0 0 15 31 5 21 18 14 0 0 0 0 0 0 16 2 12 7 0 0 23 10 0 0 30 13 0 0 24 27 11 26 0 0 0 0 0 0 0 0 19 20 0 0 0 0 0 0 6 25 8 1 28 22 2 0 0 2 63 53 48 40 57 0...
output:
? 3 2 ! 1 ? 7 2 ? 3 6 ! 6 ? 15 5 ? 9 1 ? 2 4 ! 2 ? 29 17 ? 30 13 ? 8 1 ? 18 14 ! 14 ? 1 2 ? 53 48 ? 63 19 ? 30 56 ? 55 59 ! 56 ? 20 115 ? 71 68 ? 67 3 ? 18 16 ? 123 55 ? 117 104 ! 104 ? 70 140 ? 78 250 ? 223 4 ? 220 204 ? 67 144 ? 75 15 ? 242 199 ! 242 ? 60 121 ? 414 74 ? 99 184 ? 301 403 ? 425 477 ...
result:
ok OK (15 test cases)
Test #11:
score: 0
Accepted
time: 57ms
memory: 11572kb
input:
16 2 0 0 1 0 2 4 4 2 0 0 0 0 3 0 0 0 8 3 0 0 0 0 0 0 0 1 2 0 0 6 4 5 7 0 1 2 16 16 15 0 0 0 0 0 0 7 11 8 10 0 0 13 0 0 0 0 0 0 0 3 9 0 0 4 2 5 14 6 12 0 0 0 0 32 0 0 22 21 25 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 10 30 0 1 24 12 31 0 0 0 0 16 8 3 15 11 26 23 14 28 20 6 9 0 0 13 27 0 0 0 0 7 17 0 0 0 ...
output:
? 1 2 ! 2 ? 4 2 ? 4 3 ! 4 ? 8 1 ? 6 4 ? 7 8 ! 8 ? 16 15 ? 6 12 ? 8 10 ? 8 13 ! 8 ? 19 2 ? 3 15 ? 25 18 ? 13 27 ? 13 30 ! 30 ? 9 14 ? 58 24 ? 29 32 ? 17 3 ? 15 39 ? 15 57 ! 15 ? 28 83 ? 75 21 ? 10 7 ? 30 38 ? 111 64 ? 65 51 ! 65 ? 221 218 ? 199 243 ? 66 123 ? 247 165 ? 57 198 ? 138 196 ? 15 146 ! 146...
result:
ok OK (16 test cases)
Test #12:
score: 0
Accepted
time: 56ms
memory: 11452kb
input:
15 2 0 0 1 0 2 6 6 4 1 5 0 0 0 0 3 0 0 0 0 2 14 0 0 1 7 5 11 13 9 0 0 2 8 0 0 10 0 0 0 0 0 0 0 14 6 0 0 3 4 0 0 1 30 7 0 5 13 0 0 0 0 14 30 15 20 0 0 0 0 3 19 0 0 0 0 11 21 9 1 16 24 0 0 0 0 28 2 8 10 0 0 0 0 0 0 0 0 18 6 0 0 4 29 12 25 0 0 23 26 0 0 27 22 0 0 0 2 62 0 0 0 0 28 47 7 38 0 0 0 0 17 26...
output:
? 1 2 ! 2 ? 2 6 ? 2 3 ! 3 ? 14 6 ? 3 4 ? 5 11 ! 3 ? 28 2 ? 23 26 ? 18 6 ? 8 10 ! 10 ? 61 36 ? 18 30 ? 31 50 ? 10 51 ? 42 44 ! 44 ? 18 44 ? 53 93 ? 75 45 ? 107 41 ? 80 32 ? 57 89 ! 32 ? 253 196 ? 42 224 ? 178 31 ? 218 103 ? 244 223 ? 102 147 ? 62 32 ! 32 ? 69 30 ? 210 172 ? 19 233 ? 188 349 ? 94 198 ...
result:
ok OK (15 test cases)
Test #13:
score: 0
Accepted
time: 40ms
memory: 8000kb
input:
600 2 0 0 1 0 2 3 0 0 1 3 0 0 2 4 2 4 0 0 0 0 3 0 0 0 5 2 5 0 0 0 0 0 0 4 3 1 0 6 6 4 0 0 0 0 3 0 2 1 0 0 0 0 7 0 0 0 0 2 4 5 6 0 0 0 0 1 3 0 1 8 2 7 0 0 6 0 0 0 8 3 0 0 4 5 0 0 0 0 0 9 5 2 0 0 7 4 6 8 0 0 0 0 0 0 9 1 0 0 0 0 2 10 3 5 10 7 0 0 0 0 6 2 0 0 4 0 9 1 0 0 0 0 2 0 0 11 9 6 4 1 0 0 0 0 11 ...
output:
? 1 2 ! 2 ? 1 3 ! 3 ? 4 2 ? 4 3 ! 4 ? 1 4 ? 5 3 ! 5 ? 4 5 ? 4 3 ! 4 ? 4 7 ? 5 6 ! 4 ? 5 1 ? 3 8 ? 3 6 ! 3 ? 4 1 ? 3 6 ? 3 7 ! 7 ? 1 2 ? 7 10 ? 7 4 ! 7 ? 10 1 ? 10 11 ? 5 8 ! 8 ? 12 1 ? 12 11 ? 11 2 ! 11 ? 2 4 ? 12 2 ? 12 7 ! 7 ? 8 12 ? 10 8 ? 8 5 ! 5 ? 14 9 ? 1 14 ? 11 15 ! 15 ? 10 15 ? 2 10 ? 5 9 ?...
result:
ok OK (600 test cases)
Test #14:
score: -100
Runtime Error
input:
2 99999 96748 53986 34197 77552 29863 63559 79099 26449 45078 1051 0 0 27416 4135 0 0 38606 81189 93892 68603 48776 185 79602 18311 51243 83678 89044 40032 28883 35663 0 0 0 0 21603 15821 0 0 51448 75971 70275 8326 0 0 0 0 57049 72937 3297 94939 0 0 59258 39159 3205 34675 54876 24769 0 0 0 0 0 0 851...
output:
? 30678 67916 ? 6958 84337 ? 30665 44924 ? 53241 50410 ? 66328 26921 ? 29348 8370 ? 21629 79422 ? 6123 88244 ? 83795 325 ? 4347 45077 ? 79123 15510 ? 92555 53712 ? 69472 66811 ? 8916 4051 ? 94219 43715 ? 8916 73861 ? 8916 92554