Problem p03044 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void a() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  if (S[K - 1] == 'A')
    S[K - 1] = 'a';
  if (S[K - 1] == 'B')
    S[K - 1] = 'b';
  if (S[K - 1] == 'C')
    S[K - 1] = 'c';

  cout << S;
}

void b() {
  string S;
  cin >> S;
  int fast = stoi(S.substr(0, 2));
  int last = stoi(S.substr(2, 2));

  bool may_yymm = (0 <= fast && fast <= 99) && (1 <= last && last <= 12);
  bool may_mmyy = (0 <= last && last <= 99) && (1 <= fast && fast <= 12);

  if (may_yymm && !may_mmyy)
    cout << "YYMM";
  else if (!may_yymm && may_mmyy)
    cout << "MMYY";
  else if (may_mmyy && may_yymm)
    cout << "AMBIGUOUS";
  else if (!may_mmyy && !may_yymm)
    cout << "NA";
}

int pow(int x) {
  if (x <= 0)
    return 1;
  return 2 * (pow(x - 1));
}
void c() {

  int N, K;
  cin >> N >> K;
  vector<int> vec;
  vec.push_back((N >= K) ? (N - K + 1) : 0);
  int k = K;
  while (k > 1) {
    vec.push_back(max(0, min(k, N + 1) - ((k + 1) / 2)));
    k = (k + 1) / 2;
  }

  long long upper = 0;
  for (int i = 0; i < vec.size(); i++) {
    upper += vec[i] * pow(vec.size() - 1 - i);
  }
  double p = static_cast<double>(upper) / (N * pow(vec.size() - 1));

  cout << fixed;
  cout << setprecision(12) << p;
}

char flip_color(char color) {
  if (color == '0')
    return '1';
  else
    return '0';
}
void dfs_color(int node, int parent, char required_color,
               const vector<vector<pair<int, int>>> &adj,
               vector<char> &color_map) {

  color_map[node] = required_color;
  for (auto child_weight : adj[node]) {
    int child = child_weight.first;
    int weigh = child_weight.second;
    if (child != parent) {
      if (weigh % 2 == 0) {
        dfs_color(child, node, required_color, adj, color_map);
      } else {
        dfs_color(child, node, flip_color(required_color), adj, color_map);
      }
    }
  }
}
void d() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> adj(N + 1, vector<pair<int, int>>());
  for (int i = 1; i <= N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  vector<char> color(N, '0');
  dfs_color(1, -1, '0', adj, color);

  for (int i = 1; i <= N; i++) {
    cout << color[i] << endl;
  }
}
int main() {
  d();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void a() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  if (S[K - 1] == 'A')
    S[K - 1] = 'a';
  if (S[K - 1] == 'B')
    S[K - 1] = 'b';
  if (S[K - 1] == 'C')
    S[K - 1] = 'c';

  cout << S;
}

void b() {
  string S;
  cin >> S;
  int fast = stoi(S.substr(0, 2));
  int last = stoi(S.substr(2, 2));

  bool may_yymm = (0 <= fast && fast <= 99) && (1 <= last && last <= 12);
  bool may_mmyy = (0 <= last && last <= 99) && (1 <= fast && fast <= 12);

  if (may_yymm && !may_mmyy)
    cout << "YYMM";
  else if (!may_yymm && may_mmyy)
    cout << "MMYY";
  else if (may_mmyy && may_yymm)
    cout << "AMBIGUOUS";
  else if (!may_mmyy && !may_yymm)
    cout << "NA";
}

int pow(int x) {
  if (x <= 0)
    return 1;
  return 2 * (pow(x - 1));
}
void c() {

  int N, K;
  cin >> N >> K;
  vector<int> vec;
  vec.push_back((N >= K) ? (N - K + 1) : 0);
  int k = K;
  while (k > 1) {
    vec.push_back(max(0, min(k, N + 1) - ((k + 1) / 2)));
    k = (k + 1) / 2;
  }

  long long upper = 0;
  for (int i = 0; i < vec.size(); i++) {
    upper += vec[i] * pow(vec.size() - 1 - i);
  }
  double p = static_cast<double>(upper) / (N * pow(vec.size() - 1));

  cout << fixed;
  cout << setprecision(12) << p;
}

char flip_color(char color) {
  if (color == '0')
    return '1';
  else
    return '0';
}
void dfs_color(int node, int parent, char required_color,
               const vector<vector<pair<int, int>>> &adj,
               vector<char> &color_map) {

  color_map[node] = required_color;
  for (auto child_weight : adj[node]) {
    int child = child_weight.first;
    int weigh = child_weight.second;
    if (child != parent) {
      if (weigh % 2 == 0) {
        dfs_color(child, node, required_color, adj, color_map);
      } else {
        dfs_color(child, node, flip_color(required_color), adj, color_map);
      }
    }
  }
}
void d() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> adj(N + 1, vector<pair<int, int>>());
  for (int i = 1; i <= N - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  vector<char> color(N + 1, '0');
  dfs_color(1, -1, '0', adj, color);

  for (int i = 1; i <= N; i++) {
    cout << color[i] << endl;
  }
}
int main() {
  d();
  return 0;
}