QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#756025 | #9570. Binary Tree | veg# | RE | 0ms | 6072kb | C++14 | 1.4kb | 2024-11-16 18:46:08 | 2024-11-16 18:46:10 |
Judging History
answer
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N=1e5+5;
int sz[N],mx[N],rt;
bool vis[N];
vector<int>V[N];
void getsz(int fa,int u,int tot) {
sz[u]=1; mx[u]=0;
for(int v:V[u]) {
if(!vis[v]&&v!=fa) {
getsz(u,v,tot);
sz[u]+=sz[v];
mx[u]=max(mx[u],sz[v]);
}
}
mx[u]=max(mx[u],tot-sz[u]);
if(mx[u]<=tot/2) {
rt=u;
}
}
void dfs(int u,int tot) {
if(tot==1) {
printf("! %d\n",u);
fflush(stdout);
return;
}
if(tot==2) {
for(int v:V[u]) {
if(!vis[v]) {
printf("? %d %d\n",u,v);
fflush(stdout);
int x; scanf("%d",&x);
printf("! %d\n",(x==0)?u:v);
fflush(stdout);
return;
}
}
}
getsz(0,u,tot);
vector<int>tmp; tmp.clear();
for(int v:V[rt]) {
if(!vis[v]) tmp.pb(v),sz[v]=(sz[v]<sz[rt]?sz[v]:tot-sz[rt]);
}
u=tmp[0]; int v=tmp[1];
printf("? %d %d\n",u,v); fflush(stdout);
int x; scanf("%d",&x);
if(x==0) {
vis[rt]=1;
dfs(u,sz[u]);
} else if(x==1) {
vis[u]=vis[v]=1;
dfs(rt,tot-sz[u]-sz[v]);
} else {
vis[rt]=1;
dfs(v,sz[v]);
}
}
int main() {
int T; scanf("%d",&T);
while(T--) {
int n; scanf("%d",&n);
for(int i=1;i<=n;++i) V[i].clear(),vis[i]=0;
for(int i=1;i<=n;++i) {
int x,y; scanf("%d%d",&x,&y);
if(x!=0) V[i].pb(x),V[x].pb(i);
if(y!=0) V[i].pb(y),V[y].pb(i);
}
dfs(1,n);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6072kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 1 5 ? 2 4 ! 3 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Runtime Error
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 2 2 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 1 2 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 2 5 3 0 5 1 0 0 0 0 4 0 0 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 2 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 ? 2 7 ? 7 8 ? 8 6 ! 8 ? 5 8 ? 4 2 ? 6 8 ! 8 ? 4 5 ? 3 1 ! 3 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 5 4 ! 5 ? 1 2 ? 3 5 ! 5 ? 3 2 ! 2 ? 1 2 ! 2 ? 2 3 ! 3 ? 1 9 ? 2 6 ? 4 3 ! 4 ? 1 2 ! 1 ? 5 9 ? 2 1 ? 2 6 ! 2 ? 3 10 ? 6 2 ? 4 10 ! 10 ? 3 4 ? 1 7 ? 8 2 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 8 4 ?...