Problem p02749 (C++)

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

#define MOD 1000000007
template <typename ty1, typename ty2> inline int add(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x += y;
  return x < MOD ? x : x - MOD;
}
template <typename ty1, typename ty2> inline void addto(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x += y;
  if (x >= MOD)
    x -= MOD;
}
template <typename ty1, typename ty2> inline int sub(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x -= y;
  return x < 0 ? x + MOD : x;
}
template <typename ty1, typename ty2> inline void subto(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x -= y;
  if (x < 0)
    x += MOD;
}
template <typename ty1, typename ty2> inline int mul(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  return 1ll * x * y % MOD;
}
template <typename ty1, typename ty2> void multo(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x = 1ll * x * y % MOD;
}
#define MAX 100002

int n;

vector<int> v[MAX];

vector<int> nd[2];

inline void dfs(int b, int dep = 0, int pr = -1) {
  nd[dep & 1].push_back(b);
  for (int el : v[b]) {
    if (pr == el)
      continue;
    dfs(el, dep + 1, b);
  }
}
vector<int> val[3];
int ans[MAX];
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0);
  for (int i = 1; i <= n; i++)
    val[i % 3].push_back(i);
  auto apply = [&](int a, int b, int c = -1) {
    while (nd[a].size() && val[b].size()) {
      ans[nd[a].back()] = val[b].back();
      nd[a].pop_back();
      val[b].pop_back();
    }
    b = c;
    while (b != -1 && nd[a].size() && val[b].size()) {
      ans[nd[a].back()] = val[b].back();
      nd[a].pop_back();
      val[b].pop_back();
    }
  };
  if (nd[0].size() <= val[0].size()) {
    apply(0, 0);
    apply(1, 1, 2);
    apply(1, 0);
  } else {
    if (nd[1].size() <= val[0].size()) {
      apply(0, 1, 2);
      apply(1, 0);
      apply(0, 0);
    } else {
      if (nd[0].size() >= val[1].size() && nd[1].size() >= val[2].size()) {
        apply(0, 1);
        apply(1, 2);
      } else {
        apply(0, 2);
        apply(1, 1);
      }
      apply(0, 0);
      apply(1, 0);
    }
  }
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", ans[i]);
  }
  puts("");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
template <typename ty1, typename ty2> inline int add(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x += y;
  return x < MOD ? x : x - MOD;
}
template <typename ty1, typename ty2> inline void addto(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x += y;
  if (x >= MOD)
    x -= MOD;
}
template <typename ty1, typename ty2> inline int sub(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x -= y;
  return x < 0 ? x + MOD : x;
}
template <typename ty1, typename ty2> inline void subto(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x -= y;
  if (x < 0)
    x += MOD;
}
template <typename ty1, typename ty2> inline int mul(ty1 x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  return 1ll * x * y % MOD;
}
template <typename ty1, typename ty2> void multo(ty1 &x, ty2 y) {
  if (y >= MOD)
    y %= MOD;
  if (x >= MOD)
    x %= MOD;
  x = 1ll * x * y % MOD;
}
#define MAX 200002

int n;

vector<int> v[MAX];

vector<int> nd[2];

inline void dfs(int b, int dep = 0, int pr = -1) {
  nd[dep & 1].push_back(b);
  for (int el : v[b]) {
    if (pr == el)
      continue;
    dfs(el, dep + 1, b);
  }
}
vector<int> val[3];
int ans[MAX];
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0);
  for (int i = 1; i <= n; i++)
    val[i % 3].push_back(i);
  auto apply = [&](int a, int b, int c = -1) {
    while (nd[a].size() && val[b].size()) {
      ans[nd[a].back()] = val[b].back();
      nd[a].pop_back();
      val[b].pop_back();
    }
    b = c;
    while (b != -1 && nd[a].size() && val[b].size()) {
      ans[nd[a].back()] = val[b].back();
      nd[a].pop_back();
      val[b].pop_back();
    }
  };
  if (nd[0].size() <= val[0].size()) {
    apply(0, 0);
    apply(1, 1, 2);
    apply(1, 0);
  } else {
    if (nd[1].size() <= val[0].size()) {
      apply(0, 1, 2);
      apply(1, 0);
      apply(0, 0);
    } else {
      if (nd[0].size() >= val[1].size() && nd[1].size() >= val[2].size()) {
        apply(0, 1);
        apply(1, 2);
      } else {
        apply(0, 2);
        apply(1, 1);
      }
      apply(0, 0);
      apply(1, 0);
    }
  }
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", ans[i]);
  }
  puts("");
  return 0;
}