Pagini recente » Cod sursa (job #1403984) | Cod sursa (job #2214792) | Cod sursa (job #3275095) | Cod sursa (job #863684) | Cod sursa (job #2217866)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("lca.in");
ofstream g("lca.out");
vector < vector<int>> v, lant;
int tati[100010], nivel[100010], boss[100010], k = 1, care[100010], dp[30][200010], lg[100010];
int nivel_lant[100010];
int DFS(int nod, int nivel_actual)
{
nivel[nod] = nivel_actual;
int ok = 0;
for(int i = 0 ; i < v[nod].size(); i++)
{
ok = 1;
DFS(v[nod][i], nivel_actual + 1);
}
if(ok == 0)
{
lant[k].push_back(nod);
care[nod] = k;
k++;
}
else
{
int pe_care, maxim = 0;
for(int i = 0; i < v[nod].size(); i++)
{
if(lant[care[v[nod][i]]].size() > maxim)
{
maxim = lant[care[v[nod][i]]].size();
pe_care = care[v[nod][i]];
}
}
lant[pe_care].push_back(nod);
care[nod] = pe_care;
for(int i = 0; i < v[nod].size(); i++)
{
if(care[v[nod][i]] != pe_care)
boss[care[v[nod][i]]] = nod;
}
}
return 0;
}
int creez_dinamica(int k)
{
for(int j = 1 ; j < 15; j++)
for(int i = 1; i <= k; i++)
dp[j][i] = dp[j - 1][dp[j - 1][i]];
}
int query(int x, int y)
{
/*if(care[x] == care[y])
{
if(nivel[x] > nivel[y])
return y;
else
return x;
}
else
{
for(int bit = 15; bit >= 0; bit--)
{
if(bit != 0)
{
if(care[dp[bit][x]] == care[dp[bit][y]] && care[dp[bit - 1][x]] != care[dp[bit - 1][y]])
return query(dp[bit - 1][x], dp[bit - 1][y]);
}
else
{
if(dp[bit][y] == dp[bit][x])
return dp[bit][y];
}
}
}*/
while(nivel_lant[care[x]] > nivel_lant[care[y]])
{
// cout << x << " ";
x = boss[care[x]];
}
// cout << x << " ";
// cout << endl;
while(nivel_lant[care[y]] > nivel_lant[care[x]])
{
// cout << y << " ";
y = boss[care[y]];
}
// cout << y << " ";
if(care[x] == care[y])
{
if(nivel[x] > nivel[y])
return y;
else
return x;
}
int carex = care[x];
int carey = care[y];
for(int bit = 12; bit >= 0; bit--)
{
if(bit != 0)
{
if(dp[bit][carex] == dp[bit][carey] && dp[bit - 1][carex] != dp[bit - 1][carey])
{
return query(dp[bit - 1][carex], dp[bit - 1][carey]);
}
}
else
{
if(dp[bit][carex] == dp[bit][carey])
return dp[bit][carey];
}
}
}
void debug()
{
g << "-----------DEBUG-----------" << endl;
g << "LANTURI:" << '\n';
for(int i = 1; i <= k; i++)
{
g << "Lant " << i << ": ";
for(int j = 0; j < lant[i].size(); j++)
g << lant[i][j] << " ";
g << "Boss-ul: " << boss[i] << " Nivelul: " << nivel_lant[i];
g << endl;
}
g << "DINAMICA AIA BLANAO: " << '\n';
for(int j = 0 ; j < 5; j++)
{
for(int i = 1; i <= k; i++)
g << "Lantul: " << i << " stramosul: " << (1 << j) << " " << dp[j][i] << endl;
g << endl;
}
}
void nivel_lant_brut()
{
for(int i = 0 ; i <= k; i++)
{
int aux = boss[i];
while(aux != 0)
{
aux = boss[care[aux]];
nivel_lant[i]++;
}
}
}
int main()
{
int n, m;
f >> n >> m;
v.resize(n + 2);
lant.resize(n + 2);
for(int i = 2; i <= n; i++)
{
int x;
f >> x;
tati[i] = x;
v[x].push_back(i);
lg[i] = lg[i>>1] + 1;
}
DFS(1, 0);
k--;
care[0] = care[1];
for(int i = 1; i <= k; i++)
{
dp[0][i] = boss[i];
}
creez_dinamica(k);
nivel_lant_brut();
// debug();
for(int i = 0 ; i < m ;i++)
{
int x, y, rezultat;
f >> x >> y;
rezultat = query(x, y) ;
if(rezultat == 0)
g << 1 << '\n';
else
g << rezultat << '\n';
}
}