Problem p03044 (C++)

Buggy submission (status: Runtime Error):
/*Author: Gautam*/

#include <bits/stdc++.h>
#define FastIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define MOD 1000000007
#define ull unsigned long long int
#define newline cout << endl

/* TYPE DEFINITIONS */
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

/* MATH */
bool isPrime(ull n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ull i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

ll pow(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}

//
/* MACROS */

#define all(A) (A).begin(), (A).end()
#define arrall(A, n) (A), ((A) + (n))
#define rall(A) (A).rbegin(), (A).rend()
#define sz(A) (int)(A).size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ln(X) (int)(X).length()
#define square(X) ((X) * (X))
#define cube(X) ((X) * (X) * (X))
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)
#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)
#define fore(it, a)                                                            \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
std::vector<pi> v[100005];
bool visited[10000];
bool color[10000];
void dfs(int s, int su) {
  if (visited[s])
    return;
  visited[s] = true;
  if (su & 1)
    color[s] = true;
  for (auto i : v[s]) {
    dfs(i.first, su + i.second);
  }
}
void solve() {
  int n;
  cin >> n;
  forn(i, n - 1) {
    int a, b, w;
    cin >> a >> b >> w;
    v[a].pb({b, w});
    v[b].pb({a, w});
  }
  fora(i, 1, n) {
    if (!visited[i]) {
      dfs(i, 0);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << color[i] << endl;
  }
}

int main() {

  FastIO;
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Fixed submission:
/*Author: Gautam*/

#include <bits/stdc++.h>
#define FastIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define MOD 1000000007
#define ull unsigned long long int
#define newline cout << endl

/* TYPE DEFINITIONS */
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

/* MATH */
bool isPrime(ull n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (ull i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

ll pow(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}

//
/* MACROS */

#define all(A) (A).begin(), (A).end()
#define arrall(A, n) (A), ((A) + (n))
#define rall(A) (A).rbegin(), (A).rend()
#define sz(A) (int)(A).size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ln(X) (int)(X).length()
#define square(X) ((X) * (X))
#define cube(X) ((X) * (X) * (X))
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forr(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fora(i, a, b) for (int i = int(a); i <= int(b); i++)
#define forb(i, a, b) for (int i = int(a); i >= int(b); i--)
#define fore(it, a)                                                            \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
std::vector<pi> v[100005];
bool visited[100005];
bool color[100005];
void dfs(int s, ll su) {
  if (visited[s])
    return;
  visited[s] = true;
  if (su & 1)
    color[s] = true;
  for (auto i : v[s]) {
    dfs(i.first, su + i.second);
  }
}
void solve() {
  int n;
  cin >> n;
  forn(i, n - 1) {
    int a, b, w;
    cin >> a >> b >> w;
    v[a].pb({b, w});
    v[b].pb({a, w});
  }
  fora(i, 1, n) {
    if (!visited[i]) {
      dfs(i, 0);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << color[i] << endl;
  }
}

int main() {

  FastIO;
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}