Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define Relocate                                                               \
  freopen("./in.txt", "r", stdin);                                             \
  freopen("./out.txt", "w", stdout)
#define up(mi, a, b) for (mi = a; mi <= b; ++mi)
#define down(mi, b, a) for (mi = b; mi >= a; --mi)
#define mfill(a, v) memset(a, v, sizeof(a))
#define INF 0x3f3f3f3f
#define MAX 5005
#define v_n 5005
#define LL long long
#define e_n 5000005
#define MOD 1000000007
#define eps 0.000000001
#define Blen 32
using namespace std;
int N;
struct p {
  int par, sz;
};
p ufs[MAX];
vector<int> g[MAX];
int col[MAX];
void Init() {
  for (int i = 1; i <= N; i++) {
    ufs[i].par = i;
    ufs[i].sz = 1;
  }
}
int Find(int i) {
  if (ufs[i].par == i)
    return i;
  return ufs[i].par = Find(ufs[i].par);
}
bool Union(int i, int j) {
  int si = Find(i), sj = Find(j);
  if (si == sj) {
    return 0;
  } else if (ufs[si].sz < ufs[sj].sz) {
    ufs[si].par = sj;
    ufs[sj].sz += ufs[si].sz;
  } else {
    ufs[sj].par = si;
    ufs[si].sz += ufs[sj].sz;
  }
  return 1;
}
struct e {
  int s, t;
  LL w;
};
vector<e> hh;
void dfs(int r) {
  for (auto one : g[r]) {
    if (col[one] == 0) {
      col[one] = 3 - col[r];
      dfs(one);
    }
  }
}
int main(void) {
  int i, j, k;
  FAST_IO;
  // Relocate;
  LL w;
  cin >> N;
  Init();
  up(i, 2, N) {
    cin >> j >> k >> w;
    if (!(w & 1)) {
      Union(j, k);
    } else {
      hh.push_back(e{j, k, w});
    }
  }
  if (hh.empty()) {
    up(i, 1, N) { cout << 0 << endl; }
    return 0;
  }
  int r;
  for (auto one : hh) {
    int rs = Find(one.s);
    int rt = Find(one.t);
    r = rs;
    g[rs].push_back(rt);
    g[rt].push_back(rs);
  }
  col[r] = 1;
  dfs(r);
  up(i, 1, N) { cout << col[Find(i)] - 1 << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define Relocate                                                               \
  freopen("./in.txt", "r", stdin);                                             \
  freopen("./out.txt", "w", stdout)
#define up(mi, a, b) for (mi = a; mi <= b; ++mi)
#define down(mi, b, a) for (mi = b; mi >= a; --mi)
#define mfill(a, v) memset(a, v, sizeof(a))
#define INF 0x3f3f3f3f
#define MAX 5000005
#define v_n 5005
#define LL long long
#define e_n 5000005
#define MOD 1000000007
#define eps 0.000000001
#define Blen 32
using namespace std;
int N;
struct p {
  int par, sz;
};
p ufs[MAX];
vector<int> g[MAX];
int col[MAX];
void Init() {
  for (int i = 1; i <= N; i++) {
    ufs[i].par = i;
    ufs[i].sz = 1;
  }
}
int Find(int i) {
  if (ufs[i].par == i)
    return i;
  return ufs[i].par = Find(ufs[i].par);
}
bool Union(int i, int j) {
  int si = Find(i), sj = Find(j);
  if (si == sj) {
    return 0;
  } else if (ufs[si].sz < ufs[sj].sz) {
    ufs[si].par = sj;
    ufs[sj].sz += ufs[si].sz;
  } else {
    ufs[sj].par = si;
    ufs[si].sz += ufs[sj].sz;
  }
  return 1;
}
struct e {
  int s, t;
  LL w;
};
vector<e> hh;
void dfs(int r) {
  for (auto one : g[r]) {
    if (col[one] == 0) {
      col[one] = 3 - col[r];
      dfs(one);
    }
  }
}
int main(void) {
  int i, j, k;
  FAST_IO;
  // Relocate;
  LL w;
  cin >> N;
  Init();
  up(i, 2, N) {
    cin >> j >> k >> w;
    if (!(w & 1)) {
      Union(j, k);
    } else {
      hh.push_back(e{j, k, w});
    }
  }
  if (hh.empty()) {
    up(i, 1, N) { cout << 0 << endl; }
    return 0;
  }
  int r;
  for (auto one : hh) {
    int rs = Find(one.s);
    int rt = Find(one.t);
    r = rs;
    g[rs].push_back(rt);
    g[rt].push_back(rs);
  }
  col[r] = 1;
  dfs(r);
  up(i, 1, N) { cout << col[Find(i)] - 1 << endl; }
  return 0;
}