Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;

struct Graph {
  int n;
  vector<vector<int>> g;

  Graph(int n) : n(n) { g.resize(n); }

  void init(int n_) {
    n = n_;
    g.resize(n_);
  }

  void add_edge(int from, int to) { g[from].push_back(to); }
};

vector<int> BipartiteGraph(Graph &g) {
  int n = g.n;
  vector<int> b(n);
  queue<int> que;
  for (int i = 0; i < n; i++) {
    if (b[i])
      continue;
    b[i] = 1;
    que.push(i);
    while (que.size()) {
      int u = que.front();
      que.pop();
      for (int v : g.g[u]) {
        if (b[u] == b[v]) {
          for (int i = 0; i < n; i++)
            b[i] = -1;
          return b;
        }
        if (b[v] == 0) {
          b[v] = 3 - b[u];
          que.push(v);
        }
      }
    }
  }
  return b;
}

int main() {
  int n;
  cin >> n;
  Graph g(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g.add_edge(a, b);
    g.add_edge(b, a);
  }
  vector<int> b = BipartiteGraph(g);
  int a[3]{0};
  for (int i = 0; i < n; i++) {
    a[b[i]]++;
  }
  int x = 1, y = 2, z = 3;
  int ans[100005];
  if (a[1] <= n / 3 || a[2] <= n / 3) {
    int j;
    if (a[1] <= n / 3)
      j = 1;
    else
      j = 2;
    for (int i = 0; i < n; i++) {
      if (b[i] == j) {
        ans[i] = z;
        z += 3;
      } else {
        if (x <= n) {
          ans[i] = x;
          x += 3;
        } else if (y <= n) {
          ans[i] = y;
          y += 3;
        } else {
          ans[i] = z;
          z += 3;
        }
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      if (b[i] == 1) {
        if (x <= n) {
          ans[i] = x;
          x += 3;
        } else {
          ans[i] = z;
          z += 3;
        }
      } else {
        if (y <= n) {
          ans[i] = y;
          y += 3;
        } else {
          ans[i] = z;
          z += 3;
        }
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
  cout << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;

struct Graph {
  int n;
  vector<vector<int>> g;

  Graph(int n) : n(n) { g.resize(n); }

  void init(int n_) {
    n = n_;
    g.resize(n_);
  }

  void add_edge(int from, int to) { g[from].push_back(to); }
};

vector<int> BipartiteGraph(Graph &g) {
  int n = g.n;
  vector<int> b(n);
  queue<int> que;
  for (int i = 0; i < n; i++) {
    if (b[i])
      continue;
    b[i] = 1;
    que.push(i);
    while (que.size()) {
      int u = que.front();
      que.pop();
      for (int v : g.g[u]) {
        if (b[u] == b[v]) {
          for (int i = 0; i < n; i++)
            b[i] = -1;
          return b;
        }
        if (b[v] == 0) {
          b[v] = 3 - b[u];
          que.push(v);
        }
      }
    }
  }
  return b;
}

int main() {
  int n;
  cin >> n;
  Graph g(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g.add_edge(a, b);
    g.add_edge(b, a);
  }
  vector<int> b = BipartiteGraph(g);
  int a[3]{0};
  for (int i = 0; i < n; i++) {
    a[b[i]]++;
  }
  int x = 1, y = 2, z = 3;
  int ans[200005];
  if (a[1] <= n / 3 || a[2] <= n / 3) {
    int j;
    if (a[1] <= n / 3)
      j = 1;
    else
      j = 2;
    for (int i = 0; i < n; i++) {
      if (b[i] == j) {
        ans[i] = z;
        z += 3;
      } else {
        if (x <= n) {
          ans[i] = x;
          x += 3;
        } else if (y <= n) {
          ans[i] = y;
          y += 3;
        } else {
          ans[i] = z;
          z += 3;
        }
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      if (b[i] == 1) {
        if (x <= n) {
          ans[i] = x;
          x += 3;
        } else {
          ans[i] = z;
          z += 3;
        }
      } else {
        if (y <= n) {
          ans[i] = y;
          y += 3;
        } else {
          ans[i] = z;
          z += 3;
        }
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";
  cout << endl;
}