QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#758265 | #9570. Binary Tree | wyhao | WA | 27ms | 3856kb | C++14 | 2.4kb | 2024-11-17 17:16:24 | 2024-11-17 17:16:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n;
int ch[N][2],fa[N];
int rt,u,v,G,Gmin,nn;
int siz[N];
bool vis[N];
vector<int> vec;
void dfs(int x,int p){
siz[x]=1;
int Gmax = 0;
if(!vis[fa[x]] and fa[x]!=p){
dfs(fa[x],x);
siz[x]+=siz[fa[x]];
Gmax=max(Gmax,siz[fa[x]]);
}
if(!vis[ch[x][0]] and ch[x][0]!=p){
dfs(ch[x][0],x);
siz[x]+=siz[ch[x][0]];
Gmax=max(Gmax,siz[ch[x][0]]);
}
if(!vis[ch[x][1]] and ch[x][1]!=p){
dfs(ch[x][1],x);
siz[x]+=siz[ch[x][1]];
Gmax=max(Gmax,siz[ch[x][1]]);
}
Gmax = max(Gmax,nn-siz[x]);
if(Gmax < Gmin){
Gmin = Gmax;
G = x;
}
vec.push_back(x);
}
bool fnd(){
nn=0;
for(auto p:vec){
if(!vis[p]) nn++;
}
G=rt;Gmin=0x3f3f3f3f;
vec.clear();
dfs(rt,0);
if(Gmin == 0) return false;
vec.clear();
int GG=G;
dfs(G,0);
G = GG;
return true;
}
void idfs(int x,int p){
vis[x]=true;
if(!vis[fa[x]] and fa[x]!=p){
idfs(fa[x],x);
}
if(!vis[ch[x][0]] and ch[x][0]!=p){
idfs(ch[x][0],x);
}
if(!vis[ch[x][1]] and ch[x][1]!=p){
idfs(ch[x][1],x);
}
}
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;i++) fa[i]=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&ch[i][0],&ch[i][1]);
fa[ch[i][0]]=i;
fa[ch[i][1]]=i;
vis[i]=false;
vec.push_back(i);
}
vis[0]=true;
rt = 1;
int s;
vector<int>vv;
while(fnd()){
fflush(stdout);
s=0;
vv.clear();
if(!vis[fa[G]]) vv.push_back(fa[G]);
if(!vis[ch[G][0]]) vv.push_back(ch[G][0]);
if(!vis[ch[G][1]]) vv.push_back(ch[G][1]);
if(vv.size()==1){
u = vv[0];
printf("? %d %d\n",G,u);
fflush(stdout);
scanf("%d",&s);
if(s==0){
break;
}else{
G = u;
break;
}
}else if(vv.size()==2){
u = vv[0];
v = vv[1];
printf("? %d %d\n",u,v);
fflush(stdout);
scanf("%d",&s);
if(s==1){
break;
}
vis[G]=true;
if(s==0){
idfs(v,G);
rt = u;
}else{
idfs(u,G);
rt = v;
}
}else{
sort(vv.begin(), vv.end(), [&](int x, int y) {
return siz[x] > siz[y];
});
u = vv[0];
v = vv[1];
printf("? %d %d\n",u,v);
fflush(stdout);
scanf("%d",&s);
if(s==1){
idfs(u,G);
idfs(v,G);
rt = G;
}else if(s==0){
idfs(G,u);
rt = u;
}else{
idfs(G,v);
rt = v;
}
}
}
printf("! %d\n",G);
fflush(stdout);
}
int main(){
int tests;
scanf("%d",&tests);
while(tests--){
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3856kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 1 ? 5 2 ! 5 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 27ms
memory: 3808kb
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:
? 2 4 ? 2 7 ? 1 2 ! 2 ? 7 2 ? 5 6 ? 7 5 ! 7 ? 8 3 ? 8 4 ? 2 6 ! 6 ? 2 4 ? 3 2 ! 2 ? 5 7 ? 1 4 ! 1 ? 5 1 ? 4 5 ! 5 ? 4 1 ? 3 4 ! 3 ? 2 3 ! 3 ? 1 2 ! 1 ? 2 3 ! 3 ? 7 1 ? 7 4 ? 3 6 ! 6 ? 1 2 ! 1 ? 7 2 ? 5 4 ? 7 5 ! 7 ? 3 10 ? 8 6 ? 2 4 ! 2 ? 8 2 ? 8 6 ? 4 8 ! 8 ? 1 2 ! 2 ? 5 7 ? 5 2 ? 6 5
result:
wrong answer Too many queries (test case 17)