Problem p04034 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)
#define Rep(i, r, n) for (ll i = r; i < n; i++)
#define RRep(i, r, n) for (ll i = n - 1; i <= r; i--)
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define int long long
#define pb push_back
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};
const int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30)-1;
const int INF = (1LL << 60) - 1;
const double EPS = (1 >> 30);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

using Graph = vector<vector<pair<int, int>>>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  //---------------------------------------------

  int n, m;
  cin >> n >> m;
  vector<P> a(n);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    a[i] = P(x, y);
  }
  vector<P> b(n);
  rep(i, n) { b[i].fs = 1; }
  b[0].sc = 1;
  rep(i, m) {
    int x = a[i].fs;
    int y = a[i].sc;
    x--, y--;
    if (b[x].fs == 1 && b[x].sc == 1) {
      b[x].sc = 0;
      b[y].sc = 1;
    } else if (b[x].sc == 1) {
      b[y].sc = 1;
    }
    b[x].fs--;
    b[y].fs++;
  }
  ll ans = 0;
  rep(i, n) {
    if (b[i].sc == 1) {
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)
#define Rep(i, r, n) for (ll i = r; i < n; i++)
#define RRep(i, r, n) for (ll i = n - 1; i <= r; i--)
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define int long long
#define pb push_back
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};
const int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30)-1;
const int INF = (1LL << 60) - 1;
const double EPS = (1 >> 30);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

using Graph = vector<vector<pair<int, int>>>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);
  //---------------------------------------------

  int n, m;
  cin >> n >> m;
  vector<P> a(m);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    a[i] = P(x, y);
  }
  vector<P> b(n);
  rep(i, n) { b[i].fs = 1; }
  b[0].sc = 1;
  rep(i, m) {
    int x = a[i].fs;
    int y = a[i].sc;
    x--, y--;
    if (b[x].fs == 1 && b[x].sc == 1) {
      b[x].sc = 0;
      b[y].sc = 1;
    } else if (b[x].sc == 1) {
      b[y].sc = 1;
    }
    b[x].fs--;
    b[y].fs++;
  }
  ll ans = 0;
  rep(i, n) {
    if (b[i].sc == 1) {
      ans++;
    }
  }
  cout << ans << endl;
}