QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#751418#9570. Binary TreeCrying#TL 1ms6172kbC++141.4kb2024-11-15 18:35:322024-11-15 18:35:32

Judging History

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

  • [2024-11-15 18:35:32]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:6172kb
  • [2024-11-15 18:35:32]
  • 提交

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(){
    cin>>n;
    for(int i=1;i<=n;i++)e[i].clear(),vis[i] = 1;
    for(int i=1;i<=n;i++){
        int x,y; cin>>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){
            cout<<"! "<<ans<<endl;
            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];
        //
        cout<<"? "<<x<<" "<<y<<endl;
        int res; cin>>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(){
    cin>>T;
    while(T--)solve();

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 6172kb

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...

result: