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

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, int u, int v) {

    int depth_u = find_depth(table, u);
    int depth_v = find_depth(table, v);

    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;

    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));


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

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

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

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

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)