#P7965. Toy Box Transformation

    ID: 21149 Type: Default 1000ms 256MiB

Toy Box Transformation

Toy Box Transformation

Matrin has \( n \) boxes numbered from \( 1 \) to \( n \). Initially, box \( i \) contains toy \( i \). He invited \( m \) friends to play with the toys. Each friend, after playing with the toys, moves the toy originally in box \( i \) to box \( p_i \) for all \( i \) (i.e. they apply the mapping \( f(i)=p_i \) on all boxes).

You are given \( q \) queries. In each query, you are allowed to invite friends in any order (each friend can be invited arbitrarily many times) to apply the transformation. Given two integers \( a \) and \( b \), determine whether there exists a sequence of operations such that toy \( a \) eventually ends up in box \( b \).

inputFormat

The first line contains two integers \( n \) and \( m \) (\( 1 \leq n, m \leq 10^5 \)) representing the number of boxes and the number of friends, respectively.

The second line contains \( n \) integers \( p_1, p_2, \ldots, p_n \) (\( 1 \leq p_i \leq n \)) indicating that each friend moves the toy from box \( i \) to box \( p_i \) when they play with the toys.

The third line contains an integer \( q \) (\( 1 \leq q \leq 10^5 \)) representing the number of queries.

Each of the next \( q \) lines contains two integers \( a \) and \( b \) (\( 1 \leq a, b \leq n \)), which represent a query asking if it is possible for toy \( a \) to eventually be moved to box \( b \) through a sequence of operations.

outputFormat

For each query, output Yes if it is possible to move toy \( a \) to box \( b \), or No otherwise. Each answer should be printed on a new line.

sample

5 3
2 3 4 5 1
3
1 4
2 1
3 5
Yes

Yes Yes

</p>