Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

vi color;
vector<vi> adj;

void dfs(int v, int c) {
  color[v] = c;
  for (auto u : adj[v]) {
    if (color[u] != -1)
      continue;
    dfs(u, 1 - c);
  }
}

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  color.resize(n, -1);
  adj.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(0, 0);
  vi blank[2];
  rep(i, n) blank[color[i]].pb(i);
  int x = blank[0].size();
  int y = blank[1].size();
  if (x > y) {
    swap(x, y);
    blank[0].swap(blank[1]);
  }
  vi ans(n);
  set<int> rnum, rspa;
  rep(i, n) rnum.ins(i + 1), rspa.ins(i);
  if (x <= 3 / n) {
    rep(i, x) {
      ans[blank[0][i]] = (i + 1) * 3;
      rspa.erase(blank[0][i]);
      rnum.erase((i + 1) * 3);
    }
  } else {
    rep(i, (n + 2) / 3) {
      ans[blank[0][i]] = i * 3 + 1;
      rspa.erase(blank[0][i]);
      rnum.erase(i * 3 + 1);
    }
    rep(i, (n + 1) / 3) {
      ans[blank[1][i]] = i * 3 + 2;
      rspa.erase(blank[1][i]);
      rnum.erase(i * 3 + 2);
    }
  }
  auto it = rnum.begin();
  for (auto s : rspa) {
    ans[s] = *it;
    it++;
  }
  rep(i, n) {
    if (i > 0)
      cout << " ";
    cout << ans[i];
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

vi color;
vector<vi> adj;

void dfs(int v, int c) {
  color[v] = c;
  for (auto u : adj[v]) {
    if (color[u] != -1)
      continue;
    dfs(u, 1 - c);
  }
}

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  color.resize(n, -1);
  adj.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(0, 0);
  vi blank[2];
  rep(i, n) blank[color[i]].pb(i);
  int x = blank[0].size();
  int y = blank[1].size();
  if (x > y) {
    swap(x, y);
    blank[0].swap(blank[1]);
  }
  vi ans(n);
  set<int> rnum, rspa;
  rep(i, n) rnum.ins(i + 1), rspa.ins(i);
  if (x <= n / 3) {
    rep(i, x) {
      ans[blank[0][i]] = (i + 1) * 3;
      rspa.erase(blank[0][i]);
      rnum.erase((i + 1) * 3);
    }
  } else {
    rep(i, (n + 2) / 3) {
      ans[blank[0][i]] = i * 3 + 1;
      rspa.erase(blank[0][i]);
      rnum.erase(i * 3 + 1);
    }
    rep(i, (n + 1) / 3) {
      ans[blank[1][i]] = i * 3 + 2;
      rspa.erase(blank[1][i]);
      rnum.erase(i * 3 + 2);
    }
  }
  auto it = rnum.begin();
  for (auto s : rspa) {
    ans[s] = *it;
    it++;
  }
  rep(i, n) {
    if (i > 0)
      cout << " ";
    cout << ans[i];
  }
  cout << endl;
  return 0;
}