Swaps in a Permutation
Time limit1sMemory limit1024 MB
Given a permutation and a set of allowed position swaps, find the maximum LIS length reachable by applying any number of these swaps.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Union-find, Graph, Sorting
- Solved
- No attempts yet
Problem
You are given a permutation of the numbers from to and a set of index pairs. In one move, you may choose one of these pairs and swap the elements at those two positions, which changes the permutation. You may make any number of moves, including none.
An increasing subsequence of length is a set of indices that satisfies both conditions:
- ;
- .
What is the maximum possible length of the longest increasing subsequence after the swaps?
Input
The first line contains two integers and (, ), the length of the permutation and the number of position pairs that can be swapped.
The second line contains distinct integers (), the elements of the permutation.
Each of the next lines contains two integers and (, ), the positions whose elements can be swapped. No pair appears more than once.
Output
Print one integer: the maximum possible length of the longest increasing subsequence of the permutation after the swaps.
Hint
Consider the permutation from the first example, .
Swap the elements at positions and : .
Now swap the elements at positions and : .
The longest increasing subsequence of this permutation has length . The corresponding subsequence is .