Problem p03669 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#define MAXN 1010
#define MAXM 1000010

int n, h;
int g[MAXN], nume;
int p[MAXN][2], nump;
int d[MAXN], size[MAXN], sd[MAXN][2];
bool tag[MAXN];
int f[MAXN];

int getf(int x) {
  if (f[x] == x)
    return x;
  return f[x] = getf(f[x]);
}

int main() {
#ifdef DEBUG
  freopen("E.in", "r", stdin);
#endif
  for (int i = 1; i < MAXN; i++)
    f[i] = i;
  memset(g, -1, sizeof g);
  scanf("%d%d", &n, &h);
  for (int i = 1; i <= h; i++)
    p[i][0] = ++nump;
  for (int i = 1; i <= h; i++)
    p[i][1] = ++nump;
  for (int i = 1; i <= n; i++) {
    int t1, t2, t3, t4, u, v;
    scanf("%d%d%d%d", &t1, &t2, &t3, &t4);
    if (!t3)
      u = p[t1][1];
    else
      u = p[t3][0];
    if (!t4)
      v = p[t2][0];
    else
      v = p[t4][1];
    if (getf(u) ^ getf(v))
      f[f[u]] = f[v];
    d[u]--;
    d[v]++;
    tag[u] = tag[v] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (d[p[i][0]] < 0 || d[p[i][1]] > 0) {
      puts("NO");
      return 0;
    }
  for (int i = 1; i <= nump; i++) {
    size[getf(i)]++;
    if (d[i] < 0)
      sd[getf(i)][0] += -d[i];
    else
      sd[getf(i)][1] += d[i];
    if (tag[i])
      tag[getf(i)] = 1;
  }
  int numc = 0;
  int s0 = 0, s1 = 0;
  for (int i = 1; i <= nump; i++)
    if (getf(i) == i && tag[i]) {
      if (!sd[i][0] && !sd[i][1]) {
        puts("NO");
        return 0;
      }
      s0 += sd[i][0];
      s1 += sd[i][1];
      numc++;
    }
  if (s0 ^ s1) {
    puts("NO");
    return 0;
  }
  if (s0 < numc - 1) {
    puts("NO");
    return 0;
  }
  puts("YES");
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#define MAXN 1000010

int n, h;
int g[MAXN], nume;
int p[MAXN][2], nump;
int d[MAXN], size[MAXN], sd[MAXN][2];
bool tag[MAXN];
int f[MAXN];

int getf(int x) {
  if (f[x] == x)
    return x;
  return f[x] = getf(f[x]);
}

int main() {
#ifdef DEBUG
  freopen("E.in", "r", stdin);
#endif
  for (int i = 1; i < MAXN; i++)
    f[i] = i;
  memset(g, -1, sizeof g);
  scanf("%d%d", &n, &h);
  for (int i = 1; i <= h; i++)
    p[i][0] = ++nump;
  for (int i = 1; i <= h; i++)
    p[i][1] = ++nump;
  for (int i = 1; i <= n; i++) {
    int t1, t2, t3, t4, u, v;
    scanf("%d%d%d%d", &t1, &t2, &t3, &t4);
    if (!t3)
      u = p[t1][1];
    else
      u = p[t3][0];
    if (!t4)
      v = p[t2][0];
    else
      v = p[t4][1];
    if (getf(u) ^ getf(v))
      f[f[u]] = f[v];
    d[u]--;
    d[v]++;
    tag[u] = tag[v] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (d[p[i][0]] < 0 || d[p[i][1]] > 0) {
      puts("NO");
      return 0;
    }
  for (int i = 1; i <= nump; i++) {
    size[getf(i)]++;
    if (d[i] < 0)
      sd[getf(i)][0] += -d[i];
    else
      sd[getf(i)][1] += d[i];
    if (tag[i])
      tag[getf(i)] = 1;
  }
  int numc = 0;
  int s0 = 0, s1 = 0;
  for (int i = 1; i <= nump; i++)
    if (getf(i) == i && tag[i]) {
      if (!sd[i][0] && !sd[i][1]) {
        puts("NO");
        return 0;
      }
      s0 += sd[i][0];
      s1 += sd[i][1];
      numc++;
    }
  if (s0 ^ s1) {
    puts("NO");
    return 0;
  }
  if (s0 < numc - 1) {
    puts("NO");
    return 0;
  }
  puts("YES");
  return 0;
}