Language: C++

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<deque>
#include<numeric>
#include<queue>
#include<stack>
#include<cstring>
#include<limits>
#include<functional>
#include<unordered_set>
#include<iomanip>
#include<cassert>
#include<regex>
#include<bitset>
#include<complex>
#include<chrono>
#include<random>
#define rep(i,a) for(int i=(int)0;i<(int)a;++i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(),x.end()
using ll=long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

ll gcd(ll n, ll m) {
	ll tmp;
	while (m!=0) {
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m) {
	return abs(n * m) / gcd(n, m);//gl=xy
}
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
 
using namespace std;

vector<vector<int>>g;
vector<int>color;//0:1,1:2
int one,two;

void dfs(int v,int pre,int c){
  color[v]=c;
  if(c==0)++one;
  else ++two;
  for(auto x:g[v]){
    if(x!=pre)dfs(x,v,1-c);
  }
}
void solve(){
  int n;
  cin>>n;
  g.resize(n);
  one=0,two=0;
  color.resize(n);
  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    a--;b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  dfs(0,0);
  vector<int>ans(n);
  set<int>st,st1,st2;
  for(int i=3;i<=n;i+=3)st.emplace(i);
  for(int i=1;i<=n;i+=3)st1.emplace(i);
  for(int i=2;i<=n;i+=3)st2.emplace(i);
  if((one>n/3)&&(two>n/3)){
    rep(i,n){
      if ((color[i]==0)&&(!st1.empty())){
        ans[i]=*st1.begin();
        st1.erase(*st1.begin());
      }
      else if((color[i]==1)&&(!st2.empty())){
        ans[i]=*st2.begin();
        st2.erase(*st2.begin());
      }
      else {
        ans[i]=*st.begin();
        st.erase(*st.begin());
      }
    }
  }
  else if(two<=n/3){//2が少ない
    rep(i,n){
      if(color[i]==1){
        ans[i]=*st.begin();
        st.erase(*st.begin());
      }
      else{
        if(!st.empty()){
          ans[i]=*st.begin();
          st.erase(*st.begin());
        }
        else if(!st2.empty()){
          ans[i]=*st2.begin();
          st2.erase(*st2.begin());
        }
        else{
          ans[i]=*st1.begin();
          st1.erase(*st1.begin());
        }
      }
    }
  }
  else{//1が少ない
    rep(i,n){
      if(color[i]==0){
        ans[i]=*st.begin();
        st.erase(*st.begin());
      }
      else{
        if(!st.empty()){
          ans[i]=*st.begin();
          st.erase(*st.begin());
        }
        else if(!st2.empty()){
          ans[i]=*st2.begin();
          st2.erase(*st2.begin());
        }
        else{
          ans[i]=*st1.begin();
          st1.erase(*st1.begin());
        }
      }
    }
  }
  rep(i,n){
    if(i)cout<<" ";
    cout<<ans[i];
  }
}

int main(){
	ios::sync_with_stdio(false);
  cin.tie(0);
	cout<<fixed<<setprecision(15);
  solve();
	return 0;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:88:6: error: too few arguments to function 'void dfs(int, int, int)'
   88 |   dfs(0,0);
      |   ~~~^~~~~
a.cc:67:6: note: declared here
   67 | void dfs(int v,int pre,int c){
      |      ^~~