Problem p03044 (C++)

Buggy submission (status: Runtime Error):
// common include
#include <bits/stdc++.h>
using namespace std;

// global variables
#define debug(x) cout << #x << ": " << x << endl
typedef long long ll;
const int M = 1e9 + 7;
const int INF = 1e9;

// typedef
typedef pair<int, int> P;
// sort by P.first (asd)
bool comPair(const P &firstElof, const P &secondElof) {
  return firstElof.first < secondElof.first;
}

// moving 4-direction
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

#define MAX_N 100000 // 10^5

template <class Abel> struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

struct Edge {
  int to;
  int weight;
};
vector<Edge> Graph[100000];
bool checked[100000];
int color[100000];

void dfs(int start, int c) {
  checked[start] = true;
  color[start] = c;
  for (auto gra : Graph[start]) {
    if (!checked[gra.to]) {
      if (gra.weight % 2 == 0)
        dfs(gra.to, c);
      else
        (c == 0) ? dfs(gra.to, 1) : dfs(gra.to, 0);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int f, t, w;
    cin >> f >> t >> w;
    Graph[f].emplace_back(Edge{t, w});
    Graph[t].emplace_back(Edge{f, w});
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    cout << color[i] << endl;
  }
}


Fixed submission:
// common include
#include <bits/stdc++.h>
using namespace std;

// global variables
#define debug(x) cout << #x << ": " << x << endl
typedef long long ll;
const int M = 1e9 + 7;
const int INF = 1e9;

// typedef
typedef pair<int, int> P;
// sort by P.first (asd)
bool comPair(const P &firstElof, const P &secondElof) {
  return firstElof.first < secondElof.first;
}

// moving 4-direction
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

#define MAX_N 100000 // 10^5

template <class Abel> struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

struct Edge {
  int to;
  int weight;
};
const int MAX = 1e5 + 5;
vector<Edge> Graph[MAX];
bool checked[MAX];
int color[MAX];

void dfs(int start, int c) {
  checked[start] = true;
  color[start] = c;
  for (auto gra : Graph[start]) {
    if (!checked[gra.to]) {
      if (gra.weight % 2 == 0)
        dfs(gra.to, c);
      else
        (c == 0) ? dfs(gra.to, 1) : dfs(gra.to, 0);
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int f, t, w;
    cin >> f >> t >> w;
    Graph[f].emplace_back(Edge{t, w});
    Graph[t].emplace_back(Edge{f, w});
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    cout << color[i] << endl;
  }
}