Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define x first
#define y second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define sz(a) (int)(a.size())
const int INF = 1000 * 1000 * 1000 + 7;
const ll LINF = INF * (ll)INF;
const int mod = INF;
const int MAX = 1 << 25;
int depth[100005];
int ans[100005];
vector<int> g[100005];
void dfs(int u, int p = -1) {
  for (auto v : g[u]) {
    if (v == p)
      continue;
    depth[v] = depth[u] ^ 1;
    dfs(v, u);
  }
}
void put(vector<int> &a, int val) {
  ans[a.back()] = val;
  a.pop_back();
}
void solve(vector<int> &y, int a, int b, int c) {
  if (c) {
    do {
      put(y, c * 3);
      c--;
    } while (c);
  }
  if (b) {
    do {
      put(y, b * 3 - 1);
      b--;
    } while (b);
  }
  if (a) {
    do {
      put(y, a * 3 - 2);
      a--;
    } while (a);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(0);
  vector<int> A, B;
  for (int i = 0; i < n; i++) {
    if (depth[i] == 0)
      A.pb(i);
    else
      B.pb(i);
  }
  if (sz(A) > sz(B))
    swap(A, B);
  int x = sz(A), y = sz(B);
  int a = (n + 2) / 3, b = (n + 1) / 3, c = n / 3;
  if (x <= c) {
    for (int i = 0; i < x; i++) {
      put(A, c * 3);
      c--;
    }
    solve(B, a, b, c);
    for (int i = 0; i < n; i++)
      cout << ans[i] << " ";
    return 0;
  }
  if (x >= a && y >= b) {
    do {
      put(A, a * 3 - 2);
      a--;
    } while (a);
    do {
      put(B, b * 3 - 1);
      b--;
    } while (b);
    if (A.size()) {
      do {
        put(A, c * 3);
        c--;
      } while (A.size());
    }
    if (B.size()) {
      do {
        put(B, c * 3);
        c--;
      } while (B.size());
    }
    for (int i = 0; i < n; i++)
      cout << ans[i] << " ";
    return 0;
  }

  assert(false);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define x first
#define y second
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define sz(a) (int)(a.size())
const int INF = 1000 * 1000 * 1000 + 7;
const ll LINF = INF * (ll)INF;
const int mod = INF;
const int MAX = 1 << 25;
int depth[200005];
int ans[200005];
vector<int> g[200005];
void dfs(int u, int p = -1) {
  for (auto v : g[u]) {
    if (v == p)
      continue;
    depth[v] = depth[u] ^ 1;
    dfs(v, u);
  }
}
void put(vector<int> &a, int val) {
  ans[a.back()] = val;
  a.pop_back();
}
void solve(vector<int> &y, int a, int b, int c) {
  if (c) {
    do {
      put(y, c * 3);
      c--;
    } while (c);
  }
  if (b) {
    do {
      put(y, b * 3 - 1);
      b--;
    } while (b);
  }
  if (a) {
    do {
      put(y, a * 3 - 2);
      a--;
    } while (a);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(0);
  vector<int> A, B;
  for (int i = 0; i < n; i++) {
    if (depth[i] == 0)
      A.pb(i);
    else
      B.pb(i);
  }
  if (sz(A) > sz(B))
    swap(A, B);
  int x = sz(A), y = sz(B);
  int a = (n + 2) / 3, b = (n + 1) / 3, c = n / 3;
  if (x <= c) {
    for (int i = 0; i < x; i++) {
      put(A, c * 3);
      c--;
    }
    solve(B, a, b, c);
    for (int i = 0; i < n; i++)
      cout << ans[i] << " ";
    return 0;
  }
  if (x >= a && y >= b) {
    do {
      put(A, a * 3 - 2);
      a--;
    } while (a);
    do {
      put(B, b * 3 - 1);
      b--;
    } while (b);
    if (A.size()) {
      do {
        put(A, c * 3);
        c--;
      } while (A.size());
    }
    if (B.size()) {
      do {
        put(B, c * 3);
        c--;
      } while (B.size());
    }
    for (int i = 0; i < n; i++)
      cout << ans[i] << " ";
    return 0;
  }

  assert(false);
}