QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#751425 | #9570. Binary Tree | Crying# | TL | 0ms | 6928kb | C++14 | 1.5kb | 2024-11-15 18:37:48 | 2024-11-15 18:37:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int T,n,vis[N];
vector<int> e[N];
void link(int x,int y){e[x].push_back(y); e[y].push_back(x);}
int dep[N],x,y,dx[N],dy[N];
void dfs(int u,int fa){
if(!vis[u])return;
for(auto v : e[u])if(v != fa){
dep[v] = dep[u] + 1;
dfs(v,u);
}
}
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;i++)e[i].clear(),vis[i] = 1;
for(int i=1;i<=n;i++){
int x,y; scanf("%d%d",&x,&y);
if(x)link(i,x); if(y)link(i,y);
}
while(1){
int cnt = 0,ans = 0;
for(int i=1;i<=n;i++)if(vis[i])cnt++,ans = i;
if(cnt == 1){
printf("! %d\n",ans); fflush(stdout);
return;
}
x = y = 0;
dep[ans] = 0; dfs(ans,0);
for(int i=1;i<=n;i++)if(vis[i] && dep[i] > dep[x])x = i;
dep[x] = 0; dfs(x,0);
for(int i=1;i<=n;i++)if(vis[i] && dep[i] > dep[y])y = i;
for(int i=1;i<=n;i++)dx[i] = dep[i];
dep[y] = 0; dfs(y,0);
for(int i=1;i<=n;i++)dy[i] = dep[i];
//
printf("? %d %d\n",x,y); fflush(stdout);
int res; scanf("%d",&res);
for(int i=1;i<=n;i++)if(vis[i]){
if(res == 0)vis[i] = dx[i] < dy[i];
else if(res == 1)vis[i] = dx[i] == dy[i];
else vis[i] = dx[i] > dy[i];
}
}
}
int main(){
scanf("%d",&T);
while(T--)solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 6928kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 1 2 0 2 0 0 2
output:
? 4 1 ? 1 5 ! 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 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 1 0 5 4 5 3 1 0 0 0 0 0 0 2 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 1 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 2 1 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 1 10 2 8 9 7 0 0 ...
output:
? 7 3 ? 3 5 ? 3 4 ! 4 ? 1 6 ? 1 3 ? 2 4 ! 2 ? 7 2 ? 2 4 ? 6 8 ! 6 ? 3 4 ? 4 5 ! 4 ? 1 2 ? 1 4 ! 5 ? 3 4 ? 1 3 ! 3 ? 3 1 ? 1 2 ! 5 ? 2 3 ! 3 ? 1 2 ! 2 ? 2 3 ! 1 ? 3 8 ? 3 5 ? 3 4 ! 4 ? 1 2 ! 1 ? 4 6 ? 4 5 ? 3 8 ! 8 ? 9 2 ? 2 10 ? 2 6 ! 6 ? 5 1 ? 5 4 ? 5 6 ! 6 ? 1 2 ! 1 ? 2 1 ? 1 7 ! 1 ? 3 1 ? 7 1 ? 1...