Language: C++

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_V(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl;
#define DEBUG_M(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}

vector<bool> visited, visited2;
int edge = -1;
using Graph = VV<int>;

void findEdge(const Graph &G, int v) {
    if (edge != -1)return;
    visited[v] = true;
    bool update = false;

    for (auto nx : G[v]) {
        if (edge != -1) return;
        if (visited[nx])continue;
        update = true;
        findEdge(G, nx);
    }
    if (edge != -1 or !update) {
        edge = v;
        return;
    }
}

V<int> dis[200010];
int cnt = 0, maxCnt = -1;

void dfs(const Graph &G, int v) {
    visited2[v] = true;
    maxCnt = max(maxCnt, cnt);
    dis[cnt].push_back(v);

    for (auto nx : G[v]) {
        if (visited2[nx])continue;
        cnt++;
        dfs(G, nx);
        cnt--;
    }
}

int main() {
    int N;
    cin >> N;
    Graph G(N);
    for (int i = 0; i < N - 1; ++i) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    visited.assign(N, false);
    visited2.assign(N, false);

    findEdge(G, 0);
    dfs(G, edge);

    if (maxCnt < 3) {
        for (int i = 1; i <= N; i++) {
            cout << i << " ";
        }
        cout << endl;
        return 0;
    }

    int syo = N / 3, amari = N % 3;
    int a0 = syo;
    int a1 = syo + ((amari > 0) ? 1 : 0);
    int a2 = syo + ((amari > 1) ? 1 : 0);
    vector<int> ans(N, -1);
    int a0num = 3, maxa0 = 3 * a0;
    int a1num = 1, maxa1 = 3 * (a1 - 1) + 1;
    int a2num = 2, maxa2 = 3 * (a2 - 1) + 2;

    vector<pair<int, int>> dis3;
    unordered_set<int> mod;
    for (int i = 0; i <= maxCnt - 3; i++) {
        for (int x : dis[i]) {
            if(mod.find(x)!=mod.end())continue;
            for (int y : dis[i + 3]) {
                if(mod.find(y)!=mod.end())continue;
                if(a0num>maxa0){
                    if(a1num<=maxa1 && a2num<=maxa2){
                        mod.insert(x);
                        mod.insert(y):
                        ans[x] = a1num;
                        a1num+=3;
                        ans[y] = a2num;
                        a2num+=3;
                    }
                }else{
                    mod.insert(x);
                    mod.insert(y):
                    ans[x] = a0num;
                    a0num+=3;
                    if(a1num<=maxa1){
                        ans[y] = a1num;
                        a1num+=3;
                    }else if(a0num<=maxa0){
                        ans[y] = a0num;
                        a0num+=3;
                    }else{
                        cout<<-1<<endl;
                        return 0;
                    }

                }
            }
        }
    }

    vector<int> unused;
    for (int i = a0num; i <= maxa0; i += 3) {
        unused.push_back(i);
    }
    for (int i = a1num; i <= maxa1; i += 3) {
        unused.push_back(i);
    }
    for (int i = a2num; i <= maxa2; i += 3) {
        unused.push_back(i);
    }
    int p = 0;
    for (int i = 0; i < N; i++) {
        if (p >= (int) unused.size()) {
            cout << -1 << endl;
            return 0;
        }
        if (ans[i] == -1) {
            ans[i] = unused[p];
            p++;
        }
    }
    for (int i = 0; i < N; i++) {
        cout << ans[i] << " ";
    }
    cout << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:94:38: error: expected ';' before ':' token
   94 |                         mod.insert(y):
      |                                      ^
      |                                      ;
a.cc:102:34: error: expected ';' before ':' token
  102 |                     mod.insert(y):
      |                                  ^
      |                                  ;