Under construction

Still being built. Things may change

Swaps in a Permutation

Time limit1sMemory limit1024 MB

Summary
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 aa of the numbers from 11 to nn and a set of mm index pairs. In one move, you may choose one of these mm 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 kk is a set of indices j1,j2,…,jkj_1, j_2, \ldots, j_k that satisfies both conditions:

  • 1≤j1<j2<…<jk≤n1 \le j_1 < j_2 < \ldots < j_k \le n;
  • aj1<aj2<…<ajka_{j_1} < a_{j_2} < \ldots < a_{j_k}.

What is the maximum possible length of the longest increasing subsequence after the swaps?

Input

The first line contains two integers nn and mm (1≤n≤1041 \le n \le 10^4, 0≤m≤min⁡(105,n⋅(n−1)2)0 \le m \le \min(10^5, \frac{n \cdot (n - 1)}{2})), the length of the permutation and the number of position pairs that can be swapped.

The second line contains nn distinct integers aia_i (1≤ai≤n1 \le a_i \le n), the elements of the permutation.

Each of the next mm lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i), 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, [5,2,4,6,3,1][5, 2, 4, 6, 3, 1].

Swap the elements at positions 55 and 66: [5,2,4,6,1,3][5, 2, 4, 6, 1, 3].

Now swap the elements at positions 11 and 55: [1,2,4,6,5,3][1, 2, 4, 6, 5, 3].

The longest increasing subsequence of this permutation has length 44. The corresponding subsequence is [1,2,4,6,5,3][1, 2, 4, 6, 5, 3].

Examples2

  1. Example 1

    Input
    6 2
    5 2 4 6 3 1
    5 6
    1 5
    
    Expected output
    4
    
  2. Example 2

    Input
    4 2
    2 1 4 3
    1 3
    2 4
    
    Expected output
    3