Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h> // clang-format off
using namespace std;
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (int i = (a), lim_i = (b); i < lim_i; i++)
#define REP(i, ...) REP_((i), __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Mar  8 21:00:01 JST 2020
 **/

signed main() {
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int one = n / 3 + (n % 3 >= 1);
  int two = n / 3 + (n % 3 >= 2);
  int zer = n / 3;
  assert(one + two + zer == n);
  int cur_one = 1;
  vector<int> color(n, -1);
  auto dfs = [&](auto f, int v, int prv) -> void {
    for (int s : g[v]) {
      if (s == prv)
        continue;
      color[s] = 3 - color[v];
      cur_one += color[s] == 1;
      f(f, s, v);
    }
  };
  color[0] = 1;
  dfs(dfs, 0, -1);
  int cur_two = n - cur_one;
  if (cur_one <= zer) {
    int rem_zer = zer - cur_one;
    int rem_one = one;
    REP(i, n) {
      if (color[i] == 1) {
        color[i] = 0;
      } else {
        if (rem_zer-- > 0) {
          color[i] = 0;
        }
        if (rem_one-- > 0) {
          dump(rem_one);
          color[i] = 1;
        }
      }
    }
  } else if (cur_two <= zer) {
    int rem_zer = zer - cur_two;
    int rem_two = two;
    REP(i, n) {
      if (color[i] == 2) {
        color[i] = 0;
      } else {
        if (rem_zer-- > 0) {
          color[i] = 0;
        } else if (rem_two-- > 0) {
          color[i] = 2;
        }
      }
    }
  } else {
    int x = 0, y = 0;
    REP(i, n) {
      if (color[i] == 1) {
        if (x++ >= one) {
          color[i] = 0;
        }
      } else {
        if (y++ >= two) {
          color[i] = 0;
        }
      }
    }
  }
  //
  dump(color);
  REP(k, 3) {
    int acc = k == 0 ? 3 : 0;
    REP(i, n) {
      if (color[i] == k) {
        color[i] += acc;
        acc += 3;
      }
    }
  }
  vector<int> tmp = color;
  sort(tmp.begin(), tmp.end());
  REP(i, n) { assert(tmp[i] == i + 1); }
  for (Int x : color)
    cout << x << " ";
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h> // clang-format off
using namespace std;
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (int i = (a), lim_i = (b); i < lim_i; i++)
#define REP(i, ...) REP_((i), __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Mar  8 21:00:01 JST 2020
 **/

signed main() {
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  int one = n / 3 + (n % 3 >= 1);
  int two = n / 3 + (n % 3 >= 2);
  int zer = n / 3;
  assert(one + two + zer == n);
  int cur_one = 1;
  vector<int> color(n, -1);
  auto dfs = [&](auto f, int v, int prv) -> void {
    for (int s : g[v]) {
      if (s == prv)
        continue;
      color[s] = 3 - color[v];
      cur_one += color[s] == 1;
      f(f, s, v);
    }
  };
  color[0] = 1;
  dfs(dfs, 0, -1);
  int cur_two = n - cur_one;
  if (cur_one <= zer) {
    int rem_zer = zer - cur_one;
    int rem_one = one;
    REP(i, n) {
      if (color[i] == 1) {
        color[i] = 0;
      } else {
        if (rem_zer-- > 0) {
          color[i] = 0;
        } else if (rem_one-- > 0) {
          dump(rem_one);
          color[i] = 1;
        }
      }
    }
  } else if (cur_two <= zer) {
    int rem_zer = zer - cur_two;
    int rem_two = two;
    REP(i, n) {
      if (color[i] == 2) {
        color[i] = 0;
      } else {
        if (rem_zer-- > 0) {
          color[i] = 0;
        } else if (rem_two-- > 0) {
          color[i] = 2;
        }
      }
    }
  } else {
    int x = 0, y = 0;
    REP(i, n) {
      if (color[i] == 1) {
        if (x++ >= one) {
          color[i] = 0;
        }
      } else {
        if (y++ >= two) {
          color[i] = 0;
        }
      }
    }
  }
  //
  dump(color);
  REP(k, 3) {
    int acc = k == 0 ? 3 : 0;
    REP(i, n) {
      if (color[i] == k) {
        color[i] += acc;
        acc += 3;
      }
    }
  }
  vector<int> tmp = color;
  sort(tmp.begin(), tmp.end());
  REP(i, n) { assert(tmp[i] == i + 1); }
  for (Int x : color)
    cout << x << " ";
  cout << endl;
}