Language: C++

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MAX_INF 0x7f
#define MAX_INF_VAL 0x7f7f7f7f
#define pi 3.141592653589
#define eps 1e-6
//#define p 2173412051LL
//#define sz 2

using namespace std;

int id[ 200010 ];
int ans[ 200010 ];
int res[ 200010 ];
int fa[ 200010 ];
vector< int > e[ 200010 ];

void dfs( int );
bool cmp( const int& a, const int& b ) {
	int x, y;
	x = ( fa[ fa[ fa[ a ] ] ] != 0 );
	x += max( 0LLu, ( e[ fa[ fa[ a ] ] ].size() - 1 ) );
	y = ( fa[ fa[ fa[ b ] ] ] != 0 );
	y += max( 0LLu, ( e[ fa[ fa[ b ] ] ].size() - 1 ) );
	return x > y;
}

int main() {
	int n;
	int x, y;
	int a = 1, b = 2, c = 3;
	scanf( "%d", &n );
	for( int i = 1; i < n; ++i ) {
		scanf( "%d%d", &x, &y );
		id[ i ] = i;
		e[ x ].push_back( y );
		e[ y ].push_back( x );
	}
	ans[ 1 ] = 1;
	dfs( 1 );
	id[ n ] = n;
	sort( id + 1, id + 1 + n, cmp );
	for( int i = 1; i <= n; ++i ) {
		if( ans[ id[ i ] ] == 1 ) {
			if( a <= n ) {
				res[ id[ i ] ] = a;
				a += 3;
			} else if( c <= n ) {
				res[ id[ i ] ] = c;
				c += 3;
			} else {
				res[ id[ i ] ] = b;
				b += 3;
			}
		} else {
			if( b <= n ) {
				res[ id[ i ] ] = b;
				b += 3;
			} else if( c <= n ) {
				res[ id[ i ] ] = c;
				c += 3;
			} else {
				res[ id[ i ] ] = a;
				a += 3;
			}
		}
	}
	for( int i = 1; i <= n; ++i )
		printf( "%d ", res[ i ] );
	return 0;
}

void dfs( int x ) {
	for( int i = 0; i < e[ x ].size(); ++i ) {
		if( fa[ x ] == e[ x ][ i ] )
			continue;
		fa[ e[ x ][ i ] ] = x;
		if( ans[ x ] == 1 )
			ans[ e[ x ][ i ] ] = 2;
		else
			ans[ e[ x ][ i ] ] = 1;
		dfs( e[ x ][ i ] );
	}
}

Compiler output:
a.cc: In function 'bool cmp(const int&, const int&)':
a.cc:23:17: error: no matching function for call to 'max(long long unsigned int, std::vector<int>::size_type)'
   23 |         x += max( 0LLu, ( e[ fa[ fa[ a ] ] ].size() - 1 ) );
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:23:17: note:   deduced conflicting types for parameter 'const _Tp' ('long long unsigned int' and 'std::vector<int>::size_type' {aka 'long unsigned int'})
   23 |         x += max( 0LLu, ( e[ fa[ fa[ a ] ] ].size() - 1 ) );
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:23:17: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long unsigned int'
   23 |         x += max( 0LLu, ( e[ fa[ fa[ a ] ] ].size() - 1 ) );
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:25:17: error: no matching function for call to 'max(long long unsigned int, std::vector<int>::size_type)'
   25 |         y += max( 0LLu, ( e[ fa[ fa[ b ] ] ].size() - 1 ) );
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:25:17: note:   deduced conflicting types for parameter 'const _Tp' ('long long unsigned int' and 'std::vector<int>::size_type' {aka 'long unsigned int'})
   25 |         y += max( 0LLu, ( e[ fa[ fa[ b ] ] ].size() - 1 ) );
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:25:17: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long unsigned int'
   25 |         y += max( 0LLu, ( e[ fa[ fa[ b ] ] ].size() - 1 ) );
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~