CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:ilyas.ben
Submission time:2024-10-24 11:44:28 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#10.03 sdetails
#20.03 sdetails
#30.03 sdetails
#40.03 sdetails
#50.03 sdetails
#60.44 sdetails
#70.07 sdetails
#80.14 sdetails
#90.11 sdetails
#100.11 sdetails
#110.03 sdetails
#120.13 sdetails

Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 200000;
const int LOG = 20;

vector<long long> tree[MAXN];
long long parent[MAXN][LOG];
long long depth[MAXN];
long long n, q;

void dfs(long long node, long long par) {
    parent[node][0] = par;
    for (long long i = 1; i < LOG; i++) {
        if (parent[node][i - 1] != -1) {
            parent[node][i] = parent[parent[node][i - 1]][i - 1];
        }
    }

    for (long long child : tree[node]) {
        if (child != par) {
            depth[child] = depth[node] + 1;
            dfs(child, node);
        }
    }
}

long long lca(long long a, long long b) {
    if (depth[a] < depth[b]) swap(a, b);

    for (long long i = LOG - 1; i >= 0; i--) {
        if (depth[parent[a][i]] >= depth[b]) {
            a = parent[a][i];
        }
    }

    if (a == b) return a;

    for (long long i = LOG - 1; i >= 0; i--) {
        if (parent[a][i] != parent[b][i]) {
            a = parent[a][i];
            b = parent[b][i];
        }
    }

    return parent[a][0];
}

int main() {
    cin >> n >> q;

    for (long long i = 0; i < MAXN; i++) {
        for (long long j = 0; j < LOG; j++) {
            parent[i][j] = -1;
        }
    }

    for (long long i = 2; i <= n; i++) {
        long long boss;
        cin >> boss;
        tree[boss].push_back(i);
    }

    depth[1] = 0;
    dfs(1, -1);

    for (long long i = 0; i < q; i++) {
        long long a, b;
        cin >> a >> b;
        cout << lca(a, b) << 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
6
8
3
-1
8
...

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
...

Test 3

Verdict:

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
2
2
3
1
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
...

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
74862
8750
16237
72298
58111
...
Truncated

Test 7

Verdict:

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
(empty)

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
2

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)