QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#751464#9570. Binary TreeCrying#RE 0ms0kbC++141.6kb2024-11-15 18:50:432024-11-15 18:50:45

Judging History

你现在查看的是最新测评结果

  • [2024-11-15 18:50:45]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-15 18:50:43]
  • 提交

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); int cnt = 0;
    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];
        //
        cnt++;
        if((1LL<<cnt) > n){
            assert(0);
        }
        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: 0
Runtime Error

input:

2
5
0 0
1 5
2 4
0 0
0 0

output:


result: