CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:MallocManfred
Submission time:2024-10-10 15:45:04 +0300
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.71 sdetails
#7ACCEPTED0.60 sdetails
#8ACCEPTED0.67 sdetails
#9ACCEPTED0.76 sdetails
#10ACCEPTED0.73 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.77 sdetails

Code

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define vout(x) for(int i=0;i<(long long)x.size();i++) printf("%lld ",x[i]);

// g++ <filename>.cpp -g -Wall -Wextra -DDEBUG -o <executable>

// copied from: https://codeforces.com/blog/entry/79024
// === Debug macro starts here ===

int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

// === Debug macro ends here ===

const int MAX_DEPTH = 20;


int get_kth_ancestor(vector<vector<int>> &table, int node, int k) {

    for (int j = 0; j < MAX_DEPTH; j++) {
        if (k & (1 << j)) {
            node = table[j][node];
            if (node == 0) return -1;
        }
    }
    return node; 
};

int find_depth(vector<vector<int>> &table, int node) {
    int depth = 0;
    while (node != 0) {
        node = table[0][node];
        depth++;
    }
    return depth;
}

int find_lca(vector<vector<int>> &table, vector<int> &depth, int u, int v) {

    // 1. equalize depth
    if (depth[u] > depth[v]) {
        u = get_kth_ancestor(table, u, depth[u] - depth[v]);
    } else if (depth[v] > depth[u]) {
        v = get_kth_ancestor(table, v, depth[v] - depth[u]);
    }

    if (u == v) return u;

    // 2. move up node
    for (int j = MAX_DEPTH - 1; j >= 0; j--) {
        if (table[j][u] != table[j][v]) {
            u = table[j][u];
            v = table[j][v]; 
        }
    }


    return table[0][u];
}

signed main() {
    
    int n, q;
    cin >> n >> q;
    
    vector<vector<int>> table(MAX_DEPTH, vector<int>(n+1, 0));
    vector<int> depth(n+1, 0);

    for (int i = 2; i <= n; i++) {
        cin >> table[0][i];
        depth[i] = depth[table[0][i]] + 1;
    }

    for (int j = 1; j < MAX_DEPTH; j++) { 
        for (int i = 1; i <= n; i++) { 
            if (table[j-1][i] != 0) {
                table[j][i] = table[j-1][table[j-1][i]];
            }
        }
    }

    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;

        int lca = find_lca(table, depth, u, v);
        cout << lca << "\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