A12379 | Nastya Is Buying Lunch
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
At the big break Nastya came to the school dining room. There are $n$ pupils in the school, numbered from $1$ to $n$ . 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 $u$ , $v$ such that if the pupil with number $u$ stands directly in front of the pupil with number $v$ , 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.
Formally, there are some pairs $u$ , $v$ such that if the pupil with number $u$ stands directly in front of the pupil with number $v$ , 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 $n$ and $m$ ( $1 \leq n \leq 3 \cdot 10^{5}$ , $0 \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 $n$ integers $p_1$ , $p_2$ , ..., $p_n$ — the initial arrangement of pupils in the queue, from the queue start to its end ( $1 \leq p_i \leq n$ , $p$ is a permutation of integers from $1$ to $n$ ). In other words, $p_i$ is the number of the pupil who stands on the $i$ -th position in the queue.
The $i$ -th of the following $m$ lines contains two integers $u_i$ , $v_i$ ( $1 \leq u_i, v_i \leq n, u_i \neq v_i$ ), denoting that the pupil with number $u_i$ agrees to change places with the pupil with number $v_i$ if $u_i$ is directly in front of $v_i$ . It is guaranteed that if $i \neq j$ , than $v_i \neq v_j$ or $u_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 $p_n$ .
The second line contains $n$ integers $p_1$ , $p_2$ , ..., $p_n$ — the initial arrangement of pupils in the queue, from the queue start to its end ( $1 \leq p_i \leq n$ , $p$ is a permutation of integers from $1$ to $n$ ). In other words, $p_i$ is the number of the pupil who stands on the $i$ -th position in the queue.
The $i$ -th of the following $m$ lines contains two integers $u_i$ , $v_i$ ( $1 \leq u_i, v_i \leq n, u_i \neq v_i$ ), denoting that the pupil with number $u_i$ agrees to change places with the pupil with number $v_i$ if $u_i$ is directly in front of $v_i$ . It is guaranteed that if $i \neq j$ , than $v_i \neq v_j$ or $u_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 $p_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 $1$ and $3$ .
- change places for pupils with numbers $3$ and $2$ .
- change places for pupils with numbers $1$ and $2$ .
The queue looks like $[3, 1, 2]$ , then $[1, 3, 2]$ , then $[1, 2, 3]$ , and finally $[2, 1, 3]$ after these operations.
Optimal sequence of changes in the second example is
- change places for pupils with numbers $1$ and $3$ .
- change places for pupils with numbers $3$ and $2$ .
- change places for pupils with numbers $1$ and $2$ .
The queue looks like $[3, 1, 2]$ , then $[1, 3, 2]$ , then $[1, 2, 3]$ , and finally $[2, 1, 3]$ after these operations.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted