QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#772446 | #9570. Binary Tree | F7487 | TL | 0ms | 7736kb | C++14 | 2.7kb | 2024-11-22 19:37:17 | 2024-11-22 19:37:17 |
Judging History
answer
#include<bits/stdc++.h>
#define maxn 400005
using namespace std;
int qread(){
int s=0;char a=getchar();
while(!isdigit(a))a=getchar();
while(isdigit(a)){
s=(s<<1)+(s<<3)+a-'0';
a=getchar();
}
return s;
}
int cnt,head[maxn],siz[maxn],tot,son[5];
bool vis[maxn];
struct xs{
int nxt,to;
}e[maxn];
void addn(int x,int y){
e[++cnt].nxt=head[x];head[x]=cnt;
e[cnt].to=y;return;
}
void dfs_ini(int x,int f){
int i,v;
siz[x]=1;tot++;
for(i=head[x];i;i=e[i].nxt){
v=e[i].to;
if(vis[v])continue;
if(v==f)continue;
else{
dfs_ini(v,x);siz[x]+=siz[v];
}
}
return;
}
int pos;
void dfs_core(int x,int f){
int i,v;bool bl=1;
for(i=head[x];i;i=e[i].nxt){
v=e[i].to;
if(vis[v])continue;
if(v==f)continue;
if(siz[v]>tot/2){
bl=0;dfs_core(v,x);
}
if(pos)break;
}
if(bl)pos=x;
return;
}
int main(){
int T=qread(),I,n,x,y,p,v,cts,ans,st,i,j=0,s1,s2,s3;
for(I=1;I<=T;I++){
n=qread();cnt=0;ans=0;
for(i=1;i<=n;i++){
head[i]=siz[i]=vis[i]=0;
}
for(i=1;i<=n;i++){
x=qread();y=qread();
if(x){
addn(i,x);addn(x,i);
}
if(y){
addn(i,y);addn(y,i);
}
}
pos=1;j=0;
while(1){
j++;
p=pos;pos=0;tot=0;
dfs_ini(p,0);dfs_core(p,0);cts=0;
for(i=head[pos];i;i=e[i].nxt){
v=e[i].to;if(vis[v])continue;
son[++cts]=v;
}
if(cts==3){
s1=siz[son[1]];s2=siz[son[2]];s3=siz[son[3]];
if(siz[son[1]]>siz[pos])s1-=siz[pos];
if(siz[son[2]]>siz[pos])s2-=siz[pos];
if(siz[son[3]]>siz[pos])s3-=siz[pos];
if(s2==max(s1,max(s2,s3)))swap(son[1],son[2]);
if(s3==max(s1,max(s2,s3)))swap(son[1],son[3]);
cout<<"? "<<son[1]<<" "<<son[2]<<'\n';cout.flush();
st=qread();if(st==0){
vis[pos]=1;pos=son[1];
}
else{
if(st==1){
vis[son[1]]=1;vis[son[2]]=1;
}
else{
vis[pos]=1;pos=son[2];
}
}
}
else{
if(cts==2){
cout<<"? "<<son[1]<<" "<<son[2]<<'\n';cout.flush();
st=qread();if(st==0){
vis[pos]=1;pos=son[1];
}
else{
if(st==1){
ans=pos;break;
}
else{
vis[pos]=1;pos=son[2];
}
}
}
else{
if(cts==1){
cout<<"? "<<pos<<" "<<son[1]<<'\n';cout.flush();
st=qread();if(st==0){
ans=pos;break;
}
else{
ans=son[1];break;
}
}
else{
ans=pos;break;
}
}
}
if(j>100000)break;
}
cout<<"! "<<ans<<'\n';cout.flush();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7736kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 5 ? 2 1 ! 2 ? 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 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 8 ? 6 2 ! 2 ? 2 5 ? 2 3 ! 3 ? 5 7 ? 4 1 ! 4 ? 1 5 ? 1 3 ! 3 ? 4 2 ? 4 3 ! 4 ? 2 3 ! 3 ? 1 2 ! 1 ? 3 2 ! 2 ? 7 9 ? 7 3 ? 6 4 ! 4 ? 1 2 ! 1 ? 9 5 ? 1 2 ? 2 6 ! 6 ? 10 3 ? 6 8 ? 4 2 ! 4 ? 3 4 ? 1 9 ? 9 8 ! 8 ? 1 2 ! 2 ? 4 6 ? 5 2 ! 2 ? 4 9 ? 4 8 ? 8...