Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSES Binary Lifting solution #777

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions CSES/Company Queries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// https://cses.fi/problemset/task/1687/
// O(q * log(n) + n*log(n))
// Solution using Binary Lifting
#include <bits/stdc++.h>
using namespace std;

int dfs(int root, vector<vector<int>>& tree, int d,
vector<int>& depth) {
depth[root] = d;
int curr = d;
for(auto &adj : tree[root]) {
curr = max(curr, dfs(adj, tree, d + 1, depth));
}
return curr;
}

int getDepth(int root, vector<int>& par, vector<int>& depth) {
int n = par.size();
vector<vector<int>> tree(n);
for(int i = 1; i < n; ++i) {
tree[par[i]].push_back(i);
}
return dfs(root, tree, 0, depth);
}

int getLOG(int depth) {
int LOG = 0;
while ((1 << LOG) < depth) {
LOG++;
}
return LOG;
}

int goUp(int x, int k, vector<vector<int>>& anc, int LOG) {
for(int i = LOG - 1; i >= 0; --i) {
if ((1 << i) <= k) {
k -= (1 << i);
x = anc[x][i];
}
}
return x;
}

int main() {

int n, q;
cin >> n >> q;
vector<int> par(n);
par[0] = 0;
for(int i = 1; i < n; ++i) {
int boss;
cin >> boss;
boss--;
par[i] = boss;
}

vector<int> depth(n);
int d = getDepth(0, par, depth);
int LOG = getLOG(d) + 1;
vector<vector<int>> anc(n, vector<int>(LOG));

for(int i = 0; i < n; ++i) {
anc[i][0] = par[i];
}

for(int j = 1; j < LOG; ++j) {
for(int i = 1; i < n; ++i) {
anc[i][j] = anc[ anc[i][j - 1] ][j - 1];
}
}

while (q--) {
int x, k;
cin >> x >> k;
x--;

if(k > depth[x]) {
cout << "-1\n";
}
else {
cout << goUp(x, k, anc, LOG) + 1 << "\n";
}
}

return 0;
}