Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using P = pair<long long, long long>;
#define rep(i, n) for (long long i = 0; i < (long long)n; i++)
#define FOR(i, a, b) for (long long i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((long long)(x).size())
#define COUT(x) cout << x << endl
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define onBoard(y, x) (y >= 0 && y < h && x >= 0 && x < w)
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstr vector<string>
#define vp vector<pair<int, int>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define SUM(x) accumulate(x.begin(), x.end(), 0)
#define MAX(x) *max_element(x.begin(), x.end())
#define MIN(x) *min_element(x.begin(), x.end())
#define couty cout << "Yes" << endl
#define coutn cout << "No" << endl
#define coutY cout << "YES" << endl
#define coutN cout << "NO" << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
const long long dx[4] = {1, 0, -1, 0};
const long long dy[4] = {0, 1, 0, -1};
const long long INF = 1e12;
const long long MOD = 1e9 + 7;

struct edge {
  int to, cost;
};

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  int n;
  cin >> n;
  vector<vector<edge>> g(n);
  vector<int> color(n, -1);
  rep(i, n - 1) {
    int u, v, w;
    // cin >> u >> v >> w;
    scanf("%lld%lld%lld", &u, &v, &w);
    u--;
    v--;
    g[u].PB(edge{v, w});
    g[v].PB(edge{u, w});
  }

  color[0] = 0;
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : g[v]) {
      if (color[nv.to] != -1)
        continue;
      if (nv.cost % 2 == 0) {
        color[nv.to] = color[v];
        que.push(nv.to);
      } else {
        color[nv.to] = color[v] ^ 1;
        que.push(nv.to);
      }
    }
  }
  rep(i, n) cout << color[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using P = pair<long long, long long>;
#define rep(i, n) for (long long i = 0; i < (long long)n; i++)
#define FOR(i, a, b) for (long long i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((long long)(x).size())
#define COUT(x) cout << x << endl
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define onBoard(y, x) (y >= 0 && y < h && x >= 0 && x < w)
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstr vector<string>
#define vp vector<pair<int, int>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define SUM(x) accumulate(x.begin(), x.end(), 0)
#define MAX(x) *max_element(x.begin(), x.end())
#define MIN(x) *min_element(x.begin(), x.end())
#define couty cout << "Yes" << endl
#define coutn cout << "No" << endl
#define coutY cout << "YES" << endl
#define coutN cout << "NO" << endl
#define yn(x) cout << (x ? "Yes" : "No") << endl
#define YN(x) cout << (x ? "YES" : "NO") << endl
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
const long long dx[4] = {1, 0, -1, 0};
const long long dy[4] = {0, 1, 0, -1};
const long long INF = 1e12;
const long long MOD = 1e9 + 7;

struct edge {
  int to, cost;
};

signed main() {
  // cin.tie(nullptr);
  // ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  int n;
  cin >> n;
  vector<vector<edge>> g(n);
  vector<int> color(n, -1);
  rep(i, n - 1) {
    int u, v, w;
    // cin >> u >> v >> w;
    scanf("%lld%lld%lld", &u, &v, &w);
    u--;
    v--;
    g[u].PB(edge{v, w});
    g[v].PB(edge{u, w});
  }

  color[0] = 0;
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto nv : g[v]) {
      if (color[nv.to] != -1)
        continue;
      if (nv.cost % 2 == 0) {
        color[nv.to] = color[v];
        que.push(nv.to);
      } else {
        color[nv.to] = color[v] ^ 1;
        que.push(nv.to);
      }
    }
  }
  rep(i, n) cout << color[i] << endl;
}