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

34770번 - Baralho Alho 다국어

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

문제

Researcher Isadora loves playing cards with her friends. More specifically, she plays a version called Baralho Alho, in which there are $N$ cards (duplicates are allowed). Initially, the $N$ cards are in a specific order: the $i$-th card has value $A_i$. Two cards are considered equal if they have the same value.

Before the game starts, Isadora declares: “I always shuffle Baralho Alho.” Naively, her friends agree and let her command the shuffling. Little do they know that Researcher Isadora loves to cheat. Her goal is to shuffle in such a way that, at the end of the process, the $i$-th card has value $B_i$.

However, she only knows one type of shuffling: it maps the card originally at position $i$ to position $P_i$. For example, if $P = [3, 2, 4, 1],ドル then the first card goes to the third position, the second remains in place, the third goes to the fourth, and the fourth goes to the first. Thus, if the initial deck is $[4, 2, 6, 1],ドル after applying the shuffling, Isadora gets $[1, 2, 4, 6]$.

Even with this limitation, Isadora is quite intelligent and plans to repeat the shuffling several times in order to reach new deck configurations.

Write a program that, given $A_i,ドル $B_i,ドル and $P_i,ドル determines the minimum number of times Isadora needs to apply the shuffling so that the deck is in the desired order. If this is impossible, print “IMPOSSIVEL” (without quotes). If the minimum number of shuffles is greater than 10ドル^9,ドル print “DEMAIS” (without quotes).

입력

The first line of input contains an integer $N$ (1ドル ≤ N ≤ 10^6$).

The second line contains $N$ integers $A_i$ (1ドル ≤ A_i ≤ 10^9$), representing the initial configuration of the deck.

The third line contains $N$ integers $B_i$ (1ドル ≤ B_i ≤ 10^9$), representing the desired final configuration of the deck.

The fourth line contains $N$ distinct integers $P_i$ (1ドル ≤ P_i ≤ N$), indicating that the card in position $i$ goes to position $P_i$ after one application of the shuffling.

출력

Print a single integer $k$: the minimum number of times the shuffling must be applied, starting from $A_i,ドル until the resulting configuration is $B_i$.

If this is impossible, print “IMPOSSIVEL” (without quotes).

If the minimum $k$ is greater than 10ドル^9,ドル print “DEMAIS” (without quotes).

제한

예제 입력 1

6
8 6 5 5 1 3
5 1 8 5 3 6
2 3 6 5 1 4

예제 출력 1

2

We can see the deck’s configuration after each number of shuffles $k$:

  • $k = 0$: the deck is in the order 8,ドル 6, 5, 5, 1, 3$;
  • $k = 1$: the deck is in the order 1,ドル 8, 6, 3, 5, 5$;
  • $k = 2$: the deck is in the order 5,ドル 1, 8, 5, 3, 6$.

Therefore, the answer is $k = 2$.

예제 입력 2

2
3 3
3 3
1 2

예제 출력 2

0

In this case, the deck is already in the desired configuration, so no shuffle is needed.

예제 입력 3

5
6 3 8 4 2
3 6 4 2 8
2 1 4 5 3

예제 출력 3

5

예제 입력 4

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

예제 출력 4

IMPOSSIVEL

예제 입력 5

3
1 2 3
2 1 4
1 2 3

예제 출력 5

IMPOSSIVEL

노트

출처

ICPC > Regionals > Latin America > Sub-Regional Brasil do ACM ICPC > Maratona SBC de Programação 2025 B번

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

출처

대학교 대회

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

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