QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#721906#9570. Binary Treeucup-team3519#RE 1ms5920kbC++172.3kb2024-11-07 17:09:022024-11-07 17:09:03

Judging History

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

  • [2024-11-07 17:09:03]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5920kb
  • [2024-11-07 17:09:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define V vector
#define pb push_back
typedef pair<int, int> pi;
#define fi first
#define se second

const int INF = 2e9 + 1000;
const int N = 1e5 + 100;

int n;
V<int> e[N];
int ban[N];

int d[N], sz[N], f[N];
void dfs(int x, int p) {
    f[x] = p;
    d[x] = 0;
    if(p) d[x]++;

    sz[x] = 0;
    for(auto y : e[x]) if(y != p && !ban[y]) {
        dfs(y, x);
        d[x]++;
        sz[x] += sz[y];
    }
    sz[x]++;
}


int cnt = 0;
int query(int x, int y) {
    cnt++;
    assert(cnt <= __lg(n));
    cout << "? " << x << " " << y << endl;
    int t; cin >> t;
    return t;
}

int deal(int x) {
    dfs(x, 0);
    int tot = sz[x];
    if(tot == 1) return x;
    int good = -1;
    for(int i = 1; i <= n; i++) if(!ban[i]) {
        bool ok = 1;
        for(auto y : e[i]) if(y != f[i] && !ban[y]) {
            if(sz[y] > tot / 2) ok = 0;
        }
        if(f[i]) {
            if(tot - sz[i] > tot / 2) ok = 0;
        }
        if(ok) good = i;
    }
    assert(good != -1);
    dfs(good, 0);
    assert(d[good] >= 1);
    if(d[good] == 1) {
        int u = good, v = -1;
        for(auto y : e[good]) if(!ban[y]) {
            v = y;
        }
        assert(v != -1);
        int t = query(u, v);
        if(t == 0) {
            return u;
        } else return v;
    } 
    int a = 0, b = 0;
    for(auto y : e[good]) if(!ban[y]) {
        if(sz[y] > sz[a]) {
            b = a, a = y;
        } else if(sz[y] > sz[b]) {
            b = y;
        }
    }
    int t = query(a, b);
    if(t == 0) {
        ban[good] = 1;
        return deal(a);
    } else if(t == 2) {
        ban[good] = 1;
        return deal(b);
    } else if(t == 1) {
        ban[a] = 1, ban[b] = 1;
        return deal(good);
    }
    assert(0);
}

void solve() {
    cin >> n;
    for(int i = 1; i <= n; i++) e[i].clear(), ban[i] = 0;
    cnt = 0;
    for(int i = 1; i <= n; i++) {
        int x, y; cin >> x >> y;
        if(x) e[i].pb(x), e[x].pb(i);
        if(y) e[i].pb(y), e[y].pb(i);
    }
    int ans = deal(1);
    cout << "! " << ans << endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }
}

详细

Test #1:

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

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
? 2 1
! 1

result:

ok OK (2 test cases)

Test #2:

score: -100
Runtime Error

input:

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

output:

? 2 4
? 2 7

result: