Editorial for Bài A
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Bài A
Thuật toán:
Chúng ta sẽ đến với 1 cấu trúc dữ liệu là bảng thưa
Gọi
Ví dụ
Vậy chúng ta sẽ tìm
Chúng ta thấy rằng
Giả sử: Hiện tại ta có cha của
Áp dụng
Khi đó
Vì dồ thị chỉ có
Ta thấy, vì
Áp dụng Logarit thì ta có
CODE:
Copy
const int LOG=17;
void sprase_table()
{
for(int i=1;i<=LOG;i++)
{
for(int node=1;node<=n;node++)
{
int par=trace[node][i-1];
if(par!=-1)// Nếu x có nút cha cách nó độ cao 2 ^(i-1)
{
trace[node][i]=trace[par][i-1];
}
}
}
}
Comments
https://ideone.com/1b8KYd