Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (long long i = o; i < n; i++)
#define FORR(x, arr) for (auto &x : arr)
#define oneforall                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define all(v) (v).begin(), (v).end()
#define ini(...)                                                               \
  int __VA_ARGS__;                                                             \
  in(__VA_ARGS__)
#define inl(...)                                                               \
  long long __VA_ARGS__;                                                       \
  in(__VA_ARGS__)
#define ins(...)                                                               \
  string __VA_ARGS__;                                                          \
  in(__VA_ARGS__)
const long long INF = 1e18;
void in() {}
template <typename T, class... U> void in(T &t, U &...u) {
  cin >> t;
  in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U> void out(const T &t, const U &...u) {
  cout << t;
  if (sizeof...(u))
    cout << " ";
  out(u...);
}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long>> vpll;
typedef vector<pair<int, int>> vpii;

vi adj[320000];
vi mod[3];
vi x[2];
void dfs(int x_, int visit, int x1) {
  x[x1].push_back(x_);
  FORR(tmp, adj[x_]) {
    if (tmp != visit)
      dfs(tmp, x_, x1 ^ 1);
  }
}
int main() {
  oneforall oneforall oneforall oneforall oneforall oneforall oneforall
      oneforall oneforall oneforall oneforall oneforall oneforall oneforall ini(
          n);
  int tmp1[32000];
  FOR(i, 1, n + 1) { mod[i % 3].push_back(i); }
  FOR(i, 0, n - 1) {
    ini(x4);
    ini(x4_);
    x4--;
    x4_--;
    adj[x4].push_back(x4_);
    adj[x4_].push_back(x4);
  }
  dfs(0, 0, 0);
  if (x[0].size() > x[1].size())
    swap(x[0], x[1]);
  if (x[0].size() <= mod[0].size()) {
    while (x[0].size() and mod[0].size()) {
      int pos = x[0].back();
      x[0].pop_back();
      int pos1 = mod[0].back();
      mod[0].pop_back();
      tmp1[pos] = pos1;
    }
    FORR(tmp, x[1]) {
      FOR(i, 0, 3) {
        if (mod[i].size()) {
          int pos = mod[i].back();
          mod[i].pop_back();
          tmp1[tmp] = pos;
          break;
        }
      }
    }
  } else {
    while (x[0].size() and mod[2].size()) {
      int pos = x[0].back();
      x[0].pop_back();
      int pos1 = mod[2].back();
      mod[2].pop_back();
      tmp1[pos] = pos1;
    }
    while (x[1].size() and mod[1].size()) {
      int pos = x[1].back();
      x[1].pop_back();
      int pos1 = mod[1].back();
      mod[1].pop_back();
      tmp1[pos] = pos1;
    }
    FORR(tmp14, x[0]) {
      FOR(i, 0, 3) {
        if (mod[i].size()) {
          int pos = mod[i].back();
          mod[i].pop_back();
          tmp1[tmp14] = pos;
          break;
        }
      }
    }
    FORR(tmp, x[1]) {
      FOR(i, 0, 3) {
        if (mod[i].size()) {
          int pos = mod[i].back();
          mod[i].pop_back();
          tmp1[tmp] = pos;
          break;
        }
      }
    }
  }
  FOR(i, 0, n) { cout << tmp1[i] << " "; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (long long i = o; i < n; i++)
#define FORR(x, arr) for (auto &x : arr)
#define oneforall                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define all(v) (v).begin(), (v).end()
#define ini(...)                                                               \
  int __VA_ARGS__;                                                             \
  in(__VA_ARGS__)
#define inl(...)                                                               \
  long long __VA_ARGS__;                                                       \
  in(__VA_ARGS__)
#define ins(...)                                                               \
  string __VA_ARGS__;                                                          \
  in(__VA_ARGS__)
const long long INF = 1e18;
void in() {}
template <typename T, class... U> void in(T &t, U &...u) {
  cin >> t;
  in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U> void out(const T &t, const U &...u) {
  cout << t;
  if (sizeof...(u))
    cout << " ";
  out(u...);
}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long>> vpll;
typedef vector<pair<int, int>> vpii;

vi adj[320000];
vi mod[3];
vi x[2];
void dfs(int x_, int visit, int x1) {
  x[x1].push_back(x_);
  FORR(tmp, adj[x_]) {
    if (tmp != visit)
      dfs(tmp, x_, x1 ^ 1);
  }
}
int main() {
  oneforall oneforall oneforall oneforall oneforall oneforall oneforall
      oneforall oneforall oneforall oneforall oneforall oneforall oneforall ini(
          n);
  int tmp1[320000];
  FOR(i, 1, n + 1) { mod[i % 3].push_back(i); }
  FOR(i, 0, n - 1) {
    ini(x4);
    ini(x4_);
    x4--;
    x4_--;
    adj[x4].push_back(x4_);
    adj[x4_].push_back(x4);
  }
  dfs(0, 0, 0);
  if (x[0].size() > x[1].size())
    swap(x[0], x[1]);
  if (x[0].size() <= mod[0].size()) {
    while (x[0].size() and mod[0].size()) {
      int pos = x[0].back();
      x[0].pop_back();
      int pos1 = mod[0].back();
      mod[0].pop_back();
      tmp1[pos] = pos1;
    }
    FORR(tmp, x[1]) {
      FOR(i, 0, 3) {
        if (mod[i].size()) {
          int pos = mod[i].back();
          mod[i].pop_back();
          tmp1[tmp] = pos;
          break;
        }
      }
    }
  } else {
    while (x[0].size() and mod[2].size()) {
      int pos = x[0].back();
      x[0].pop_back();
      int pos1 = mod[2].back();
      mod[2].pop_back();
      tmp1[pos] = pos1;
    }
    while (x[1].size() and mod[1].size()) {
      int pos = x[1].back();
      x[1].pop_back();
      int pos1 = mod[1].back();
      mod[1].pop_back();
      tmp1[pos] = pos1;
    }
    FORR(tmp14, x[0]) {
      FOR(i, 0, 3) {
        if (mod[i].size()) {
          int pos = mod[i].back();
          mod[i].pop_back();
          tmp1[tmp14] = pos;
          break;
        }
      }
    }
    FORR(tmp, x[1]) {
      FOR(i, 0, 3) {
        if (mod[i].size()) {
          int pos = mod[i].back();
          mod[i].pop_back();
          tmp1[tmp] = pos;
          break;
        }
      }
    }
  }
  FOR(i, 0, n) { cout << tmp1[i] << " "; }
  return 0;
}