class FunctionalGraph {
int[] f;// [N] から [N] への写像
int[] pre;
int[] order;
int[] period;
public FunctionalGraph(int[] f, int src) {
this.f=f;
int N=f.length;
pre=new int[N];
order=new int[N];
period=new int[N];
Arrays.fill(order, -1);
for (int i=0;; ++i, src=f[src]) {
if (order[src]!=-1) {
period=Arrays.copyOfRange(pre, order[src], i);
pre=Arrays.copyOf(pre, order[src]);
break;
}
pre[i]=src;
order[src]=i;
}
}
// f^x(src)
int get(long x) {
if (x>=pre.length) {
x-=pre.length;
return period[(int)(x%period.length)];
} else {
return pre[(int)x];
}
}
}
Functional Graph
Published in データ構造
Comments