Task: | Company Queries II |
Sender: | ilyas.ben |
Submission time: | 2024-10-24 12:39:37 +0300 |
Language: | C++ (C++20) |
Status: | READY |
Result: | WRONG ANSWER |
test | verdict | time | |
---|---|---|---|
#1 | WRONG ANSWER | 0.03 s | details |
#2 | WRONG ANSWER | 0.03 s | details |
#3 | WRONG ANSWER | 0.03 s | details |
#4 | WRONG ANSWER | 0.03 s | details |
#5 | WRONG ANSWER | 0.03 s | details |
#6 | RUNTIME ERROR | 0.44 s | details |
#7 | RUNTIME ERROR | 0.07 s | details |
#8 | RUNTIME ERROR | 0.13 s | details |
#9 | RUNTIME ERROR | 0.11 s | details |
#10 | RUNTIME ERROR | 0.11 s | details |
#11 | WRONG ANSWER | 0.03 s | details |
#12 | RUNTIME ERROR | 0.13 s | details |
Code
#include <iostream> #include <vector> #include <algorithm> using namespace std; const int MAXN = 200000; const int LOG = 20; vector<long long> tree[MAXN]; long long parent[MAXN][LOG]; long long depth[MAXN]; long long n, q; void dfs(long long node, long long par) { parent[node][0] = par; for (long long i = 1; i < LOG; i++) { if (parent[node][i - 1] != -1) { parent[node][i] = parent[parent[node][i - 1]][i - 1]; } } for (long long child : tree[node]) { if (child != par) { depth[child] = depth[node] + 1; dfs(child, node); } } } long long lca(long long a, long long b) { if (depth[a] < depth[b]) swap(a, b); for (long long i = LOG - 1; i >= 0; i--) { if (depth[parent[a][i]] >= depth[b]) { a = parent[a][i]; } } if (a == b) return a; for (long long i = LOG - 1; i >= 0; i--) { if (parent[a][i] != parent[b][i]) { a = parent[a][i]; b = parent[b][i]; } } return parent[a][0]; } int main() { cin >> n >> q; for (long long i = 0; i < MAXN; i++) { for (long long j = 0; j < LOG; j++) { parent[i][j] = -1; } } for (long long i = 2; i <= n; i++) { long long boss; cin >> boss; tree[boss].push_back(i); } depth[1] = 0; dfs(1, -1); for (long long i = 0; i < q; i++) { long long a, b; cin >> a >> b; cout << lca(a, b) << endl; } return 0; }
Test details
Test 1
Verdict: WRONG ANSWER
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: WRONG ANSWER
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: WRONG ANSWER
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: WRONG ANSWER
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: WRONG ANSWER
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: RUNTIME ERROR
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 |
---|
74862 8750 16237 72298 58111 ... |
Test 7
Verdict: RUNTIME ERROR
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: RUNTIME ERROR
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: RUNTIME ERROR
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: RUNTIME ERROR
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: WRONG ANSWER
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: RUNTIME ERROR
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) |