Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// cout <<  << endl;
typedef long long int ll;
typedef double ld;

vector<pair<ll, ll>> p;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define MAX_N 100010
#define MAX_H 310
#define MAX_W 310
struct edge {
  ll to, cost;
};
bool x[MAX_N];
ll num[MAX_N];

ll eratosthenes(ll n) {
  int p = 0;
  for (ll i = 0; i <= n; ++i)
    x[i] = true;
  x[0] = x[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (x[i]) {
      p++;
      for (int j = 2 * i; j <= n; j += i)
        x[j] = false;
    }

    num[i] = p;
  }

  return p;
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

vector<edge> G[MAX_N];
ll color[MAX_N];

bool dfs(ll v, ll p, ll c) {
  color[v] = c;
  for (auto e : G[v]) {
    if (e.to == p)
      continue;
    if (e.cost & 1)
      dfs(e.to, v, 1 - c);
    else
      dfs(e.to, v, c);
  }
  return true;
}

int main() {
  ll v;
  cin >> v;
  ll s, t, w;
  for (ll i = 0; i < v - 1; i++) {
    cin >> s >> t >> w;
    s--;
    t--;
    G[s - 1].push_back(edge{t, w});
    G[t - 1].push_back(edge{s, w});
  }
  dfs(0LL, -1, 1);
  for (ll i = 0; i < v; i++) {
    cout << color[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// cout <<  << endl;
typedef long long int ll;
typedef double ld;

vector<pair<ll, ll>> p;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define MAX_N 100010
#define MAX_H 310
#define MAX_W 310
struct edge {
  ll to, cost;
};
bool x[MAX_N];
ll num[MAX_N];

ll eratosthenes(ll n) {
  int p = 0;
  for (ll i = 0; i <= n; ++i)
    x[i] = true;
  x[0] = x[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (x[i]) {
      p++;
      for (int j = 2 * i; j <= n; j += i)
        x[j] = false;
    }

    num[i] = p;
  }

  return p;
}

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

vector<edge> G[MAX_N];
ll color[MAX_N];

bool dfs(ll v, ll p, ll c) {
  color[v] = c;
  for (auto e : G[v]) {
    if (e.to == p)
      continue;
    if (e.cost & 1)
      dfs(e.to, v, 1 - c);
    else
      dfs(e.to, v, c);
  }
  return true;
}

int main() {
  ll v;
  cin >> v;
  ll s, t, w;
  for (ll i = 0; i < v - 1; i++) {
    cin >> s >> t >> w;
    s--;
    t--;
    G[s].push_back(edge{t, w});
    G[t].push_back(edge{s, w});
  }
  dfs(0LL, -1, 1);
  for (ll i = 0; i < v; i++) {
    cout << color[i] << endl;
  }
  return 0;
}