Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
string s;
int k;
int INFi = 1000000000;
ll INFl = 1000000000000000;

struct UnionFind {
  vector<int> par;  // 初期化
  vector<int> rank; // 高さ
  UnionFind(int n = 0) : par(n), rank(n, 0) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]); // 経路圧縮
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (rank[rx] < rank[ry])
      rank[rx] = ry;
    else
      rank[ry] = rx;
    if (rank[rx] == rank[ry])
      rank[rx]++;
  }
  bool same(int x, int y) { return root(x) == root(y); }
};
vector<int> color(100000, -1);
vector<pi> G[100000];
void dfs(int s, int c) {
  color[s] = c;
  for (int i = 0; i < G[s].size(); i++) {
    if (color[G[s][i].first] == -1) {
      if (G[s][i].second % 2 == 0)
        dfs(G[s][i].first, (c == 1 ? 1 : 0));
      else
        dfs(G[s][i].first, (c == 1 ? 0 : 1));
    }
  }
}
int main() {
  int N;
  cin >> N;
  int u, v, w;
  for (int i = 0; i < N; i++) {
    cin >> u >> v >> w;
    u--;
    v--;
    G[u].push_back(pi(v, w));
    G[v].push_back(pi(u, w));
  }
  dfs(0, 1);
  for (int i = 0; i < N; i++)
    cout << color[i] << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
string s;
int k;
int INFi = 1000000000;
ll INFl = 1000000000000000;

struct UnionFind {
  vector<int> par;  // 初期化
  vector<int> rank; // 高さ
  UnionFind(int n = 0) : par(n), rank(n, 0) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]); // 経路圧縮
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    if (rank[rx] < rank[ry])
      rank[rx] = ry;
    else
      rank[ry] = rx;
    if (rank[rx] == rank[ry])
      rank[rx]++;
  }
  bool same(int x, int y) { return root(x) == root(y); }
};
vector<int> color(100000, -1);
vector<pi> G[100000];
void dfs(int s, int c) {
  color[s] = c;
  for (int i = 0; i < G[s].size(); i++) {
    if (color[G[s][i].first] == -1) {
      if (G[s][i].second % 2 == 0)
        dfs(G[s][i].first, (c == 1 ? 1 : 0));
      else
        dfs(G[s][i].first, (c == 1 ? 0 : 1));
    }
  }
}
int main() {
  int N;
  cin >> N;
  int u, v, w;
  for (int i = 0; i < N - 1; i++) {
    cin >> u >> v >> w;
    u--;
    v--;
    G[u].push_back(pi(v, w));
    G[v].push_back(pi(u, w));
  }
  dfs(0, 1);
  for (int i = 0; i < N; i++)
    cout << color[i] << endl;
}