Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> //sort,reverse
#include <deque>     //double_ended queue
#include <iostream>
#include <queue> //queue,priority_queue
#include <string>
#include <utility> //pair
#include <vector>
using namespace std;
typedef pair<int, int> P; // Pでpair<-,->を表す。
typedef long long LL;
LL MOD = 1000000007;

/*ここから*/
int V;                  // 頂点の個数
vector<int> e[2000010]; // iから出る辺
int t[2000010];         // 親情報の記録媒体
int h[2000010];
bool h_flg[2000010];

// すでにある辺と頂点の情報から木の作成
void Tree(int s) { // sは一番根元
  for (int i = 1; i <= V; i++) {
    t[i] = i; // 初期情報
  }
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int from;
    from = q.front();
    q.pop();
    while (!e[from].empty()) {
      int to;
      to = e[from].back();
      e[from].pop_back();
      if (to != t[from]) {
        t[to] = from;
        q.push(to);
      }
    }
  }
}

void height(int s) {
  if (t[s] == s) {
    h[s] = 1;
    h_flg[s] = true;
  } else {
    if (!h_flg[t[s]]) {
      height(s);
    }
    h[s] = h[t[s]] + 1;
    h_flg[s] = true;
  }
}

/*ここまで*/
int main() {
  int n;
  cin >> n;
  V = n;
  for (int i = 0; i < V - 1; i++) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  Tree(1);
  for (int i = 0; i < 2000010; i++) {
    h[i] = 0;
    h_flg[i] = false;
  }
  for (int i = 1; i <= V; i++) {
    height(i);
  }
  // for(int i=1;i<=V;i++){cout<<h[i]<<endl;}
  int ans[n + 1];
  int cnt1 = 0;
  int cnt2 = 0;
  int b1 = 0;
  int b2 = 0;
  if (n % 3 == 0) {
    b1 = n / 3;
    b2 = n / 3;
  } else if (n % 3 == 1) {
    b1 = n / 3 + 1;
    b2 = n / 3;
  } else {
    b1 = n / 3 + 1;
    b2 = n / 3 + 1;
  }
  for (int i = 1; i <= n; i++) {
    if (h[i] % 2 == 1) {
      cnt1++;
      if (cnt1 <= b1) {
        ans[i] = 1;
      } else {
        ans[i] = 3;
      }
    } else {
      cnt2++;
      if (cnt2 <= b2) {
        ans[i] = 2;
      } else {
        ans[i] = 3;
      }
    }
  }
  if (cnt1 < b1) {
    int x = cnt1;
    cnt1 = 0;
    cnt2 = 0;
    int cnt3 = x;
    for (int i = 1; i <= n; i++) {
      if (ans[i] == 1) {
        ans[i] = 3 * x;
        x--;
      } else {
        if (cnt1 < b1) {
          ans[i] = 3 * cnt1 + 1;
          cnt1++;
        } else if (cnt2 < b2) {
          ans[i] = 3 * cnt2 + 2;
          cnt2++;
        } else {
          ans[i] = cnt3 * 3 + 3;
          cnt3++;
        }
      }
    }
  } else if (cnt2 < b2) {
    int x = cnt2;
    cnt1 = 0;
    cnt2 = 0;
    int cnt3 = x;
    for (int i = 1; i <= n; i++) {
      if (ans[i] == 2) {
        ans[i] = 3 * x;
        x--;
      } else {
        if (cnt1 < b1) {
          ans[i] = 3 * cnt1 + 1;
          cnt1++;
        } else if (cnt2 < b2) {
          ans[i] = 3 * cnt2 + 2;
          cnt2++;
        } else {
          ans[i] = cnt3 * 3 + 3;
          cnt3++;
        }
      }
    }
  } else {
    cnt1 = 0;
    cnt2 = 0;
    int cnt3 = 0;
    for (int i = 1; i <= n; i++) {
      if (ans[i] == 1) {
        ans[i] = cnt1 * 3 + 1;
        cnt1++;
      } else if (ans[i] == 2) {
        ans[i] = cnt2 * 3 + 2;
        cnt2++;
      } else {
        ans[i] = cnt3 * 3 + 3;
        cnt3++;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    cout << ans[i] << ' ';
  }
  cout << ans[n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm> //sort,reverse
#include <deque>     //double_ended queue
#include <iostream>
#include <queue> //queue,priority_queue
#include <string>
#include <utility> //pair
#include <vector>
using namespace std;
typedef pair<int, int> P; // Pでpair<-,->を表す。
typedef long long LL;
LL MOD = 1000000007;

/*ここから*/
int V;                  // 頂点の個数
vector<int> e[2000010]; // iから出る辺
int t[2000010];         // 親情報の記録媒体
int h[2000010];
bool h_flg[2000010];

// すでにある辺と頂点の情報から木の作成
void Tree(int s) { // sは一番根元
  for (int i = 1; i <= V; i++) {
    t[i] = i; // 初期情報
  }
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int from;
    from = q.front();
    q.pop();
    while (!e[from].empty()) {
      int to;
      to = e[from].back();
      e[from].pop_back();
      if (to != t[from]) {
        t[to] = from;
        q.push(to);
      }
    }
  }
}

void height(int s) {
  if (t[s] == s) {
    h[s] = 1;
    h_flg[s] = true;
  } else {
    if (!h_flg[t[s]]) {
      height(t[s]);
    }
    h[s] = h[t[s]] + 1;
    h_flg[s] = true;
  }
}

/*ここまで*/
int main() {
  int n;
  cin >> n;
  V = n;
  for (int i = 0; i < V - 1; i++) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
  }
  Tree(1);
  for (int i = 0; i < 2000010; i++) {
    h[i] = 0;
    h_flg[i] = false;
  }
  for (int i = 1; i <= V; i++) {
    height(i);
  }
  // for(int i=1;i<=V;i++){cout<<h[i]<<endl;}
  int ans[n + 1];
  int cnt1 = 0;
  int cnt2 = 0;
  int b1 = 0;
  int b2 = 0;
  if (n % 3 == 0) {
    b1 = n / 3;
    b2 = n / 3;
  } else if (n % 3 == 1) {
    b1 = n / 3 + 1;
    b2 = n / 3;
  } else {
    b1 = n / 3 + 1;
    b2 = n / 3 + 1;
  }
  for (int i = 1; i <= n; i++) {
    if (h[i] % 2 == 1) {
      cnt1++;
      if (cnt1 <= b1) {
        ans[i] = 1;
      } else {
        ans[i] = 3;
      }
    } else {
      cnt2++;
      if (cnt2 <= b2) {
        ans[i] = 2;
      } else {
        ans[i] = 3;
      }
    }
  }
  if (cnt1 < b1) {
    int x = cnt1;
    cnt1 = 0;
    cnt2 = 0;
    int cnt3 = x;
    for (int i = 1; i <= n; i++) {
      if (ans[i] == 1) {
        ans[i] = 3 * x;
        x--;
      } else {
        if (cnt1 < b1) {
          ans[i] = 3 * cnt1 + 1;
          cnt1++;
        } else if (cnt2 < b2) {
          ans[i] = 3 * cnt2 + 2;
          cnt2++;
        } else {
          ans[i] = cnt3 * 3 + 3;
          cnt3++;
        }
      }
    }
  } else if (cnt2 < b2) {
    int x = cnt2;
    cnt1 = 0;
    cnt2 = 0;
    int cnt3 = x;
    for (int i = 1; i <= n; i++) {
      if (ans[i] == 2) {
        ans[i] = 3 * x;
        x--;
      } else {
        if (cnt1 < b1) {
          ans[i] = 3 * cnt1 + 1;
          cnt1++;
        } else if (cnt2 < b2) {
          ans[i] = 3 * cnt2 + 2;
          cnt2++;
        } else {
          ans[i] = cnt3 * 3 + 3;
          cnt3++;
        }
      }
    }
  } else {
    cnt1 = 0;
    cnt2 = 0;
    int cnt3 = 0;
    for (int i = 1; i <= n; i++) {
      if (ans[i] == 1) {
        ans[i] = cnt1 * 3 + 1;
        cnt1++;
      } else if (ans[i] == 2) {
        ans[i] = cnt2 * 3 + 2;
        cnt2++;
      } else {
        ans[i] = cnt3 * 3 + 3;
        cnt3++;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    cout << ans[i] << ' ';
  }
  cout << ans[n] << endl;
  return 0;
}