Problem p03669 (C++)

Buggy submission (status: Runtime Error):
// Ho-kago Tea Time

#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = (int)1e5, MAXM = 200;

int n, m;

inline void input() { n = read<int>(), m = read<int>(); }

vector<int> adj[MAXN + 5];
int vis[MAXN + 5], edge_cnt = 0;

inline void dfs(int u) {
  vis[u] = 1, edge_cnt += SZ(adj[u]);
  for (auto v : adj[u])
    if (!vis[v])
      dfs(v);
}

inline void solve() {
  static int indeg[MAXM * 2 + 5], outdeg[MAXM * 2 + 5];
  static int plg[MAXM + 5], skt[MAXM + 5], type[MAXM * 2 + 5];
  int cnt = 0;

  for (int i = 1; i <= m; ++i) {
    type[plg[i] = ++cnt] = 0;
    type[skt[i] = ++cnt] = 1;
  }

  for (int i = 1; i <= n; ++i) {
    int a = read<int>(), b = read<int>(), c = read<int>(), d = read<int>();
    int u = c == 0 ? plg[a] : skt[c];
    int v = d == 0 ? skt[b] : plg[d];
    adj[u].push_back(v);
    ++outdeg[u], ++indeg[v];
  }

  int din = 0, dout = 0;
  for (int i = 1; i <= cnt; ++i)
    if (indeg[i] != outdeg[i]) {
      if (outdeg[i] > indeg[i]) {
        if (type[i] == 1) {
          puts("NO");
          return;
        }
        dout += outdeg[i] - indeg[i];
      } else {
        if (type[i] == 0) {
          puts("NO");
          return;
        }
        din += indeg[i] - outdeg[i];
      }
    }
  if (din != dout) {
    puts("NO");
    return;
  }

  for (int i = 1; i <= n; ++i)
    if (!vis[i] && outdeg[i] > indeg[i])
      dfs(i);
  if (edge_cnt < n) {
    puts("NO");
    return;
  }

  puts("YES");
}

int main() {
#ifdef K_ON // K-ON!
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}


Fixed submission:
// Ho-kago Tea Time

#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = (int)1e5, MAXM = 200;

int n, m;

inline void input() { n = read<int>(), m = read<int>(); }

vector<int> adj[MAXN + 5];
int vis[MAXN + 5], edge_cnt = 0;

inline void dfs(int u) {
  vis[u] = 1, edge_cnt += SZ(adj[u]);
  for (auto v : adj[u])
    if (!vis[v])
      dfs(v);
}

inline void solve() {
  static int indeg[MAXM * 2 + 5], outdeg[MAXM * 2 + 5];
  static int plg[MAXM + 5], skt[MAXM + 5], type[MAXM * 2 + 5];
  int cnt = 0;

  for (int i = 1; i <= m; ++i) {
    type[plg[i] = ++cnt] = 0;
    type[skt[i] = ++cnt] = 1;
  }

  for (int i = 1; i <= n; ++i) {
    int a = read<int>(), b = read<int>(), c = read<int>(), d = read<int>();
    int u = c == 0 ? plg[a] : skt[c];
    int v = d == 0 ? skt[b] : plg[d];
    adj[u].push_back(v);
    ++outdeg[u], ++indeg[v];
  }

  int din = 0, dout = 0;
  for (int i = 1; i <= cnt; ++i)
    if (indeg[i] != outdeg[i]) {
      if (outdeg[i] > indeg[i]) {
        if (type[i] == 1) {
          puts("NO");
          return;
        }
        dout += outdeg[i] - indeg[i];
      } else {
        if (type[i] == 0) {
          puts("NO");
          return;
        }
        din += indeg[i] - outdeg[i];
      }
    }
  if (din != dout) {
    puts("NO");
    return;
  }

  for (int i = 1; i <= cnt; ++i)
    if (!vis[i] && outdeg[i] > indeg[i])
      dfs(i);
  if (edge_cnt < n) {
    puts("NO");
    return;
  }

  puts("YES");
}

int main() {
#ifdef K_ON // K-ON!
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}