Pagini recente » Cod sursa (job #2184276) | Cod sursa (job #2346897) | Cod sursa (job #532193) | Cod sursa (job #2636395) | Cod sursa (job #2908122)
#include <fstream>
#include <vector>
#include <cmath>
std::ifstream in("lca.in");
std::ofstream out("lca.out");
constexpr int N = 1e5+1, L = 17;
std::vector<int> g[N];
int ancestor[L][N], ti[N], to[N], currTime, power[N]; // ancestor[e][i] -> e'th ancestor of node i
void dfs(int i){
ti[i] = ++currTime;
for(auto child : g[i])
dfs(child);
to[i] = ++currTime;
}
bool is_ancestor(int x, int y){ // x is ancestor of y
return (ti[x] <= ti[y] && to[x] >= to[y]);
}
int lca(int x, int y){
if(is_ancestor(x, y))
return x;
for(int e=power[x]; e>=0; --e){
if(ancestor[e][x] != 0 && !is_ancestor(ancestor[e][x], y))
x = ancestor[e][x];
}
return ancestor[0][x];
}
int main(){
int n, q;
in >> n >> q;
in.close();
for(int i=2; i<=n; ++i){
in >> ancestor[0][i];
g[ancestor[0][i]].push_back(i);
}
for(int e=1; (1 << e) <= n; ++e){
for(int i=1; i<=n; ++i){
ancestor[e][i] = ancestor[e-1][ancestor[e-1][i]];
power[i] = e * (ancestor[e][i] > 0);
}
}
dfs(1);
for(int i=0; i<q; ++i){
int x, y;
in >> x >> y;
out << lca(x, y) << '\n';
}
out.close();
return 0;
}