Problem p03669 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ph push
#define ptc putchar
#define enter putchar('\n')
using namespace std;
typedef pair<int, int> pii;
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef long long lnt;
inline int read() {
  int x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x;
}
inline void write(int x) {
  if (!x) {
    ptc('0');
    return;
  }
  int dg[20], len = 0;
  while (x)
    dg[len++] = x % 10, x /= 10;
  while (len--)
    ptc(dg[len] + '0');
}
inline void writeln(int x) {
  write(x);
  ptc('\n');
}
const int N = 1e5 + 10;
const int M = 2e2 + 10;

int par[M << 1], sz[M << 1];
int find(int x) {
  if (par[x] != x) {
    return par[x] = find(par[x]);
  }
  return par[x];
}
void merge(int x, int y) {
  //	printf("[] %d %d %d %d\n",x,y,find(x),find(y));
  x = find(x), y = find(y);
  if (x != y) {
    par[x] = y;
  }
  sz[y]++;
}
int ind[M << 1], outd[M << 1], okay[M << 1];
int n, h;

int main() {
  scanf("%d%d", &n, &h);
  for (int i = 1; i <= h << 1; i++)
    par[i] = i;
  for (int i = 1; i <= n; i++) {
    int A, B, C, D;
    scanf("%d%d%d%d", &A, &B, &C, &D);
    int l = C ? C : A + h, r = D ? D + h : B;
    merge(l, r);

    //		printf("%d -> %d\n",l,r);
    outd[l]++;
    ind[r]++;
  }

  for (int i = 1; i <= h; i++) {
    if (ind[i] < outd[i])
      return puts("NO"), 0;
  }

  for (int i = 1 + h; i <= h << 1; i++) {
    if (ind[i] > outd[i])
      return puts("NO"), 0;
  }
  //	printf("%d %d\n",ind[2],outd[2]);

  for (int i = 1; i <= h << 1; i++)
    if (ind[i] != outd[i] || !sz[find(i)]) {
      //		printf("__ %d %d\n",i,find(i));
      okay[find(i)] = 1;
    }

  for (int i = 1; i <= h << 1; i++)
    if (!okay[find(i)]) {
      //		printf("[%d]\n",i);
      return puts("NO");
    }
  puts("YES");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ph push
#define ptc putchar
#define enter putchar('\n')
using namespace std;
typedef pair<int, int> pii;
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef long long lnt;
inline int read() {
  int x = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + c - '0', c = getchar();
  return x;
}
inline void write(int x) {
  if (!x) {
    ptc('0');
    return;
  }
  int dg[20], len = 0;
  while (x)
    dg[len++] = x % 10, x /= 10;
  while (len--)
    ptc(dg[len] + '0');
}
inline void writeln(int x) {
  write(x);
  ptc('\n');
}
const int N = 1e5 + 10;
const int M = 2e2 + 10;

int par[M << 1], sz[M << 1];
int find(int x) {
  if (par[x] != x) {
    return par[x] = find(par[x]);
  }
  return par[x];
}
void merge(int x, int y) {
  //	printf("[] %d %d %d %d\n",x,y,find(x),find(y));
  x = find(x), y = find(y);
  if (x != y) {
    par[x] = y;
  }
  sz[y]++;
}
int ind[M << 1], outd[M << 1], okay[M << 1];
int n, h;

int main() {
  scanf("%d%d", &n, &h);
  for (int i = 1; i <= h << 1; i++)
    par[i] = i;
  for (int i = 1; i <= n; i++) {
    int A, B, C, D;
    scanf("%d%d%d%d", &A, &B, &C, &D);
    int l = C ? C : A + h, r = D ? D + h : B;
    merge(l, r);

    //		printf("%d -> %d\n",l,r);
    outd[l]++;
    ind[r]++;
  }

  for (int i = 1; i <= h; i++) {
    if (ind[i] < outd[i])
      return puts("NO"), 0;
  }

  for (int i = 1 + h; i <= h << 1; i++) {
    if (ind[i] > outd[i])
      return puts("NO"), 0;
  }
  //	printf("%d %d\n",ind[2],outd[2]);

  for (int i = 1; i <= h << 1; i++)
    if (ind[i] != outd[i] || !sz[find(i)]) {
      //		printf("__ %d %d\n",i,find(i));
      okay[find(i)] = 1;
    }

  for (int i = 1; i <= h << 1; i++)
    if (!okay[find(i)]) {
      //		printf("[%d]\n",i);
      return puts("NO"), 0;
    }
  puts("YES");
  return 0;
}