Language: C++

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;

struct Graph
{
    int n;
    vector<vector<int>> g;
    
    Graph(int n) : n(n){
        g.resize(n);
    }
    
    void init(int n_){
        n = n_;
        g.resize(n_);
    }
    
    void add_edge(int from, int to){
        g[from].push_back(to);
    }
};

vector<int> BipartiteGraph(Graph &g){
    int n = g.n;
    vector<int> b(n);
    queue<int> que;
    for(int i = 0; i < n; i++){
        if(b[i]) continue;
        b[i] = 1;
        que.push(i);
        while(que.size()){
            int u = que.front();
            que.pop();
            for(int v : g.g[u]){
                if(b[u] == b[v]){
                    for(int i = 0; i < n; i++) b[i] = -1;
                    return b;
                }
                if(b[v] == 0){
                    b[v] = 3 - b[u];
                    que.push(v);
                }
            }
        }
    }
    return b;
}

struct Tree	//create tree(directed) from graph(undirected)
{
    int n;
    int root;
    vector<vector<int>> t;
    vector<int> par;
    vector<int> dpt;
    
    void init(Graph &g, int root_){
        n = g.n;
        root = root_;
        t.resize(n);
        par.resize(n);
        dpt.resize(n);
        fill(dpt.begin(), dpt.end(), -1);
        queue<int> que;
        par[root] = -1;
        dpt[root] = 0;
        que.push(root);
        while(que.size()){
            int pa = que.front();
            que.pop();
            for(int ch : g.g[pa]){
                if(dpt[ch] == -1){
                    t[pa].push_back(ch);
                    par[ch] = pa;
                    dpt[ch] = dpt[pa] + 1;
                    que.push(ch);
                }
            }
        }
    }

    Tree(){}
    
    Tree(Graph &g, int root_){
        init(g, root_);
    }
};

class UnionFind
{
    vector<int> par, rank;
    
public:
    UnionFind(int n){
        par.resize(n);
        rank.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;
            rank[i] = 0;
        }
    }
    
    int find(int i){
        if(i == par[i]) return i;
        return par[i] = find(par[i]);
    }
    
    bool same(int i, int j){
        return find(i) == find(j);
    }
    
    void unite(int i, int j){
        if(same(i, j)) return;
        i = find(i), j = find(j);
        if(rank[i] < rank[j]) par[i] = j;
        else{
            par[j] = i;
            if(rank[i] == rank[j]) rank[i]++;
        }
    }
};

bool f[100005][3];

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.add_edge(a, b);
        g.add_edge(b, a);
    }
    Tree t(g, 0);
    UnionFind uf(n);
    for(int u = 0; u < n; u++){
        if(t.dpt[u] <= 1 || (t.dpt[u] == 2 && t.dpt[t.par[t.par[u]]] == 1) || f[u][0]) continue;
        int v = t.par[u];
        for(int w : t.t[v]){
            uf.unite(u, w);
            f[w][0] = true;
        }
        v = t.par[v];
        if(f[v][2]) continue;
        for(int w : t.t[v]){
            if(w == t.par[u]) continue;
            if(uf.same(u, w)){
                f[v][2] = true;
                break;
            }
            uf.unite(u, w);
        }
        if(t.par[v] != -1) uf.unite(u, t.par[v]);
    }
    vector<int> b = BipartiteGraph(g);
    for(int i = 0; i < n; i++) b[i]--;
    vector<int> v[100005][2];
    for(int i = 0; i < n; i++){
        v[uf.find(i)][b[i]].push_back(i);
    }
    P p[100005];
    for(int i = 0; i < n; i++) p[i] = P(abs((int)v[i][0].size() - (int)v[i][1].size()), i);
    sort(p, p + n, greater<P>());
    int X = n / 3;
    if(n % 3 > 0) X++;
    int Y = n / 3;
    if(n % 3 == 2) Y++;
    int Z = n / 3;
    int x = 0, y = 0, z = 0;
    int ans[100005];
    for(int c = 0; c < n; c++){
        int i = p[c].second;
        if((int)v[i][0].size() > max(X - x, Y - y) + Z - z){
            for(int j : v[i][1]){
                ans[j] = z * 3 + 3;
                z++;
            }
            for(int j : v[i][0]){
                if(X - x > Y - y){
                    ans[j] = x * 3 + 1;
                    x++;
                }
                else{
                    ans[j] = y * 3 + 2;
                    y++;
                }
            }
            continue;
        }
        else if((int)v[i][1].size() > max(X - x, Y - y) + Z - z){
            for(int j : v[i][0]){
                ans[j] = z * 3 + 3;
                z++;
            }
            for(int j : v[i][1]){
                if(X - x > Y - y){
                    ans[j] = x * 3 + 1;
                    x++;
                }
                else{
                    ans[j] = y * 3 + 2;
                    y++;
                }
            }
            continue;
        }
        int s;
        if((X - x > Y - y) == ((int)v[i][0].size() > (int)v[i][1].size())) s = 0;
        else s = 1;
        for(int j : v[i][s]){
            if(x < X){
                ans[j] = x * 3 + 1;
                x++;
            }
            else if(z < Z){
                ans[j] = z * 3 + 3;
                z++;
            }
            else{
                cout << -1 << endl;
                return 0;
            }
        }
        for(int j : v[i][1 - s]){
            if(y < Y){
                ans[j] = y * 3 + 2;
                y++;
            }
            else if(z < Z){
                ans[j] = z * 3 + 3;
                z++;
            }
            else{
                cout << -1 << endl;
                return 0;
            }
        }
    }
    for(int i = 0; i < n; i++) cout << ans[i] << " ";
    cout << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:169:5: error: 'sort' was not declared in this scope; did you mean 'short'?
  169 |     sort(p, p + n, greater<P>());
      |     ^~~~
      |     short