Nastya Is Buying Lunch

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.

Input

The first line contains two integers nn and mm (1n31051≤n≤3⋅105, 0m51050≤m≤5⋅105) — 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 p1p1, p2p2, ..., pnpn — the initial arrangement of pupils in the queue, from the queue start to its end (1pin1≤pi≤n, pp is a permutation of integers from 11 to nn). In other words, pipi 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 uiui, vivi (1ui,vin,uivi1≤ui,vi≤n,ui≠vi), denoting that the pupil with number uiui agrees to change places with the pupil with number vivi if uiui is directly in front of vivi. It is guaranteed that if iji≠j, than vivjvi≠vj or uiujui≠uj. 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 pnpn.

Output

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

Examples

Input
2 1
1 2
1 2
Output
1
Input
3 3
3 1 2
1 2
3 1
3 2
Output
2
Input
5 2
3 1 5 4 2
5 2
5 4
Output
1

Note

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.

#include<vector>
#include<iostream>
#include<cstdio>
#include<algorithm>
const int N = 3e5+10;
using namespace std;
vector<int >v[N];
int num[N],pos[N],cnt[N];
int main()
{
	int i,n,m,ans = 0,x,a,b;
	scanf("%d%d",&n,&m);
	for(i = 1; i <= n; i++){
		scanf("%d",&x);
		num[i] = x;
		pos[x] = i;
	}
	for(i = 0; i < m; i++){
		scanf("%d%d",&a,&b);
		if(pos[b] > pos[a]){
			v[b].push_back(a);
		}
	}
	for(i = 0; i<v[num[n]].size(); i++){
		cnt[v[num[n]][i]]++;
	}
	for(i = n-1; i > 0; i--){
		if(cnt[num[i]] == n-i-ans){
			ans++;
		}
		else{
			for(int j = 0; j < v[num[i]].size(); i++){
				cnt[v[num[i]][j]]++;
			}
		}
	}
	printf("%d
",ans);
	return 0;
}

  

原文地址:https://www.cnblogs.com/clb123/p/10596599.html