Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define X first
#define Y second
#define pb push_back
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
#define repe(X, Y) for ((X) = 0; (X) < (Y); ++(X))
#define peat(X, Y) for (; (X) < (Y); ++(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define Endl endl
#define NL << "\n"

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
// #undef NUIP
#ifdef NUIP
#include "benri.h"
#else
#define out(args...)
#endif
#ifdef __cpp_init_captures
template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }
template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}
#endif
const ll MOD = 1e9 + 7; // 998244353

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(0);
  ll n;
  cin >> n;
  n;
  vv<int> g(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].pb(b);
    g[b].pb(a);
  }
  vector<int> d(n, MOD);
  {
    d[0] = 0;
    queue<int> que;
    que.emplace(0);
    while (que.size()) {
      int v = que.front();
      que.pop();
      for (int w : g[v])
        if (MN(d[w], d[v] + 1))
          que.emplace(w);
    }
  }
  out(d, 1);
  vector<int> os, es;
  rep(i, n)(d[i] % 2 ? es : os).pb(i);
  vector<int> re(n);
  reps(o, 1, 3) {
    if (os.size() > es.size())
      swap(os, es);
    for (int v = o; v <= n; v += 3) {
      re[es.back()] = v;
      es.pop_back();
    }
  }
  os.insert(os.end(), all(es));
  for (int v = 3; v <= n; v += 3) {
    re[os.back()] = v;
    os.pop_back();
  }
  assert(os.empty());
  assert(es.empty());
  rep(i, n) cout << re[i] << " \n"[i + 1 == n];
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define X first
#define Y second
#define pb push_back
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
#define repe(X, Y) for ((X) = 0; (X) < (Y); ++(X))
#define peat(X, Y) for (; (X) < (Y); ++(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define Endl endl
#define NL << "\n"

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
// #undef NUIP
#ifdef NUIP
#include "benri.h"
#else
#define out(args...)
#endif
#ifdef __cpp_init_captures
template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }
template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}
#endif
const ll MOD = 1e9 + 7; // 998244353

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(0);
  ll n;
  cin >> n;
  n;
  vv<int> g(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].pb(b);
    g[b].pb(a);
  }
  vector<int> d(n, MOD);
  {
    d[0] = 0;
    queue<int> que;
    que.emplace(0);
    while (que.size()) {
      int v = que.front();
      que.pop();
      for (int w : g[v])
        if (MN(d[w], d[v] + 1))
          que.emplace(w);
    }
  }
  out(d, 1);
  vector<int> os, es;
  rep(i, n)(d[i] % 2 ? es : os).pb(i);
  vector<int> re(n);
  reps(o, 1, 3) {
    if (os.size() > es.size())
      swap(os, es);
    for (int v = o; v <= n; v += 3) {
      re[es.back()] = v;
      es.pop_back();
    }
  }
  os.insert(os.end(), all(es));
  for (int v = 3; v <= n; v += 3) {
    re[os.back()] = v;
    os.pop_back();
  }
  rep(i, n) cout << re[i] << " \n"[i + 1 == n];
  return 0;
}