Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
#define FOR(i,a,n) for(ll i=(ll)a;i<(ll)n;i++)
#define RFOR(i,a,n) for(ll i=(ll)n-1;i >= (ll)a;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,0,n)
#define ALL(v) v.begin(), v.end()
#define bra(first,second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
ll INF = 1001001001001001001;
long double EPS = 1e-11;
long double PI = 3.141592653589793238;
template<typename T>
void remove(std::vector<T>& vector, unsigned int index){
    vector.erase(vector.begin() + index);
}

using Graph = vector<vector<ll>>;

ll N;
Graph G(200010);
ll A[200010];
ll B[200010];
ll d[200010];

queue<ll> Q,Q1,Q2,QUE;
ll depth = 0;

void dijksttra(ll s){
    fill(d,d+200010,INF);
    d[s] = 0;
    QUE.push(s);
    A[s] = Q2.front();
    Q2.pop();
    while(QUE.size()){
        ll x = QUE.front();
        ll n = G[x].size();
        QUE.pop();
        rep(i,n){
            ll y = G[x][i];
            if(d[y] != INF) continue;
            d[y] = d[x] + 1;
            QUE.push(y);
            if(depth % 2 == 0){
                if(Q1.size() != 0){
                    A[y] = Q1.front();
                    Q1.pop();
                }else{
                    if(Q.size() != 0){
                        A[y] = Q.front(); 
                        Q.pop();
                    }else{
                        A[y] = Q2.front();
                        Q2.pop();
                    }
                }
            }else{
                if(Q2.size() != 0){
                    A[y] = Q2.front();
                    Q2.pop();
                }else{
                    if(Q.size() != 0){
                        A[y] = Q.front();
                        Q.pop();
                    }else{
                        A[y] = Q1.front();
                        Q1.pop();
                    }
                }
            }
        }
        depth++;
    }
}

ll p3(ll x){
    ll res = x;
    rep(i,3){
        res = P[res];
        if(i != 2 && res == 0){
            return -1;
        }
    }
    return res;
}
 

int main(){
    cin >> N;
    rep(i,N-1){
        ll a,b;
        cin >> a >> b;
        a--;b--;
        B[a]++;B[b]++;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    FOR(i,1,N+1){
        if(i % 3 == 0)  Q.push(i);
        if(i % 3 == 1) Q1.push(i);
        if(i % 3 == 2) Q2.push(i);
    }
    ll root = 0,bad = 0;
    bool flag = 0;
    rep(i,N){
        if((B[i] - 1) > Q.size() + max(Q1.size(),Q2.size())){
            bad = i;
            flag = 1;
        }
    }
    if(flag){
        ll x = p3(bad);
        A[x] = Q.front();
        Q.pop();
        ll n = G[bad].size();
        rep(i,n){
            if(Q1.size()){
                A[G[bad][i]] = Q1.front();
                Q1.pop();
            }else if(Q2.size()){
                A[G[bad][i]] = Q2.front();
                Q2.pop();
            }else{
                A[G[bad][i]] = Q.front();
                Q.pop();
            }
        }
        rep(i,N){
            if(A[i] != 0) continue;
            if(Q1.size()){
                A[i] = Q1.front();
                Q1.pop();
            }else if(Q2.size()){
                A[i] = Q2.front();
                Q2.pop();
            }else{
                A[i] = Q.front();
                Q.pop();
            }
        }
        rep(i,N){
            cout << A[i] << ' ';
        }
        cout << endl;
        return 0;
    }
    rep(i,N){
        if(B[i] == 1){
            root = i;
            dijksttra(root);
            break;
        }
    }
    rep(i,N){
        cout << A[i] << ' ';
    }
    cout << endl;
}


Compiler output:
a.cc: In function 'll p3(ll)':
a.cc:80:15: error: 'P' was not declared in this scope
   80 |         res = P[res];
      |               ^