QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#729842 | #9570. Binary Tree | ucup-team3282# | WA | 5ms | 7724kb | C++14 | 1.6kb | 2024-11-09 17:56:07 | 2024-11-09 17:56:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN=5e5+5;
ll t,n;
struct edge{
ll u,v,nxt;
}e[MAXN];
ll head[MAXN],edge_num;
void add_edge(ll From,ll To){
e[++edge_num]=(edge){From,To,head[From]};
head[From]=edge_num;
return;
}
bool vis[MAXN];
ll deep[MAXN];
ll res;
ll fa[MAXN];
void solve(ll now,ll father){
deep[now]=deep[father]+1;
fa[now]=father;
if(deep[now]>deep[res])res=now;
for(int i=head[now];i;i=e[i].nxt){
if(vis[e[i].v]||e[i].v==father)continue;
solve(e[i].v,now);
}
return;
}
ll x,y;
int main(){
cin>>t;
while(t--){
cin>>n;
for(int i=1;i<=n;i++){
cin>>x>>y;
if(x){
add_edge(i,x);
add_edge(x,i);
}
if(y){
add_edge(i,y);
add_edge(y,i);
}
}
ll L=1,R=n;
res = 0;
solve(1,1);
L=res;
solve(L,L);
R=res;
while(L!=R){
ll tmp;
cout<<"? "<<L<<" "<<R<<endl;
cin>>tmp;
if (tmp == 1)
{
int u = R;
while (deep[u] * 2 != deep[L] + deep[R])
{
vis[u] = 1;
u = fa[u];
}
vis[fa[u]] = 1;
res = 0;
solve(u, u);
L = res;
solve(L, L);
R = res;
}
if (tmp == 2)
{
swap(L,R);
solve(L,L);
}
int u = R;
while (deep[u] - deep[L] > deep[R] - deep[u])
{
vis[u] = 1;
u = fa[u];
}
R = u;
res = 0;
solve(L, L);
L = res;
solve(L, L);
R = res;
}
cout << "! " << L <<endl;
for(int i=1;i<=n;i++)vis[i]=head[i]=fa[i]=deep[i]=0;
edge_num=0;
//clear all array !
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7724kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 1 2 0 2 0 0 2
output:
? 4 5 ? 1 5 ! 2 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 5ms
memory: 7684kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 2 2
output:
? 3 7 ? 1 7 ? 2 1 ! 1 ? 6 4 ? 7 4 ? 1 4 ? 2 4
result:
wrong answer Too many queries (test case 2)