Language: C++

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

int color[200005];

void BFS(vector<int> adj[] , int s)
{
    queue<int> Q;
    color[s] = 1;
    Q.push(s);

    while(!Q.empty())
    {
        int p = Q.front();
        Q.pop();
        for(int i=0 ; i<adj[p].size() ; i++)
        {
            if(color[adj[p][i]] == 0)
            {
                if(color[p]==1)
                    color[adj[p][i]] = 2;
                else
                    color[adj[p][i]] = 1;
                Q.push(adj[p][i]);
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    int n;
    cin >> n;
    vector<int>  adj[n+1];
    for(int i=0 ; i<(n-1) ; i++)
    {
        int u,v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    BFS(adj,1);

    ll cc = 0;
   for(int i=1 ; i<=n ;i++)
    {
        if(color[i] == 1)
            cc++;
    }
    if(cc > n/2)
    {
        for(int i=1 ; i<=n ; i++)
        {
            if(color[i] == 1)
                color[i] = 2;
            else
                color[i] = 1;
        }
        cc=0;
        for(int i=1 ; i<=n ; i++)
        {
            if(color[i] == 1)
                cc++;
        }
    }
    ll ans[n+1];
    ll ka = n/3;
    if(cc <= ka)
    {
       vector<int> t0,t1,t2;
       for(int i=1 ; i<=n ; i++)
       {
           if(i%3==0)
            t0.push_back(i);
           else if(i%3==1)
            t1.push_back(i);
           else
            t2.push_back(i);
       }

       int p1=0,p2=0,p0=0;
       for(int i=1 ; i<=n ; i++)
       {
           if(color[i] == 1)
                ans[i] = t0[p0++];
           else
           {
               if(p0<t0.size())
                ans[i] = t0[p0++];
               else if(p1<t1.size())
                ans[i] = t1[p1++];
               else if(p2 < t2.size())
                ans[i] = t2[p2++];
           }
       }
       for(int i=1 ; i<=n ;i++)
        cout << ans[i] << " ";
    }
    else
    {
       vector<int> t0,t1,t2;
       for(int i=1 ; i<=n ; i++)
       {
           if(i%3==0)
            t0.push_back(i);
           else if(i%3==1)
            t1.push_back(i);
           else
            t2.push_back(i);
       }

       int p1=0,p2=0,p0=0;
       for(int i=1 ; i<=n ; i++)
       {
           if(color[i] == 1)
           {
               if(p1<t1.size())
               {
                   ans[i] = t1[p1];
                   p1++;
               }
               else if(p0<t0.size())
                ans[i] = t0[p0++];
                else
                    ans[i] = t2[p2++];
           }
           else
           {
               if(p2<t2.size())
               {
                   ans[i] = t2[p2];
                   p2++;
               }
               else ig(p0<t0.size())
                ans[i] = t0[p0++];
                else
                    ans[i] = t1[p1++];
           }
       }
       for(int i=1 ; i<=n ;i++)
        cout << ans[i] << " ";
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:139:21: error: 'ig' was not declared in this scope; did you mean 'i'?
  139 |                else ig(p0<t0.size())
      |                     ^~
      |                     i
a.cc:141:17: error: 'else' without a previous 'if'
  141 |                 else
      |                 ^~~~