CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:Farah
Submission time:2024-10-13 21:19:08 +0300
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.26 sdetails
#7ACCEPTED0.14 sdetails
#8ACCEPTED0.22 sdetails
#9ACCEPTED0.28 sdetails
#10ACCEPTED0.25 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.32 sdetails

Code

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

const int MAXN = 200005;
const int MAXK = 18;  // Because log2(2e5) ≈ 17.6

int n, q;
vector<int> adj[MAXN];
int up[MAXN][MAXK];  // up[node][k] = 2^k-th ancestor of node
int depth[MAXN];

void dfs(int u, int parent) {
    up[u][0] = parent;
    for (int k = 1; k < MAXK; ++k) {
        if (up[u][k - 1] != 0)
            up[u][k] = up[up[u][k - 1]][k - 1];
        else
            up[u][k] = 0;
    }
    for (int v : adj[u]) {
        if (v != parent) {
            depth[v] = depth[u] + 1;
            dfs(v, u);
        }
    }
}

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

    // Lift node a up so that depth[a] == depth[b]
    int diff = depth[a] - depth[b];
    for (int k = 0; k < MAXK; ++k) {
        if ((diff >> k) & 1)
            a = up[a][k];
    }

    if (a == b)
        return a;

    // Lift both a and b up until their ancestors are the same
    for (int k = MAXK - 1; k >= 0; --k) {
        if (up[a][k] != 0 && up[a][k] != up[b][k]) {
            a = up[a][k];
            b = up[b][k];
        }
    }

    return up[a][0];  // or up[b][0]
}

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

    cin >> n >> q;
    // Since employee 1 is the root, we set its parent to 0
    for (int v = 2; v <= n; ++v) {
        int u;
        cin >> u;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

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

    while (q--) {
        int a, b;
        cin >> a >> b;
        int ans = lca(a, b);
        cout << ans << '\n';
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

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: ACCEPTED

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: 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: ACCEPTED

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: ACCEPTED

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: ACCEPTED

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

Test 8

Verdict: ACCEPTED

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

Test 9

Verdict: ACCEPTED

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
2796
633
633
151
2690
...
Truncated

Test 10

Verdict: ACCEPTED

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
365
73
103
365
216
...
Truncated

Test 11

Verdict: ACCEPTED

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: ACCEPTED

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
27468
6353
27468
6353
6353
...
Truncated