QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#760657#9570. Binary Treenodnodnod123ML 3ms8308kbC++141.4kb2024-11-18 18:12:372024-11-18 18:12:37

Judging History

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

  • [2024-11-18 18:12:37]
  • 评测
  • 测评结果:ML
  • 用时:3ms
  • 内存:8308kb
  • [2024-11-18 18:12:37]
  • 提交

answer

//G Nanjing
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define ll long long
const int N = 2*1e5 + 10, INF = 0x3f3f3f3f;
vector<int>son[N]; //节点的子节点
int n;//结点个数
int rt;
void co2(int x) { cout << "! " << x << endl; cout.flush(); }
void co1(int x, int y) { cout << "? " << x << " " << y << endl; cout.flush(); }
void dfs(int rt) {
    int siz = son[rt].size(),ans;
    if (siz==0) {
        co2(rt); return;
    }
    else if (siz == 2) {
        co1(son[rt][0], son[rt][1]);
        cin >> ans;
        if (ans == 1) { co2(rt); return; }
        else if (ans == 2) { dfs(son[rt][1]); }
        else { dfs(son[rt][0]); }
    }
    else {
        co1(son[rt][0], rt); cin >> ans;
        if (ans == 2) { co2(rt); return; }
        else { dfs(son[rt][0]); }
    }
}
void solve() {
    set<int>ss;
    cin >> n;
    int x, y;
    for (int i = 1; i <= n; ++i) {
        cin >> x >> y;
        son[i].clear();
        ss.insert(x); ss.insert(y);
        if(x)son[i].push_back(x);
        if(y)son[i].push_back(y);
    }
    //find root
    for (int i = 1; i <= n; ++i) {
        if (ss.count(i) == 0) {
            rt = i; break;
        }
    }
    dfs(rt);//dfs
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
    int t = 1; 
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 8308kb

input:

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

output:

? 2 4
? 1 5
! 2
? 2 1
! 1

result:

ok OK (2 test cases)

Test #2:

score: -100
Memory Limit Exceeded

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
0
0
2

output:

? 2 1
? 8 6
? 5 4
? 3 4

result: