Circle Passing

시간 제한2초메모리 제한1024 MB

요약
2N명의 학생이 원에 둘러앉아 이웃끼리 서로 알고, 길이 N인 절친 M쌍이 추가로 연결될 때 두 학생 사이 최단 경로 길이를 Q번 구한다.
난이도

보통10점 중 7점

유형
그래프, BFS, 최단 경로, 구현
정답자
아직 제출이 없습니다

문제

It is the first day of high school for Anouk; as a warm-up activity, her sports teacher is making the class play name-learning games. There are 2N2N students in the class. Most of them do not know each other, but there are MM pairs of best friends who do everything together. Each student has at most one best friend.

The teacher arranges all of the students in a circle, consecutively assigning each student a number from 00 to 2N−12N - 1. More specifically, for each 0≤i<2N−10 \leq i < 2N - 1, students ii and i+1i + 1 stand next to each other. Additionally, students 00 and 2N−12N - 1 stand next to each other.

Since the teacher wants everyone to meet new students, best friends have to stand as far away from each other as possible, i.e. opposite each other. That is, the students forming the iith pair of best friends are standing at positions k_ik\_i and k_i+Nk\_i + N respectively, where 0≤k_i<N0 \leq k\_i < N.

The teacher selects two students xx and yy and hands a ball to student xx. The goal is to send the ball to student yy, but each student may only pass the ball to another student whose name they already know. Of course, best friends know each other's names. While the rules were explained, each student got to know the names of the two students standing directly beside them. Other than that, no one knows any other names.

The game is played QQ times; the teacher chooses two students each time. Since the students are not paying attention, they do not learn any new names throughout the games. What is the minimum number of passes needed to get the ball from student xx to student yy in each game?

입력

The first line of input contains three integers, NN, MM and QQ, where 2N2N is the number of students in Anouk's class, MM is the number of pairs of best friends, and QQ is the number of games that are played.

The second line contains MM integers k_0,…,k_M−1k\_0, \ldots, k\_{M-1}, with k_ik\_i describing the iith pair of best friends. For each ii, the best friends stand at positions k_ik\_i and k_i+Nk\_i + N respectively. Each student has at most one best friend.

The following QQ lines each contain two integers, x_ix\_i and y_iy\_i, the two selected students in game ii.

출력

Output QQ lines, the iith line containing a single integer, the minimum number of passes needed in game ii.

제한

  • 2≤N≤5⋅1082 \leq N \leq 5 \cdot 10^8.
  • 1≤M≤5⋅1051 \leq M \leq 5 \cdot 10^5 and M≤NM \le N.
  • 1≤Q≤2⋅1041 \leq Q \leq 2 \cdot 10^4.
  • 0≤k_0<k_1<…<k_M−1<N0 \leq k\_0 < k\_1 < \ldots < k\_{M - 1} < N.
  • 0≤x_i,y_i<2N0 \leq x\_i, y\_i < 2N with x_i≠y_ix\_i \neq y\_i.

힌트

The following two figures depict the arrangements in the first and the fourth sample. Two students are connected by an edge if they know each other's names.

In the first game of the first sample, the ball is given to student 11. Student 11 passes the ball to their best friend, student 55. The ball reaches student 44 after student 55 passes it to them, needing two passes in total.

예제5

  1. 예제 1

    입력
    4 1 5
    1
    1 4
    1 5
    1 7
    1 2
    1 6
    
    예상 출력
    2
    1
    2
    1
    2
    
  2. 예제 2

    입력
    6 1 3
    5
    5 7
    5 1
    5 11
    
    예상 출력
    2
    3
    1
    
  3. 예제 3

    입력
    4 2 4
    2 3
    0 2
    0 3
    0 6
    0 7
    
    예상 출력
    2
    2
    2
    1
    
  4. 예제 4

    입력
    5 2 5
    0 4
    0 9
    1 8
    8 3
    1 6
    3 9
    
    예상 출력
    1
    3
    3
    3
    2
    
  5. 예제 5

    입력
    500000000 4 3
    543234 1234566 2300001 249999999
    2334445 123567
    6578996 12455726
    3 269979899
    
    예상 출력
    2210878
    5876730
    231106567