Restaurant Recommendation Rescue
시간 제한2초메모리 제한2048 MB
배열 B가 주어지고 원소 교환이 여러 번 일어날 때, K의 추천 알고리즘이 만들 수 있는 배열 A와 일치하는 모든 순환 시프트 k의 개수와 합을 각 단계마다 구한다.
문제
A certain aspiring musician K loves going for shabu-shabu! Recently, she’s been to shabushabu restaurants, numbered , following the following algorithm:
- K keeps an ordered list of recommendations, starting with restaurant .
- On the -th day, she visits the next recommended restaurant on her list, which recommends her restaurants .
- K appends to her list of restaurants to visit.
- K repeats steps 2-4 until she runs out of recommended restaurants.
- K writes down the array , where equals the number of restaurants she was recommended on the -th day. That is, .
It is guaranteed that and for , that is, every restaurant, other than the first, will be recommended by exactly one other restaurant.
Once K finishes her list, K’s delinquent friend H decides to play a prank on her! She replaces the array with another array ! K thinks that this new array might just be a cyclic shift of her array, so she asks you to determine all possible such that , for all and any valid output of her algorithm .
Furthermore, K will then perform operations, where for the -th operation, she swaps , and asks you to do the same on the new array. Can you help K see through her friend’s prank?
입력
The first line of input will contain two integers, () and ().
The next line of input will contain space-separated non-negative integers, (), the initial sequence.
The -th of the next lines of input will contain two integers each, and ( and ), indicating you are to swap with .
출력
For each of the arrays (including the initial array ), let denote the set of integers such that there exists a valid output of K’s algorithm such that for all . Output, on a single line, the integers and , separated by a space.
In particular, if , your output should be 0 0.