Problem p03669 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 998244353
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int n, m;
vector<P> vec;
int sum[405];
int par[405], ran[405];
void init() {
  for (int i = 0; i < 405; i++)
    par[i] = i;
}
int find(int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (ran[x] < ran[y])
    par[x] = y;
  else {
    par[y] = x;
    if (ran[x] == ran[y])
      ran[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int cnt[405], X;
set<int> S;
int main() {
  cin >> n >> m;
  init();
  rep(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c == 0)
      a += 200;
    else
      a = c;
    if (d == 0)
      ;
    else
      b = 200 + d;
    vec.pb(mp(a, b)); // cout<<a<<" "<<b<<endl;
    sum[a]++;
    sum[b]--;
    unite(a, b);
    S.insert(a);
    S.insert(b);
    X = a;
  }
  int val = 0, val2 = 0;
  for (int i = 1; i <= m; i++) {
    if (sum[i] > 0) {
      puts("NO");
      return 0;
    } else
      cnt[i] = -1 * sum[i];
    val += cnt[i];
    if (sum[200 + i] < 0) {
      puts("NO");
      return 0;
    } else
      cnt[200 + i] = sum[i];
    val2 += cnt[200 + i];
  }
  assert(val == val2);
  if (!val) {
    puts("NO");
    return 0;
  }
  while (1) {
    bool upd = 0;
    {
      repn(i, m) repn(j, m) {
        if (cnt[i] >= 1 && cnt[j + 200] >= 1 && !same(i, j + 200)) {
          unite(i, j + 200);
          cnt[i]--;
          cnt[j + 200]--;
          upd = 1;
        }
      }
    }
    if (!upd)
      break;
  }
  for (set<int>::iterator it = S.begin(); it != S.end(); ++it) {
    if (!same(*it, X)) {
      puts("NO");
      return 0;
    }
  }
  puts("YES");
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 998244353
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int n, m;
vector<P> vec;
int sum[405];
int par[405], ran[405];
void init() {
  for (int i = 0; i < 405; i++)
    par[i] = i;
}
int find(int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (ran[x] < ran[y])
    par[x] = y;
  else {
    par[y] = x;
    if (ran[x] == ran[y])
      ran[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int cnt[405], X;
set<int> S;
int main() {
  cin >> n >> m;
  init();
  rep(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c == 0)
      a += 200;
    else
      a = c;
    if (d == 0)
      ;
    else
      b = 200 + d;
    vec.pb(mp(a, b)); // cout<<a<<" "<<b<<endl;
    sum[a]++;
    sum[b]--;
    unite(a, b);
    S.insert(a);
    S.insert(b);
    X = a;
  }
  int val = 0, val2 = 0;
  for (int i = 1; i <= m; i++) {
    if (sum[i] > 0) {
      puts("NO");
      return 0;
    } else
      cnt[i] = -1 * sum[i];
    val += cnt[i];
    if (sum[200 + i] < 0) {
      puts("NO");
      return 0;
    } else
      cnt[200 + i] = sum[200 + i];
    val2 += cnt[200 + i];
  }
  assert(val == val2);
  if (!val) {
    puts("NO");
    return 0;
  }
  while (1) {
    bool upd = 0;
    {
      repn(i, m) repn(j, m) {
        if (cnt[i] >= 1 && cnt[j + 200] >= 1 && !same(i, j + 200)) {
          unite(i, j + 200);
          cnt[i]--;
          cnt[j + 200]--;
          upd = 1;
        }
      }
    }
    if (!upd)
      break;
  }
  for (set<int>::iterator it = S.begin(); it != S.end(); ++it) {
    if (!same(*it, X)) {
      puts("NO");
      return 0;
    }
  }
  puts("YES");
}