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

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:15:19: warning: comparison of integer expressions of different signedness: 'int' and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
   15 |     for(int i=0; i<n-1; i++){
      |                  ~^~~~
input/code.cpp:37:19: warning: comparison of integer expressions of different signedness: 'int' and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
   37 |     for(int i=0; i<q; i++){
      |                  ~^~

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int d[200001];

int main(){
    ull n, q;
    cin >> n >> q;

    map<int,vector<int>> e;
    map<int,int> boss;

    for(int i=0; i<n-1; i++){
        int b;
        cin >> b;
        e[b].push_back(i+2);
        boss[i+2] = b;
    }

    d[1] = 0;
    stack<int> s;
    s.push(1);
    while(!s.empty()){
        stack<int> s2;
        while(!s.empty()){
            int sa = s.top(); s.pop();
            for(int e : e[sa]){
                d[e] = d[sa] + 1;
                s2.push(e);
            }
        }
        s = s2;
    }

    for(int i=0; i<q; i++){
        int a,b;
        cin >> a >> b;

        int da = d[a];
        int db = d[b];
        if(da > db){
            swap(da, db);
            swap(a,b);
        }

        for(; da < db; db--){
            b = boss[b];
        }

        while(b != a){
            b = boss[b];
            a = boss[a];
        }
        cout << b << endl;
    }
}

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:

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)