Problem p02749 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
struct edge {
  int to;
  int cost;
  edge(int t, int w) : to(t), cost(w) {}
};
int main() {
  vector<int> y, z;
  stack<int> s;
  int c[200010], ans1[200010], i, n, a, b, ans[200010], x;
  int INF = (1 << 29);
  vector<vector<edge>> g(200010);
  scanf("%d\n", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d\n", &a, &b);
    a--, b--;
    g[a].push_back(edge(b, 1));
    g[b].push_back(edge(a, 1));
  }
  for (i = 0; i < n; i++) {
    ans[i] = 0;
  }
  s.push(0), ans[0] = 1;
  while (!s.empty()) {
    x = s.top();
    s.pop();
    for (i = 0; i < g[x].size(); i++) {
      if (ans[g[x][i].to] == 0) {
        ans[g[x][i].to] = -ans[x];
        s.push(g[x][i].to);
      }
    }
  }
  for (i = 0; i < n; i++) {
    if (ans[i] == 1) {
      y.push_back(i);
    } else {
      z.push_back(i);
    }
  }
  for (i = 0; i <= n; i++) {
    c[i] = 0;
  }
  int d = 0;
  if (y.size() <= n / 3) {
    for (i = 1; i <= n; i++) {
      if (y.size() == d) {
        break;
      }
      if (i % 3 == 0) {
        c[i] = 1;
        ans1[y[d]] = i;
        d++;
      }
    }
    d = 0;
    for (i = 1; i <= n; i++) {
      if (c[i] == 0) {
        ans1[z[d]] = i;
        d++;
      }
    }
  } else if (z.size() <= n / 3) {
    for (i = 1; i <= n; i++) {
      if (z.size() == d) {
        break;
      }
      if (i % 3 == 0) {
        c[i] = 1;
        ans1[z[d]] = i;
        d++;
      }
    }
    d = 0;
    for (i = 1; i <= n; i++) {
      if (c[i] == 0) {
        ans1[y[d]] = i;
        d++;
      }
    }
  } else {
    for (i = 1; i <= n; i++) {
      if (d == y.size()) {
        break;
      }
      if (i % 3 == 1) {
        c[i] = 1;
        ans1[y[d]] = i;
        d++;
      }
    }
    if (d < y.size()) {
      for (i = 1; i <= n; i++) {
        if (d == y.size()) {
          break;
        }
        if (i % 3 == 0) {
          c[i] = 1;
          ans1[y[d]] = i;
          d++;
        }
      }
    }
    d = 0;
    for (i = 1; i <= n; i++) {
      if (c[i] == 0) {
        ans1[z[d]] = i;
        d++;
      }
    }
  }
  for (i = 0; i < n; i++) {
    if (i == 0) {
      printf("%d", ans1[i]);
      continue;
    }
    printf(" %d", ans1[i]);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
struct edge {
  int to;
  int cost;
  edge(int t, int w) : to(t), cost(w) {}
};
int main() {
  vector<int> y, z;
  stack<int> s;
  int c[200010], ans1[200010], i, n, a, b, ans[200010], x;
  int INF = (1 << 29);
  vector<vector<edge>> g(200010);
  scanf("%d\n", &n);
  for (i = 0; i < n - 1; i++) {
    scanf("%d %d\n", &a, &b);
    a--, b--;
    g[a].push_back(edge(b, 1));
    g[b].push_back(edge(a, 1));
  }
  for (i = 0; i < n; i++) {
    ans[i] = 0;
  }
  s.push(0), ans[0] = 1;
  while (!s.empty()) {
    x = s.top();
    s.pop();
    for (i = 0; i < g[x].size(); i++) {
      if (ans[g[x][i].to] == 0) {
        ans[g[x][i].to] = -ans[x];
        s.push(g[x][i].to);
      }
    }
  }
  for (i = 0; i < n; i++) {
    if (ans[i] == 1) {
      y.push_back(i);
    } else {
      z.push_back(i);
    }
  }
  for (i = 0; i <= n; i++) {
    c[i] = 0;
  }
  int d = 0;
  if (y.size() <= n / 3) {
    for (i = 1; i <= n; i++) {
      if (y.size() == d) {
        break;
      }
      if (i % 3 == 0) {
        c[i] = 1;
        ans1[y[d]] = i;
        d++;
      }
    }
    d = 0;
    for (i = 1; i <= n; i++) {
      if (c[i] == 0) {
        ans1[z[d]] = i;
        d++;
      }
    }
  } else if (z.size() <= n / 3) {
    for (i = 1; i <= n; i++) {
      if (z.size() == d) {
        break;
      }
      if (i % 3 == 0) {
        c[i] = 1;
        ans1[z[d]] = i;
        d++;
      }
    }
    d = 0;
    for (i = 1; i <= n; i++) {
      if (c[i] == 0) {
        ans1[y[d]] = i;
        d++;
      }
    }
  } else {
    for (i = 1; i <= n; i++) {
      if (d == y.size()) {
        break;
      }
      if (i % 3 == 1) {
        c[i] = 1;
        ans1[y[d]] = i;
        d++;
      }
    }
    if (d < y.size()) {
      for (i = 1; i <= n; i++) {
        if (d == y.size()) {
          break;
        }
        if (i % 3 == 0) {
          c[i] = 1;
          ans1[y[d]] = i;
          d++;
        }
      }
    }
    d = 0;
    for (i = 1; i <= n; i++) {
      if (c[i] == 0) {
        ans1[z[d]] = i;
        d++;
      }
    }
  }
  for (i = 0; i < n; i++) {
    if (i == 0) {
      printf("%d", ans1[i]);
      continue;
    }
    printf(" %d", ans1[i]);
  }
  return 0;
}