CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:odanobunaga8199
Submission time:2024-10-21 17:48:42 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.01 sdetails
#6ACCEPTED0.34 sdetails
#7ACCEPTED0.15 sdetails
#8ACCEPTED0.24 sdetails
#9ACCEPTED0.32 sdetails
#10ACCEPTED0.27 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.38 sdetails

Code

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

// Maximum number of nodes
const int MAX = 200005;
// Maximum number of levels for Binary Lifting (2^20 > 1e6)
const int LOG = 20;

int n, q;
vector<vector<int>> adj(MAX);
int up[MAX][LOG]; // up[v][k] is the 2^k-th ancestor of v
int depth_node[MAX];

// DFS to compute depth and up tables
void dfs(int v, int parent_v) {
    up[v][0] = parent_v;
    for(int k = 1; k < LOG; k++) {
        if(up[v][k-1] != -1)
            up[v][k] = up[ up[v][k-1] ][k-1];
        else
            up[v][k] = -1;
    }
    for(auto &u : adj[v]) {
        if(u != parent_v) {
            depth_node[u] = depth_node[v] + 1;
            dfs(u, v);
        }
    }
}

// Function to compute LCA of a and b
int lca(int a, int b) {
    if(depth_node[a] < depth_node[b])
        swap(a, b);
    // Bring a to the same depth as b
    int k = LOG - 1;
    for(; k >=0; k--) {
        if(up[a][k] != -1 && depth_node[ up[a][k] ] >= depth_node[b])
            a = up[a][k];
    }
    if(a == b)
        return a;
    // Now lift both a and b together
    for(int k = LOG -1; k >=0; k--) {
        if(up[a][k] != -1 && up[a][k] != up[b][k]) {
            a = up[a][k];
            b = up[b][k];
        }
    }
    return up[a][0];
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> q;
    // Read e_2 to e_n
    for(int i = 2; i <= n; i++) {
        int boss;
        cin >> boss;
        adj[boss].push_back(i);
        adj[i].push_back(boss);
    }
    // Initialize
    depth_node[1] = 0;
    dfs(1, -1);
    // Process queries
    while(q--){
        int a, b;
        cin >> a >> b;
        cout << lca(a, b) << "\n";
    }
}

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

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

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

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

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