Graph query - MarisaOJ: Marisa Online Judge

Graph query

Time limit: 1000 ms
Memory limit: 256 MB
Given a directed graph of $n$ vertices and $n$ edges. Each vertex has exactlyy one outgoing edge. Given $q$ queries of the form $(u, x)$. Each query represents a scenario where we start at vertex $u$ and need to determine the vertex we will reach after traversing through $x$ consecutive vertices. ### Input - The first line contains two integers $n,q$. - The next lines contains $n$ intergers $V_i$, there is a directed edge from $i$ to $V_i$. - The next $q$ lines, each line contains two integers $u, x$, a query. ### Output - Print the answer for each query. ### Constraints - $1 \le n \le 10^5$. - $1 \le u,V_i \le n$. - $1 \le x \le 10^9$. ### Example Input: ``` 3 2 2 3 1 1 3 2 2 ``` Output: ``` 1 1 ```
Topic
Graph
Data structure
Rating 1500
Solution (1) Solution