Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

int n;
vvi g;
vi a, b;
vvi c;

void dfs(int v, int p, int t) {
  a[v] = t;
  for (auto u : g[v])
    if (u != p)
      dfs(u, v, 1 - t);
}

int main() {
  cin >> n;
  g = vvi(n);
  a = b = vi(n);
  c = vvi(3);
  for (int i = 1; i < n; i++)
    c[i % 3].push_back(i);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0, -1, 0);
  int t = 0;
  for (auto i : a)
    t += i;
  if (t <= n / 3 || n - t <= n / 3) {
    int I = 0;
    set<int> st;
    int tmp = (t <= n / 3 ? 1 : 0);
    for (int i = 0; i < n; i++)
      if (tmp == a[i]) {
        b[i] = c[0][I++];
        st.insert(b[i]);
      }
    I = 1;
    for (int i = 0; i < n; i++)
      if (!b[i]) {
        while (st.find(I) != st.end())
          I++;
        b[i] = I;
        I++;
      }
  } else {
    int I = 0, J = 0;
    set<int> st;
    for (int i = 0; i < n; i++) {
      if (a[i] && I < c[1].size()) {
        b[i] = c[1][I++];
        st.insert(b[i]);
      }
      if (!a[i] && J < c[2].size()) {
        b[i] = c[2][J++];
        st.insert(b[i]);
      }
    }
    I = 1;
    for (int i = 0; i < n; i++)
      if (!b[i]) {
        while (st.find(I) != st.end())
          I++;
        b[i] = I;
        I++;
      }
  }
  for (int i = 0; i < n; i++)
    cout << b[i] << " \n"[i == n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

int n;
vvi g;
vi a, b;
vvi c;

void dfs(int v, int p, int t) {
  a[v] = t;
  for (auto u : g[v])
    if (u != p)
      dfs(u, v, 1 - t);
}

int main() {
  cin >> n;
  g = vvi(n);
  a = b = vi(n);
  c = vvi(3);
  for (int i = 1; i <= n; i++)
    c[i % 3].push_back(i);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0, -1, 0);
  int t = 0;
  for (auto i : a)
    t += i;
  if (t <= n / 3 || n - t <= n / 3) {
    int I = 0;
    set<int> st;
    int tmp = (t <= n / 3 ? 1 : 0);
    for (int i = 0; i < n; i++)
      if (tmp == a[i]) {
        b[i] = c[0][I++];
        st.insert(b[i]);
      }
    I = 1;
    for (int i = 0; i < n; i++)
      if (!b[i]) {
        while (st.find(I) != st.end())
          I++;
        b[i] = I;
        I++;
      }
  } else {
    int I = 0, J = 0;
    set<int> st;
    for (int i = 0; i < n; i++) {
      if (a[i] && I < c[1].size()) {
        b[i] = c[1][I++];
        st.insert(b[i]);
      }
      if (!a[i] && J < c[2].size()) {
        b[i] = c[2][J++];
        st.insert(b[i]);
      }
    }
    I = 1;
    for (int i = 0; i < n; i++)
      if (!b[i]) {
        while (st.find(I) != st.end())
          I++;
        b[i] = I;
        I++;
      }
  }
  for (int i = 0; i < n; i++)
    cout << b[i] << " \n"[i == n - 1];
}