Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef int LL;
#define pb push_back
const LL maxn = 1e6 + 9;
struct node {
  LL to, nxt;
} dis[maxn];
LL n, num;
LL head[maxn], p[maxn], dep[maxn];
std::vector<LL> V[3], a[3];
void Add(LL u, LL v) {
  dis[++num] = (node){v, head[u]};
  head[u] = num;
}
void Dfs(LL u, LL f) {
  V[dep[u]].pb(u);
  for (LL i = head[u]; i; i = dis[i].nxt) {
    LL v(dis[i].to);
    if (v == f)
      continue;
    dep[v] = (dep[u] ^ 1);
    Dfs(v, u);
  }
}
int main() {
  scanf("%d", &n);
  for (LL i = 1; i < n; ++i) {
    LL u, v;
    scanf("%d%d", &u, &v);
    Add(u, v);
    Add(v, u);
  }
  for (LL i = 1; i <= n; ++i) {
    a[i % 3].pb(i);
  }
  LL ret(n / 3);
  Dfs(1, 0);
  if (V[0].size() > V[1].size())
    std::swap(V[0], V[1]);
  LL tot0(0), tot1(0), tot2(0);
  if (V[0].size() <= ret) {
    for (LL i = 0; i < V[0].size(); ++i) {
      LL x(V[0][i]);
      p[x] = a[0][tot0++];
    }
    for (LL i = 0; i < V[1].size(); ++i) {
      LL x(V[0][i]);
      if (tot2 < a[2].size())
        p[x] = a[2][tot2++];
      else if (tot0 < a[0].size())
        p[x] = a[0][tot0++];
      else
        p[x] = a[1][tot1++];
    }
    for (LL i = 1; i <= n; ++i)
      printf("%d ", p[i]);
    puts("");
    return 0;
  }
  for (LL i = 0; i < V[0].size(); ++i) {
    LL x(V[0][i]);
    if (tot1 < a[1].size())
      p[x] = a[1][tot1++];
    else if (tot0 < a[0].size())
      p[x] = a[0][tot0++];
    else
      p[x] = a[2][tot2++];
  }
  for (LL i = 0; i < V[1].size(); ++i) {
    LL x(V[1][i]);
    if (tot2 < a[2].size())
      p[x] = a[2][tot2++];
    else if (tot0 < a[0].size())
      p[x] = a[0][tot0++];
    else
      p[x] = a[1][tot1++];
  }
  for (LL i = 1; i <= n; ++i)
    printf("%d ", p[i]);
  puts("");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef int LL;
#define pb push_back
const LL maxn = 1e6 + 9;
struct node {
  LL to, nxt;
} dis[maxn];
LL n, num;
LL head[maxn], p[maxn], dep[maxn];
std::vector<LL> V[3], a[3];
void Add(LL u, LL v) {
  dis[++num] = (node){v, head[u]};
  head[u] = num;
}
void Dfs(LL u, LL f) {
  V[dep[u]].pb(u);
  for (LL i = head[u]; i; i = dis[i].nxt) {
    LL v(dis[i].to);
    if (v == f)
      continue;
    dep[v] = (dep[u] ^ 1);
    Dfs(v, u);
  }
}
int main() {
  scanf("%d", &n);
  for (LL i = 1; i < n; ++i) {
    LL u, v;
    scanf("%d%d", &u, &v);
    Add(u, v);
    Add(v, u);
  }
  for (LL i = 1; i <= n; ++i) {
    a[i % 3].pb(i);
  }
  LL ret(n / 3);
  Dfs(1, 0);
  if (V[0].size() > V[1].size())
    std::swap(V[0], V[1]);
  LL tot0(0), tot1(0), tot2(0);
  if (V[0].size() <= ret) {
    for (LL i = 0; i < V[0].size(); ++i) {
      LL x(V[0][i]);
      p[x] = a[0][tot0++];
    }
    for (LL i = 0; i < V[1].size(); ++i) {
      LL x(V[1][i]);
      if (tot2 < a[2].size())
        p[x] = a[2][tot2++];
      else if (tot0 < a[0].size())
        p[x] = a[0][tot0++];
      else
        p[x] = a[1][tot1++];
    }
    for (LL i = 1; i <= n; ++i)
      printf("%d ", p[i]);
    puts("");
    return 0;
  }
  for (LL i = 0; i < V[0].size(); ++i) {
    LL x(V[0][i]);
    if (tot1 < a[1].size())
      p[x] = a[1][tot1++];
    else if (tot0 < a[0].size())
      p[x] = a[0][tot0++];
    else
      p[x] = a[2][tot2++];
  }
  for (LL i = 0; i < V[1].size(); ++i) {
    LL x(V[1][i]);
    if (tot2 < a[2].size())
      p[x] = a[2][tot2++];
    else if (tot0 < a[0].size())
      p[x] = a[0][tot0++];
    else
      p[x] = a[1][tot1++];
  }
  for (LL i = 1; i <= n; ++i)
    printf("%d ", p[i]);
  puts("");
  return 0;
}