Submission details
Task:Company Queries II
Sender:Abduvohid
Submission time:2025-10-11 18:30:03 +0300
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6--details
#7--details
#8--details
#9--details
#10--details
#11ACCEPTED0.00 sdetails
#12--details

Code

#include <bits/stdc++.h>

using namespace std;
#define ll long long

#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);

class Node
{
public:
    int data;
    vector<Node *> children;
    Node(int x)
    {
        data = x;
    }
};

// Function to find path from root to given node.
bool findPath(Node *root, vector<Node *> &path, int n)
{
    if (root == nullptr)
        return false;

    // Store current node value in the path.
    path.push_back(root);

    if (root->data == n)
        return true;

    for (Node *child : root->children)
    {
        if (findPath(child, path, n))
            return true;
    }

    // else remove root from path and return false
    path.pop_back();
    return false;
}

Node *lca(Node *root, int n1, int n2)
{
    vector<Node *> path1, path2;

    // Find paths from root to n1 and root to n2.
    if (!findPath(root, path1, n1) || !findPath(root, path2, n2))
        return nullptr;

    // Compare the paths to get the last common node
    Node *res = nullptr;
    for (size_t i = 0; i < path1.size() && i < path2.size(); i++)
    {
        if (path1[i] == path2[i])
            res = path1[i];
        else
            break;
    }

    return res;
}

// g++ -std=c++17 -O2 -pipe -Wall -Wextra a.cpp -o a
int main()
{
    fastio();

    int n, q;
    cin >> n >> q;

    vector<int> e(n - 1);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> e[i];
    }
    vector<Node *> nodes(n + 1);
    for (int i = 1; i <= n; i++)
    {
        nodes[i] = new Node(i);
    }
    for (int i = 2; i <= n; i++)
    {
        nodes[e[i - 2]]->children.push_back(nodes[i]);
    }
    Node *root = nodes[1];
    while (q--)
    {
        int a, b;
        cin >> a >> b;
        cout << lca(root, a, b)->data << "\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:

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