CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:HFalke
Submission time:2024-10-23 19:45:22 +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
#7ACCEPTED0.08 sdetails
#8ACCEPTED0.10 sdetails
#9--details
#10ACCEPTED0.88 sdetails
#11ACCEPTED0.00 sdetails
#12--details

Code

#include <bits/stdc++.h>

using namespace std;

//Definitions for quicker writing
#define REP(i,a,b) for (int i = a; i < b; i++)
#define PB push_back
#define MP make_pair
#define F first
#define S second

//Typedefs for quicker writing
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int,int> pi;
typedef pair<long long, long long> pl;

//Max values
const long long lmx = LLONG_MAX;
const int imx = INT_MAX;

int main() {
	//IO optimization
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	//Input definition
	int n;
	int q;

	//Read In
	cin >> n >> q;

	int boss[n];
	int level[n];
	boss[0] = 1;
	level[0] = 0;
	REP(i,1,n){
	       	cin >> boss[i];
		level[i] = level[boss[i]-1] + 1;
	}

	pi queries[q];
	REP(i,0,q){
	       	cin >> queries[i].first;
		cin >> queries[i].second;
	}
	
	//Main part
	int ans[q];
	int emp1, emp2;
	int lvl1, lvl2;
	REP(i,0,q){
		emp1 = queries[i].first;
		lvl1 = level[emp1-1];
		emp2 = queries[i].second;	
		lvl2 = level[emp2-1];
		if(lvl1 < lvl2){
			REP(j,0,lvl2-lvl1){
				emp2 = boss[emp2-1];
				//cout << "l2: " << level[emp2]-1 << "\n";
			}
		}
		if(lvl2 < lvl1){
			REP(j,0,lvl1-lvl2){
				emp1 = boss[emp1-1];
				//cout << "l1: " << level[emp1]-1 << "\n";
			}
		}
		while(emp1 != emp2){
			emp1 = boss[emp1-1];
			emp2 = boss[emp2-1];
		}
		ans[i] = emp1;
	}

	//Write out
	REP(i,0,q) cout << ans[i] << "\n";
		
	//Return
	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
...

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

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

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)