Logo
(追記) (追記ここまで)

33728번 - Post Office 서브태스크다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB173325.000%

문제

In the JOI country, there are $N$ post offices, numbered from 1ドル$ to $N$. Each post office has an assigned ”destination,” and the destination of post office $i$ is post office $P_i$. Note that it is possible for $P_i = i$. If a package is sent from post office $i$ at time $t,ドル it will arrive at post office $P_i$ at time $t + 1$. However, a post office cannot send another package while it is in the process of sending a package. Each post office can store an unlimited number of packages at any given time.

Now, $M$ packages need to be delivered in the JOI country. The $j$-th package arrives at post office $A_j$ at time 0ドル,ドル and it must eventually be delivered to the assigned post office $B_j$. Given the information about the post offices and the packages, write a program to determine whether it is possible to deliver all the packages to their assigned post offices, and if so, find the smallest possible time at which the last package arrives at its assigned post office.

입력

Read the following data from the standard input.

$N$

$P_1$ $P_2$ $\cdots$ $P_N$

$M$

$A_1$ $B_1$

$A_2$ $B_2$

$\vdots$

$A_M$ $B_M$

출력

Output a single line to the standard output. If it is possible to deliver all the packages to their assigned post offices, output the smallest possible time at which the last package arrives at its assigned post office. Otherwise, output -1 instead.

제한

  • 2ドル ≦ N ≦ 200,円 000$.
  • 1ドル ≦ M ≦ 200,円 000$.
  • 1ドル ≦ P_i ≦ N$ (1ドル ≦ i ≦ N$).
  • 1ドル ≦ A_j, B_j ≦ N$ (1ドル ≦ j ≦ M$).
  • $A_j \ne B_j$ (1ドル ≦ j ≦ M$).
  • Given values are all integers.

서브태스크

번호배점제한
13

$N ≦ 3,円 000,ドル $M = 1$.

29

$N ≦ 3,円 000,ドル $M ≦ 3,円 000$.

313

$P = (1, 1, 2, \cdots , N − 1),ドル and $\max(B_1, B_2, \dots , B_M) < \min(A_1, A_2, \dots , A_M)$.

425

$P = (1, 1, 2, \cdots , N − 1)$.

511

$P = (N, 1, 2, \cdots , N − 1)$.

625

$P_1 = 1,ドル $P_i < i$ (2ドル ≦ i ≦ N$).

714

No additional constraints.

예제 입력 1

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

예제 출력 1

3

For example, by sending the packages in the following way, all the packages can be delivered to their assigned post offices by time 3ドル$:

  • At time 0ドル,ドル packages 1ドル,ドル 2ドル,ドル and 3ドル$ are at post office 3ドル$. Send package 2ドル$ to post office 2ドル$.
  • At time 1ドル,ドル package 2ドル$ is at post office 2ドル,ドル and packages 1ドル$ and 3ドル$ are at post office 3ドル$. From post office 2ドル,ドル send package 2ドル$ to post office 1ドル,ドル and from post office 3ドル,ドル send package 3ドル$ to post office 2ドル$.
  • At time 2ドル,ドル package 2ドル$ is at post office 1ドル,ドル package 3ドル$ is at post office 2ドル,ドル and package 1ドル$ is at post office 3ドル$. From post office 2ドル,ドル send package 3ドル$ to post office 1ドル,ドル and from post office 3ドル,ドル send package 1ドル$ to post office 2ドル$.
  • At time 3ドル,ドル packages 2ドル$ and 3ドル$ are at post office 1ドル,ドル and package 1ドル$ is at post office 2ドル$. At this point, all the packages have reached their destinations.

Since it is not possible to deliver all the packages to their assigned post offices by time 2ドル,ドル output 3ドル$.

This sample input satisfies the constraints of Subtasks 2, 3, 4, 6, 7.

예제 입력 2

3
2 1 3
1
1 3

예제 출력 2

-1

No matter how the packages are sent, it is impossible to deliver a package from post office 1ドル$ to post office 3ドル,ドル so output -1.

This sample input satisfies the constraints of Subtasks 1, 2, 7.

예제 입력 3

7
1 1 2 3 4 5 6
6
4 2
5 1
5 3
6 2
7 3
7 6

예제 출력 3

5

This sample input satisfies the constraints of Subtasks 2, 4, 6, 7.

예제 입력 4

4
4 1 2 3
4
4 1
4 1
2 3
2 3

예제 출력 4

4

This sample input satisfies the constraints of Subtasks 2, 5, 7.

예제 입력 5

7
1 1 1 3 3 4 4
5
6 1
6 3
7 1
5 1
5 1

예제 출력 5

5

This sample input satisfies the constraints of Subtasks 2, 6, 7.

예제 입력 6

11
3 1 2 5 6 7 8 4 4 5 10
6
2 1
9 8
11 8
10 4
5 6
5 7

예제 출력 6

6

This sample input satisfies the constraints of Subtasks 2, 7.

힌트

출처

Olympiad > Japanese Olympiad in Informatics > JOI 2024/2025 5번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /