Submission details
Task:Company Queries II
Sender:aalto26gh_039
Submission time:2026-10-22 15:18:46 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#30.00 sdetails
#40.00 sdetails
#50.00 sdetails
#6--details
#70.49 sdetails
#80.59 sdetails
#9--details
#10--details
#110.00 sdetails
#12--details

Code

#include <iostream>
#include <vector>
#include <climits>
using std::cout;
using std::endl;
#define DEBUG std::cout<<"LINE"<<__LINE__;
#define IN(A) int A;std::cin>>A;
#define BUG(A,B) std::cout<<A<<B<<std::endl;

using namespace std;

struct compute {
   vector<vector<int>> tree;
   vector<int> parents;
   vector<int> depth;
  void start(){

      IN(n);
      IN(q);

      tree.assign(n,vector<int>{});

      parents.assign(n,{});
      depth.assign(n,{});
   
   for (int i=1;i<n;i++) {
      IN(temp);
      tree[temp-1].emplace_back(i);
      parents[i]=temp-1;
   }

     bfs(0,0);
     for (int i=0;i<q;i++) {
        IN(a);
        IN(b);
         a=a-1;
        b=b-1;
        while (depth[a]<depth[b]) {
           b=parents[b];
        }

        while (depth[b]<depth[a]) {
           a=parents[a];
        }
        while (a!=b) {
           a=parents[a];
           b=parents[b];
        }
        cout<<a+1<<endl;
     }
  }



  void bfs(int node,int d) {
     depth[node]=d;
     for (auto child:tree[node]) {
        bfs(child,d+1);
     }

  }
};

int main() {

   compute comp;
   comp.start();




   return true;

}

Test details

Test 1

Verdict:

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:

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:

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:

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:

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

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

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:

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)