Problem p04034 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define REV(v) reverse(v.begin(), v.end());
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, start, stop) for (int i = start; i < stop; i++)
#define FORR(i, start, stop) for (int i = start; i > stop; i--)
#define SORT(v, n) sort(v, v + n);
#define SORTR(v, n) sort(v, v + n, greater<ll>());
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.begin(), v.end(), greater<ll>());
#define REMOVE(v, n) remove(vector<ll> v, v + v.size(), ll n)
#define ERASE(v, n) v.erase(v.begin() + n);
#define ull unsigned long long
#define pb(a) push_back(a)
#define INF 999999999
#define V(v, i, j) vector(v.begin() + i, v.begin() + j)
#define INSERT(va, vb) va.insert(va.end(), vb.begin(), vb.end())
using namespace std;
typedef vector<ll> vll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
const int MOD = 1000000007;
void chmin(int &a, int b) {
  if (a > b)
    a = b;
}

// vector< vector<int> > v (size1, vector<int>(size2) );

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, M;
  cin >> N >> M;
  vll ball(N, 1); // ボールの個数を格納
  vector<bool> red(M, false); // 赤いボールが入っている可能性のある箱をture
  red[0] = true;

  ll x, y;
  FOR(i, 0, M) {
    cin >> x >> y;
    x--;
    y--;
    if (red[x])
      red[y] = true;
    if (ball[x] == 1)
      red[x] = false;
    ball[x]--;
    ball[y]++;
  }

  // 集計
  ll ans = 0;
  FOR(i, 0, N)
  if (red[i])
    ans++;

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define REV(v) reverse(v.begin(), v.end());
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, start, stop) for (int i = start; i < stop; i++)
#define FORR(i, start, stop) for (int i = start; i > stop; i--)
#define SORT(v, n) sort(v, v + n);
#define SORTR(v, n) sort(v, v + n, greater<ll>());
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.begin(), v.end(), greater<ll>());
#define REMOVE(v, n) remove(vector<ll> v, v + v.size(), ll n)
#define ERASE(v, n) v.erase(v.begin() + n);
#define ull unsigned long long
#define pb(a) push_back(a)
#define INF 999999999
#define V(v, i, j) vector(v.begin() + i, v.begin() + j)
#define INSERT(va, vb) va.insert(va.end(), vb.begin(), vb.end())
using namespace std;
typedef vector<ll> vll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
const int MOD = 1000000007;
void chmin(int &a, int b) {
  if (a > b)
    a = b;
}

// vector< vector<int> > v (size1, vector<int>(size2) );

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, M;
  cin >> N >> M;
  vll ball(N, 1); // ボールの個数を格納
  vector<bool> red(N, false); // 赤いボールが入っている可能性のある箱をture
  red[0] = true;

  ll x, y;
  FOR(i, 0, M) {
    cin >> x >> y;
    x--;
    y--;
    if (red[x])
      red[y] = true;
    if (ball[x] == 1)
      red[x] = false;
    ball[x]--;
    ball[y]++;
  }

  // 集計
  ll ans = 0;
  FOR(i, 0, N)
  if (red[i])
    ans++;

  cout << ans << endl;

  return 0;
}