QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#758373#9570. Binary TreekukkaWA 2ms7760kbC++205.1kb2024-11-17 18:05:392024-11-17 18:05:41

Judging History

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

  • [2024-11-17 18:05:41]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7760kb
  • [2024-11-17 18:05:39]
  • 提交

answer

#include<bits/stdc++.h>
#ifdef LOCAL_MACHINE
    #define debug(...) printf(__VA_ARGS__)
    #define sp() system("pause")
    #define IOS
    #define cout cout << ">>>\t"
#else
    #define debug(...)
    #define sp()
    #define IOS std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#endif

using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;

constexpr i64 Mod = 998244353;
constexpr int N = 1e5, M = 1e5, Inf = 1e9;

std::vector<int> g[N + 5], deep(N + 5);
int st[N + 5][30];
bool vis[N + 5];
void init(int n = 0) {
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        vis[i] = false;
        deep[i] = 0;
        for (int j = 0; j < 30; j++) {
            st[i][j] = 0;
        }
    }
}

void dfs0(int cur, int fa = 0, int dp = 1) {
    deep[cur] = dp;
    for(int i = 1; (1 << i) <= deep[cur]; ++i) {
        st[cur][i] = st[st[cur][i-1]][i-1];
    }
    for (auto &to : g[cur]) {
        if (to != fa) {
            dfs0(to, cur, dp + 1);
        }
    }
}

void dfs(int cur, std::pair<int, int> &car, int fa = 0, int dp = 1) {
    if (dp > car.second) {
        car = { cur, dp };
    }
    for (auto &to : g[cur]) {
        if (to == fa || vis[to]) {
            continue;
        }
        dfs(to, car, cur, dp + 1);
    }
}

int main()
{
    /*
1
11
2 3
4 5
6 7
0 0
8 9
0 0
0 0
0 0
10 11
0 0
0 0
    */
    IOS;
    int t = 1;
    std::cin >> t;
    
    while (t--)
    {
        int n = 0;
        std::cin >> n;
        init(n);

        for (int i = 1; i <= n; i++) {
            int l = 0, r = 0;
            std::cin >> l >> r;
            if (l) {
                g[i].push_back(l);
                g[l].push_back(i);
                st[l][0] = i;
            }
            if (r) {
                g[i].push_back(r);
                g[r].push_back(i);
                st[r][0] = i;
            }
        }

        for (int i = 1; i <= n; i++) {
            if (st[i][0] == 0) {
                dfs0(i);
                break;
            }
        }
        int ans = 0;
        int ver = 1;
        while (true) {
            std::pair<int, int> u{}; dfs(ver, u);
            std::pair<int, int> v{}; dfs(u.first, v);
            int _u = u.first, _v = v.first;
            if (_u == _v) {
                ans = _u;
                break;
            }
            if (deep[_u] < deep[_v]) {
                std::swap(_u, _v);
            }
            if (deep[_u] != deep[_v]) {
                for (int i = 29; ~i; i--) {
                    if (deep[st[_u][i]] >= deep[_v]) {
                        _u = st[_u][i];
                    }
                }
            }
            int lca = _u;
            if (_u != _v)
            {
                for (int i = 29; ~i; i--) {
                    if (st[_u][i] != st[_v][i]) {
                        _u = st[_u][i];
                        _v = st[_v][i];
                    }
                }
                lca = st[_u][0];
            }
            int lu = deep[u.first] - deep[lca];
            int lv = deep[v.first] - deep[lca];
            
            std::cout << "? " << u.first << ' ' << v.first << std::endl;
            int f = 0;
            std::cin >> f;
            if (f == 2) {
                f = 0;
                std::swap(u, v);
                std::swap(lu, lv);
            }
            if (f == 1) {
                if (lu == lv) {
                    ver = lca;
                    for (auto &to : g[lca]) {
                        if (to != st[lca][0]) {
                            vis[to] = true;
                        }
                    }
                }
                else {
                    if (deep[u.first] < deep[v.first]) {
                        std::swap(u, v);
                        std::swap(lu, lv);
                    }
                    int len = (lu + lv) / 2 - 1;
                    for (int i = 0; i < 30; i++) {
                        if ((len >> i & 1)) {
                            u.first = st[u.first][i];
                        }
                    }
                    vis[u.first] = true;
                    vis[st[u.first][1]] = true;
                    ver = st[u.first][0];
                }
            }
            else {
                ver = u.first;
                int len = (lu + lv - 1) / 2;
                if (lu >= lv) {
                    for (int i = 0; i < 30; i++) {
                        if ((len >> i & 1)) {
                            u.first = st[u.first][i];
                        }
                    }
                    vis[st[u.first][0]] = true;
                }
                else {
                    len = (lu + lv - len - 1);
                    for (int i = 0; i < 30; i++) {
                        if ((len >> i & 1)) {
                            v.first = st[v.first][i];
                        }
                    }
                    vis[v.first] = true;
                }
            }
        }
        std::cout << "! " << ans << std::endl;
    }

    sp();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 5648kb

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

output:

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

result:

wrong answer Too many queries (test case 20)