Problem p02749 (C++)

Buggy submission (status: Runtime Error):
/*
二部グラフになるように色を付ける。
不可能ならば空のvectorを返す

0 or 1で色を付ける
-1は頂点0とつながってない部分

vector<int>();

GはGraphを想定
WeightGraphの場合は//+++++の部分を変える必要がある

verify:
https://atcoder.jp/contests/hitachi2020/tasks/hitachi2020_c

*/
#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;

using Graph = vector<vector<int>>;

template <typename G> vector<int> bipartite(const G &g) {
  vector<int> color(g.size(), -1);
  stack<pii> st;
  st.push({0, 0});
  while (!st.empty()) {
    int idx, nc;
    tie(idx, nc) = st.top();
    st.pop();
    for (auto next : g[idx]) {
      if (color[next] == -1) {
        color[idx] = nc ^ 1;
        st.push({next, nc ^ 1}); //++++++++++++++++++++
      } else {
        if (color[next] != nc ^ 1)
          return vector<int>(); //+++++++++++
      }
    }
  }
  return color;
}

signed main() {
  int n;
  cin >> n;
  Graph g(n);

  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  auto color = bipartite(g);

  // vout(color);
  int cnt0 = 0, cnt1 = 0;
  int num0 = 0, num1 = 1;
  for (int i = 0; i < n; i++) {
    if (color[i] == 0)
      cnt0++;
    else
      cnt1++;
  }
  vector<int> ret(n);
  if (cnt0 > cnt1)
    swap(cnt1, cnt0), swap(num1, num0);
  vector<int> v0, v1;
  if (cnt0 <= n / 3) {
    for (int i = 0; i < n / 3; i++) {
      if (i < cnt0)
        v0.push_back(3 * (i + 1));
      else
        v1.push_back(3 * (i + 1));
    }
    for (int i = 1; i <= n; i++) {
      if (i % 3 == 0)
        continue;
      v1.push_back(i);
    }
  } else {
    int now = 1;
    for (int i = 1; i <= n; i++) {
      if (i % 3 == 1)
        v0.push_back(i);
      else if (i % 3 == 2)
        v1.push_back(i);
    }
    for (int i = 0; i < cnt0 - (n + 2) / 3; i++) {
      v0.push_back(now * 3);
      now++;
    }
    for (int i = 0; i < cnt1 - (n + 1) / 3; i++) {
      v1.push_back(now * 3);
      now++;
    }
  }
  int now0 = 0, now1 = 0;
  for (int i = 0; i < n; i++) {
    if (num0 == color[i]) {
      cout << v0[now0] << endl;
      now0++;
    } else {
      cout << v1[now1] << endl;
      now1++;
    }
  }
}

Fixed submission:
/*
二部グラフになるように色を付ける。
不可能ならば空のvectorを返す

0 or 1で色を付ける
-1は頂点0とつながってない部分

vector<int>();

GはGraphを想定
WeightGraphの場合は//+++++の部分を変える必要がある

verify:
https://atcoder.jp/contests/hitachi2020/tasks/hitachi2020_c

*/
#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;

using Graph = vector<vector<int>>;

template <typename G> vector<int> bipartite(const G &g) {
  vector<int> color(g.size(), -1);
  stack<pii> st;
  st.push({0, 0});
  while (!st.empty()) {
    int idx, nc;
    tie(idx, nc) = st.top();
    st.pop();
    for (auto next : g[idx]) {
      if (color[next] == -1) {
        color[next] = nc ^ 1;
        st.push({next, nc ^ 1}); //++++++++++++++++++++
      } else {
        if (color[next] != nc ^ 1)
          return vector<int>(); //+++++++++++
      }
    }
  }
  return color;
}

signed main() {
  int n;
  cin >> n;
  Graph g(n);

  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  auto color = bipartite(g);

  // vout(color);
  int cnt0 = 0, cnt1 = 0;
  int num0 = 0, num1 = 1;
  for (int i = 0; i < n; i++) {
    if (color[i] == 0)
      cnt0++;
    else
      cnt1++;
  }
  vector<int> ret(n);
  if (cnt0 > cnt1)
    swap(cnt1, cnt0), swap(num1, num0);
  vector<int> v0, v1;
  if (cnt0 <= n / 3) {
    for (int i = 0; i < n / 3; i++) {
      if (i < cnt0)
        v0.push_back(3 * (i + 1));
      else
        v1.push_back(3 * (i + 1));
    }
    for (int i = 1; i <= n; i++) {
      if (i % 3 == 0)
        continue;
      v1.push_back(i);
    }
  } else {
    int now = 1;
    for (int i = 1; i <= n; i++) {
      if (i % 3 == 1)
        v0.push_back(i);
      else if (i % 3 == 2)
        v1.push_back(i);
    }
    for (int i = 0; i < cnt0 - (n + 2) / 3; i++) {
      v0.push_back(now * 3);
      now++;
    }
    for (int i = 0; i < cnt1 - (n + 1) / 3; i++) {
      v1.push_back(now * 3);
      now++;
    }
  }
  int now0 = 0, now1 = 0;
  for (int i = 0; i < n; i++) {
    if (num0 == color[i]) {
      cout << v0[now0] << endl;
      now0++;
    } else {
      cout << v1[now1] << endl;
      now1++;
    }
  }
}