Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

int main() {
  ll N;

  cin >> N;

  int ans[N];
  vector<P> aLst[N];

  queue<ll> que;
  for (int i = 0; i < N; ++i) {
    ll u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    P tmp0(v, w);
    aLst[u].emplace_back(tmp0);
    P tmp1(u, w);
    aLst[v].emplace_back(tmp1);
  }

  que.push(0);
  ans[0] = 0;

  unordered_map<ll, bool> visited;

  while (!que.empty()) {
    ll cur = que.front();
    que.pop();
    // cout << "cur=" << cur << endl;
    for (size_t i = 0; i < aLst[cur].size(); ++i) {
      ll cur_child = aLst[cur][i].first;
      if (visited.count(cur_child) == 0) {
        que.push(cur_child);
        visited[cur_child] = true;
        if (aLst[cur][i].second % 2 == 0) {
          ans[cur_child] = ans[cur];
        } else {
          ans[cur_child] = 1 - ans[cur];
        }
      }
    }
  }

  for (int i = 0; i < N; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x)                                                      \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

int main() {
  ll N;

  cin >> N;

  int ans[N];
  vector<P> aLst[N];

  queue<ll> que;
  for (int i = 0; i < N; ++i) {
    ll u, v, w;
    cin >> u >> v >> w;
    P tmp0(v - 1, w);
    aLst[u - 1].emplace_back(tmp0);
    P tmp1(u - 1, w);
    aLst[v - 1].emplace_back(tmp1);
  }

  que.push(0);
  ans[0] = 0;

  unordered_map<ll, bool> visited;

  while (!que.empty()) {
    ll cur = que.front();
    que.pop();
    // cout << "cur=" << cur << endl;
    for (size_t i = 0; i < aLst[cur].size(); ++i) {
      ll cur_child = aLst[cur][i].first;
      if (visited.count(cur_child) == 0) {
        que.push(cur_child);
        visited[cur_child] = true;
        if (aLst[cur][i].second % 2 == 0) {
          ans[cur_child] = ans[cur];
        } else {
          ans[cur_child] = 1 - ans[cur];
        }
      }
    }
  }

  for (int i = 0; i < N; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}