Task: | Company Queries II |
Sender: | Nallue |
Submission time: | 2024-10-19 16:05:41 +0300 |
Language: | C++ (C++11) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.01 s | details |
#6 | ACCEPTED | 0.84 s | details |
#7 | ACCEPTED | 0.57 s | details |
#8 | ACCEPTED | 0.70 s | details |
#9 | ACCEPTED | 0.87 s | details |
#10 | ACCEPTED | 0.79 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | ACCEPTED | 0.96 s | details |
Code
#include <iostream>#include <vector>using namespace std;class CompanyHierarchy {private:const int LOG = 20; // log2(200000) ~ 17, so 20 is a safe valuevector<vector<int>> adj; // adjacency list for the hierarchyvector<vector<int>> parent; // parent table for binary liftingvector<int> depth; // depth of each employee// DFS to populate parent and depth arraysvoid dfs(int node, int par) {parent[node][0] = par; // direct parent of nodefor (int i = 1; i < LOG; i++) {parent[node][i] = parent[parent[node][i-1]][i-1];}for (int child : adj[node]) {if (child != par) {depth[child] = depth[node] + 1;dfs(child, node);}}}// Binary lifting to find the lowest common ancestorint lca(int u, int v) {if (depth[u] < depth[v]) swap(u, v);int diff = depth[u] - depth[v];for (int i = 0; i < LOG; i++) {if (diff & (1 << i)) {u = parent[u][i];}}if (u == v) return u;for (int i = LOG - 1; i >= 0; i--) {if (parent[u][i] != parent[v][i]) {u = parent[u][i];v = parent[v][i];}}return parent[u][0];}public:CompanyHierarchy(int n) {adj.resize(n + 1); // employee numbers from 1 to nparent.assign(n + 1, vector<int>(LOG)); // binary lifting tabledepth.resize(n + 1); // depth array}// Add an employee and their boss relationshipvoid add_employee(int employee, int boss) {adj[boss].push_back(employee);}// Preprocess the hierarchy starting from the general director (node 1)void preprocess() {depth[1] = 0;dfs(1, 0);}// Answer a query about the lowest common bossint query(int a, int b) {return lca(a, b);}};int main() {int n, q;cin >> n >> q;// Create the company hierarchy objectCompanyHierarchy company(n);// Read the hierarchyfor (int i = 2; i <= n; i++) {int boss;cin >> boss;company.add_employee(i, boss);}// Preprocess to prepare for LCA queriescompany.preprocess();while (q--) {int a, b;cin >> a >> b;cout << company.query(a, b) << "\n";}}
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: ACCEPTED
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 ... 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: 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 ... Truncated |
Test 9
Verdict: ACCEPTED
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 |
---|
2796 633 633 151 2690 ... Truncated |
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 ... Truncated |
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: ACCEPTED
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 |
---|
27468 6353 27468 6353 6353 ... Truncated |