解题思路
题目统计使用未初始化变量的语句数,那么只需要依次判断每一句的右值变量是否已经初始化
所以可以开辟一个数组进行标记该变量是否已经初始化
依次读入每一句赋值语句,判断右值是否初始化,并令左值标记已初始化即可
代码实现
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 100010;
bool a[N]; //记录该变量是否已经初始化
int n, k;
int main()
{
cin >> n >> k;
a[0] = 1; //a[0]为常量,不需要初始化
int res = 0;
while (k --)
{
int x, y;
cin >> x >> y;
if (!a[y]) res ++; //右值未初始化,则答案加一
a[x] = 1; //另左值标记为已初始化
}
cout << res;
return 0;
}