CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:Nallue
Submission time:2024-10-19 13:54:05 +0300
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#10.00 sdetails
#20.00 sdetails
#30.00 sdetails
#40.01 sdetails
#50.00 sdetails
#60.76 sdetails
#7ACCEPTED0.69 sdetails
#80.73 sdetails
#90.74 sdetails
#100.75 sdetails
#110.00 sdetails
#120.74 sdetails

Compiler report

input/code.cpp: In member function 'int TreeAncestor::getKthAncestor(int, int)':
input/code.cpp:30:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |         for (int j = 0; j < up[0].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class TreeAncestor {
public:
int n;
vector<vector<int>> up;
TreeAncestor(int n, vector<int>& parent) : n(n) {
int maxLog = log2(n) + 1;
up.assign(n, vector<int>(maxLog, -1));
for (int i = 0; i < n; i++) {
up[i][0] = parent[i];
}
for (int j = 1; j < maxLog; j++) {
for (int i = 0; i < n; i++) {
if (up[i][j - 1] != -1) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
}
}
int getKthAncestor(int node, int k) {
for (int j = 0; j < up[0].size(); j++) {
if (k & (1 << j)) {
node = up[node][j];
if (node == -1) return -1;
}
}
return node;
}
};
int main() {
int n,q; // Número de nodos en el árbol (1-indexado, pero usamos 0-indexado en el código)
cin >> n >> q;
// El nodo 0 es una raíz ficticia, y los nodos 1-8 tienen la estructura según el ejemplo
vector<int> parent(n,-1);
for(int i=1; i<n; i++){
int temp;
cin >> temp;
parent[i] = temp-1;
}
TreeAncestor treeAncestor(n, parent);
for(int i=0; i<q; i++){
int a,b;
cin >> a >> b;
a -=1;
b -=1;
vector<bool> jefe(n,false);
int count=1;
int comm;
if(a==0 or b==0){
cout << 1 << endl;
continue;
}
while(true){
int jef1 = treeAncestor.getKthAncestor(a, count);
int jef2 = treeAncestor.getKthAncestor(b, count);
if(jefe[jef1]){
comm = jef1+1;
break;
}
else jefe[jef1]=true;
if(jefe[jef2]){
comm = jef2+1;
break;
}
else jefe[jef2]=true;
}
cout << comm <<endl;
}
}

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

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
4
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
1
3
9
...

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
167533
8749
16236
90860
102869
...
Truncated

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
57441
39658
18686
770
9087
...
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
33593
181356
4231
126193
78223
...
Truncated

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
72601
48542
1630
51761
50395
...
Truncated

Test 11

Verdict:

input
2 4
1
1 1
1 2
2 1
...

correct output
1
1
1
2

user output
1
1
1
1

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
31654
133728
131947
43679
16241
...
Truncated