Submission details
Task:Company Queries II
Sender:aalto26gh_014
Submission time:2026-10-19 12:04:15 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#3ACCEPTED0.00 sdetails
#40.00 sdetails
#50.00 sdetails
#6--details
#7ACCEPTED0.98 sdetails
#8--details
#9--details
#10--details
#110.00 sdetails
#12--details

Compiler report

input/code.cpp: In function 'int ancestor(int, int)':
input/code.cpp:23:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |     for (int ind=0; ind<ancestors[node].size(); ind++) {
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~

Code

#include <vector>
#include <iostream>
#include <math.h>
using namespace std;

vector<vector<int>> ancestors;  // a[node][lb(x)]
vector<int> depth;
vector<bool> been;
vector<int> dfsOrder;
vector<int> boss;

int dfs(int node) {
    if (been[node]) return depth[node];
    been[node] = true;
    if (node == 0) return depth[node];
    int d = dfs(boss[node]) + 1;
    dfsOrder.push_back(node);
    depth[node] = d;
    return d;
}

int ancestor(int node, int up) {
    for (int ind=0; ind<ancestors[node].size(); ind++) {
        if ((1 << ind) & up) {
            node = ancestors[node][ind];
        }
    }
    return node;
}

int main() {
    int n, q;
    cin >> n >> q;
    ancestors.resize(n);
    depth.resize(n);
    been.resize(n);
    dfsOrder.resize(0);
    boss.resize(n);
    for (int i=0; i<n; i++) { been[i] = false; }

    boss[0] = 0;
    depth[0] = 0;
    dfsOrder.push_back(0);
    
    int a, b;
    for (int i=1; i<n; i++) {
        cin >> b;
        b--;
        boss[i] = b;
    }

    for (int i=0; i<n; i++) { dfs(i); }

    int logN = log2(2*n);
    
    // set ancestors
    for (int i=0; i<n; i++) { ancestors[i].push_back(boss[i]); }
    for (int i=0; i<logN; i++) {
        for (int j=0; j<n; j++) {
            ancestors[dfsOrder[j]].push_back(ancestors[ancestors[dfsOrder[j]][i]][i]);
        }
    }
    int da, db;
    for (int i=0; i<q; i++) {
        cin >> a >> b;
        a--; b--;
        da = depth[a];
        db = depth[b];
        if (da > db) swap(a, b);  // b is lower
        b = ancestor(b, abs(da - db));  // level

        // binary search
        int k = 0;
        for (int up = n/2; up >= 1; up /= 2) {
            while (k+up < n && (ancestor(a, k+up) != ancestor(b, k+up))) k += up;
        }
        cout << ancestor(a, k + 1) + 1 << endl;
    }

    return 0;
}

Test details

Test 1

Verdict:

input
10 10
1 2 3 4 5 6 7 8 9
6 9
8 10
10 3
...

correct output
6
8
3
1
8
...

user output
5
7
2
1
7
...

Feedback: Incorrect character on line 1 col 1: expected "6", got "5"

Test 2

Verdict:

input
10 10
1 1 1 1 1 1 1 1 1
1 7
3 4
4 1
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Feedback: Incorrect character on line 7 col 1: expected "4", got "1"

Test 3

Verdict: ACCEPTED

input
10 10
1 1 1 1 2 3 4 4 1
1 8
2 7
8 3
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 4

Verdict:

input
10 10
1 1 3 1 2 2 5 3 9
7 2
7 6
3 9
...

correct output
2
2
3
1
1
...

user output
1
2
1
1
1
...

Feedback: Incorrect character on line 1 col 1: expected "2", got "1"

Test 5

Verdict:

input
10 10
1 2 3 2 5 3 2 2 4
6 1
1 3
1 9
...

correct output
1
1
1
2
2
...

user output
1
1
1
2
2
...

Feedback: Incorrect character on line 6 col 1: expected "3", got "2"

Test 6

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
74862
8750
16237
72298
58111
...

user output
(empty)

Test 7

Verdict: ACCEPTED

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 8

Verdict:

input
200000 200000
1 2 1 2 3 2 1 6 3 1 10 12 13 4...

correct output
1
2
2
2
1
...

user output
(empty)

Test 9

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2796
633
633
151
2690
...

user output
(empty)

Test 10

Verdict:

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
365
73
103
365
216
...

user output
(empty)

Test 11

Verdict:

input
2 4
1
1 1
1 2
2 1
...

correct output
1
1
1
2

user output
1
1
1
1

Feedback: Incorrect character on line 4 col 1: expected "2", got "1"

Test 12

Verdict:

input
200000 200000
1 1 2 3 4 5 6 7 8 9 10 11 12 1...

correct output
27468
6353
27468
6353
6353
...

user output
(empty)