Cod sursa(job #2447502)
Utilizator | Data | 13 august 2019 15:16:52 | |
---|---|---|---|
Problema | Lowest Common Ancestor | Scor | 90 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.48 kb |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int anc[MAXN];
int main()
{
ifstream fin("lca.in");
ofstream fout("lca.out");
int n, m;
fin >> n >> m;
for(int i = 2; i <= n; ++i)
fin >> anc[i];
while(m){
int x, y;
fin >> x >> y;
while(x != y){
if(x > y) x = anc[x];
else y = anc[y];
}
fout << x << "\n";
m--;
}
return 0;
}