QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#740253#9570. Binary TreefosovWA 3ms7408kbC++143.1kb2024-11-13 07:47:182024-11-13 07:47:18

Judging History

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

  • [2024-11-13 07:47:18]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:7408kb
  • [2024-11-13 07:47:18]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;

#define ll long long 
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3fll
#define MOD 998244353
#define pii pair<int, int>

#define N 100010
#define K 20

int vld[N], f[N], sz[N], w[N], ind[N];
vector<int> G[N];

void add_edge(int u, int v) {
    G[u].emplace_back(v);
    G[v].emplace_back(u);
}

int dfs(int u, int fa) {
    sz[u] = 1;
    for (auto v : G[u]) {
        if (v == fa || !vld[v]) continue;
        sz[u] += dfs(v, u);
    }
    return sz[u];
}

void center(int u, int fa, int psz, int& ans) {
    w[u] = psz ++;
    for (auto v : G[u]) {
        if (v == fa || !vld[v]) continue;
        psz += sz[v];
        w[u] = max(w[u], sz[v]);
    }
    if (ans == -1 || w[u] < w[ans]) ans = u;
    for (auto v : G[u]) {
        if (v == fa || !vld[v]) continue;
        center(v, u, psz - sz[v], ans);
    }
}

void fg(int u, int fa, int flag) {
    f[u] = flag;
    for (auto v : G[u]) {
        if (v == fa || !vld[v]) continue;
        fg(v, u, flag);
    }
}

int query(int u, int v) {
    cout << "? " << u << ' ' << v << '\n';
    cout.flush();
    int res; cin >> res;

    if (res == -1) {
        cout << "ask " << u << ' ' << v << '\n';
        exit(1);
    }
    return res;
}

void show(int u, int fa) {
    cout << u << ' ';
    for (auto v : G[u]) {
        if (v == fa || !vld[v]) continue;
        show(v, u);
    }
}

int main() {
#ifdef TEST
    freopen("zz.in", "r+", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t; cin >> t;
    while (t --) {
        int n; cin >> n;
        for (int i = 1; i <= n; ++ i) {
            G[i].clear();
            vld[i] = 1;
        }

        for (int i = 1; i <= n; ++ i) {
            int l, r; cin >> l >> r;
            if (l) add_edge(i, l);
            if (r) add_edge(i, r);
        }

        int rt = 1;
        while (dfs(rt, 0) > 1) {
            int nrt = -1;
            center(rt, 0, 0, nrt);
            rt = nrt;

            dfs(rt, 0);

            // show(rt, 0);
            // cout << '\n';

            vector<int> vch;
            for (auto v : G[rt]) {
                if (vld[v] && sz[v]) vch.emplace_back(v);
            }
            
            fg(rt, 0, 0);
            if (vch.size() >= 2) {
                int u = vch[0], v = vch[1];
                int x = query(u, v);
                if (x == 0) fg(u, rt, 1);
                if (x == 1) fg(rt, 0, 1), fg(u, rt, 0), fg(v, rt, 0);
                if (x == 2) fg(v, rt, 1);
            } else {
                assert(sz[rt] == 2);
                int u = vch[0];
                int x = query(u, rt);
                if (x == 0) fg(u, rt, 1);
                if (x == 1) assert(0);
                if (x == 2) fg(rt, 0, 1), fg(u, rt, 0);                
            }
    
            for (int i = 1; i <= n; ++ i) vld[i] &= f[i];
            for (int i = 1; i <= n; ++ i) if (vld[i]) rt = i;
        }

        cout << "! " << rt << '\n';
        cout.flush();
    }
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 7400kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 7408kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
0
2
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
1
1
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
0
5
3 0
5 1
0 0
0 0
4 0
2
0
5
5 0
0 0
0 0
3 0
2 4
1
2
3
3 0
1 0
0 0
1
2
2 0
0 0
2
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

? 1 8
? 5 4
? 3 4
! 3
? 2 7
? 5 6
? 5 7
! 7
? 5 8
? 4 2
? 6 8
! 6
? 4 5
? 3 1
! 2
? 5 6
? 1 4
! 1
? 5 1
? 1 3
! 1
? 1 2
? 3 5
! 5
? 3 2
! 1
? 2 1
! 1
? 2 3
! 3
? 1 9
? 2 6
? 4 3
! 6
? 2 1
! 2
? 5 9
? 4 8
? 3 5
! 5
? 3 10
? 6 2
? 4 10
! 4
? 3 4
? 1 7
? 8 2
! 2
? 2 1
! 1
? 4 3
? 1 7
! 7
? 4 9
? 2 3
? ...

result:

wrong answer Too many queries (test case 90)