题意分析
先翻译一下题意:给出 个不大于 的自然数对 ,求有多少个 ,使得 且 与 中的任意数不相等。
我们要判断某一 在 中出现过,因此需要一个布尔数组,存储目前 在序列 中的出现情况。
直接模拟:对于每次输入的 和 ,判断 是否在 中出现过,若没有,将答案增加 ;然后标记 出现过。至于 这一特殊情况,我们在程序开始时将 标记为出现过即可。
代码
AC code
#include <iostream>
using namespace std;
int n, k;
bool flag[100005]; // 出现情况
int ans;
int main(){
cin >> n >> k;
flag[0] = 1;
while(k--){
int x, y;
cin >> x >> y;
ans += !flag[y]; // 等价写法:if(!flag[y]) ans++;
flag[x] = 1;
}
cout << ans;
return 0;
}