| Task: | Company Queries II |
| Sender: | Abduvohid |
| Submission time: | 2025-10-11 18:27:17 +0300 |
| Language: | C++ (C++17) |
| Status: | READY |
| Result: | RUNTIME ERROR |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | RUNTIME ERROR | 0.00 s | details |
| #3 | RUNTIME ERROR | 0.00 s | details |
| #4 | RUNTIME ERROR | 0.00 s | details |
| #5 | RUNTIME ERROR | 0.00 s | details |
| #6 | TIME LIMIT EXCEEDED | -- | details |
| #7 | RUNTIME ERROR | 0.03 s | details |
| #8 | RUNTIME ERROR | 0.04 s | details |
| #9 | TIME LIMIT EXCEEDED | -- | details |
| #10 | RUNTIME ERROR | 0.04 s | details |
| #11 | ACCEPTED | 0.00 s | details |
| #12 | TIME LIMIT EXCEEDED | -- | details |
Compiler report
input/code.cpp: In function 'Node* lca(Node*, int, int)':
input/code.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (i = 0; i < path1.size() && i < path2.size(); i++)
| ~~^~~~~~~~~~~~~~
input/code.cpp:58:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (i = 0; i < path1.size() && i < path2.size(); i++)
| ~~^~~~~~~~~~~~~~Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
class Node
{
public:
int data;
Node *left, *right;
Node(int x)
{
data = x;
left = nullptr;
right = nullptr;
}
};
// Function to find path from root to given node.
bool findPath(Node *root, vector<Node *> &path, int n)
{
if (root == nullptr)
return false;
// Store current node value in the path.
path.push_back(root);
if (root->data == n ||
findPath(root->left, path, n) ||
findPath(root->right, path, n))
return true;
// else remove root from path and return false
path.pop_back();
return false;
}
Node *lca(Node *root, int n1, int n2)
{
vector<Node *> path1, path2;
// Find paths from root to n1
// and root to n2.
if (!findPath(root, path1, n1) ||
!findPath(root, path2, n2))
return nullptr;
// Compare the paths to get the first
// different value
int i;
for (i = 0; i < path1.size() && i < path2.size(); i++)
{
if (path1[i] != path2[i])
return path1[i - 1];
}
return path1[i - 1];
}
// g++ -std=c++17 -O2 -pipe -Wall -Wextra a.cpp -o a
int main()
{
fastio();
int n, q;
cin >> n >> q;
vector<int> e(n - 1);
for (int i = 0; i < n - 1; i++)
{
cin >> e[i];
}
vector<Node *> nodes(n + 1);
for (int i = 1; i <= n; i++)
{
nodes[i] = new Node(i);
}
for (int i = 2; i <= n; i++)
{
if (nodes[e[i - 2]]->left == nullptr)
nodes[e[i - 2]]->left = nodes[i];
else
nodes[e[i - 2]]->right = nodes[i];
}
Node *root = nodes[1];
while (q--)
{
int a, b;
cin >> a >> b;
cout << lca(root, a, b)->data << "\n";
}
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: RUNTIME ERROR
| 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 |
|---|
| (empty) |
Test 3
Verdict: RUNTIME ERROR
| 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 |
|---|
| (empty) |
Test 4
Verdict: RUNTIME ERROR
| 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 |
|---|
| (empty) |
Test 5
Verdict: RUNTIME ERROR
| 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 |
|---|
| (empty) |
Test 6
Verdict: TIME LIMIT EXCEEDED
| 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: 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: TIME LIMIT EXCEEDED
| 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: 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: TIME LIMIT EXCEEDED
| 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) |
