Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

const int N = 100010;

int n;
bool vis[N], par[N];
vector<int> ver[2];
int cnt[3], pc[2], mat[3];
int ans[N];

vector<int> g[N];

void dfs(int u) {
  ++pc[par[u]];
  ver[par[u]].push_back(u);
  vis[u] = true;
  for (int v : g[u])
    if (!vis[v]) {
      par[v] = !par[u];
      dfs(v);
    }
}

int main() {
#ifdef LBT
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  for (int rep = 1; rep < n; ++rep) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);
  for (int i = 1; i <= n; ++i)
    ++cnt[i % 3];
  int mi = pc[0] < pc[1] ? 0 : 1, mj = cnt[1] < cnt[2] ? 1 : 2;
  if (pc[mi] < cnt[mj]) {
    mat[1] = mat[2] = !mi;
  } else {
    mat[mj] = mi;
    mat[3 - mj] = !mi;
  }
  for (int i = 1; i <= n; ++i)
    if (i % 3) {
      ans[ver[mat[i % 3]].back()] = i;
      ver[mat[i % 3]].pop_back();
    }
  for (int i = 1; i <= n; ++i)
    if (i % 3 == 0) {
      if (ver[0].empty())
        swap(ver[0], ver[1]);
      ans[ver[0].back()] = i;
      ver[0].pop_back();
    }
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << ' ';

#ifdef LBT
  LOG("Time: %dms\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}


Fixed submission:
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

const int N = 200010;

int n;
bool vis[N], par[N];
vector<int> ver[2];
int cnt[3], pc[2], mat[3];
int ans[N];

vector<int> g[N];

void dfs(int u) {
  ++pc[par[u]];
  ver[par[u]].push_back(u);
  vis[u] = true;
  for (int v : g[u])
    if (!vis[v]) {
      par[v] = !par[u];
      dfs(v);
    }
}

int main() {
#ifdef LBT
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  for (int rep = 1; rep < n; ++rep) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);
  for (int i = 1; i <= n; ++i)
    ++cnt[i % 3];
  int mi = pc[0] < pc[1] ? 0 : 1, mj = cnt[1] < cnt[2] ? 1 : 2;
  if (pc[mi] < cnt[mj]) {
    mat[1] = mat[2] = !mi;
  } else {
    mat[mj] = mi;
    mat[3 - mj] = !mi;
  }
  for (int i = 1; i <= n; ++i)
    if (i % 3) {
      ans[ver[mat[i % 3]].back()] = i;
      ver[mat[i % 3]].pop_back();
    }
  for (int i = 1; i <= n; ++i)
    if (i % 3 == 0) {
      if (ver[0].empty())
        swap(ver[0], ver[1]);
      ans[ver[0].back()] = i;
      ver[0].pop_back();
    }
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << ' ';

#ifdef LBT
  LOG("Time: %dms\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}