ダブリング
dub(vector
引数のvectorはそれぞれの場所について次の日に行く場所を記録
query(x,y)で今xにいる時y日後の場所がわかる
class dub{
public:
vector<vector<int>> d;
int n;
dub(vector<int>& a){
// 今の場所 i
// 次の日 a[i]
// のvectorを入れる
n = a.size();
d.assign(n,vector<int>(64));
rep(i,n){
d[i][0] = a[i];
}
for(int j = 1;j < 64;j++){
for(int i = 0;i < n;i++){
d[i][j] = d[d[i][j-1]][j-1];
}
}
}
int query(int x,int y){
// 今xにいる時y日後にいるところ
bitset<64> b(y);
int now = x;
rep(i,64){
if(b.test(i)){
now = d[now][i];
}
}
return now;
}
};