CF1136D.Nastya Is Buying Lunch

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

At the big break Nastya came to the school dining room. There are nn pupils in the school, numbered from 11 to nn . Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils.

Formally, there are some pairs uu , vv such that if the pupil with number uu stands directly in front of the pupil with number vv , Nastya can ask them and they will change places.

Nastya asks you to find the maximal number of places in queue she can move forward.

输入格式

The first line contains two integers nn and mm ( 1n31051 \leq n \leq 3 \cdot 10^{5} , 0m51050 \leq m \leq 5 \cdot 10^{5} ) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second.

The second line contains nn integers p1p_1 , p2p_2 , ..., pnp_n — the initial arrangement of pupils in the queue, from the queue start to its end ( 1pin1 \leq p_i \leq n , pp is a permutation of integers from 11 to nn ). In other words, pip_i is the number of the pupil who stands on the ii -th position in the queue.

The ii -th of the following mm lines contains two integers uiu_i , viv_i ( 1ui,vin,uivi1 \leq u_i, v_i \leq n, u_i \neq v_i ), denoting that the pupil with number uiu_i agrees to change places with the pupil with number viv_i if uiu_i is directly in front of viv_i . It is guaranteed that if iji \neq j , than vivjv_i \neq v_j or uiuju_i \neq u_j . Note that it is possible that in some pairs both pupils agree to change places with each other.

Nastya is the last person in the queue, i.e. the pupil with number pnp_n .

输出格式

Print a single integer — the number of places in queue she can move forward.

输入输出样例

  • 输入#1

    2 1
    1 2
    1 2
    

    输出#1

    1
  • 输入#2

    3 3
    3 1 2
    1 2
    3 1
    3 2
    

    输出#2

    2
  • 输入#3

    5 2
    3 1 5 4 2
    5 2
    5 4
    

    输出#3

    1

说明/提示

In the first example Nastya can just change places with the first pupil in the queue.

Optimal sequence of changes in the second example is

  • change places for pupils with numbers 11 and 33 .
  • change places for pupils with numbers 33 and 22 .
  • change places for pupils with numbers 11 and 22 .

The queue looks like [3,1,2][3, 1, 2] , then [1,3,2][1, 3, 2] , then [1,2,3][1, 2, 3] , and finally [2,1,3][2, 1, 3] after these operations.

首页