Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects Vertex a_i and Vertex b_i.

Takahashi loves the number 3. He is seeking a permutation p_1, p_2, \ldots , p_N of integers from 1 to N satisfying the following condition:

For every pair of vertices (i, j), if the distance between Vertex i and Vertex j is 3, the sum or product of p_i and p_j is a multiple of 3.

Here the distance between Vertex i and Vertex j is the number of edges contained in the shortest path from Vertex i to Vertex j.

Help Takahashi by finding a permutation that satisfies the condition.

Constraints

2\leq N\leq 2\times 10^5

1\leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

If no permutation satisfies the condition, print -1.

Otherwise, print a permutation satisfying the condition, with space in between.
If there are multiple solutions, you can print any of them.

Sample Input 1

5
1 2
1 3
3 4
3 5

Sample Output 1

1 2 5 4 3

The distance between two vertices is 3 for the two pairs (2, 4) and (2, 5).

p_2 + p_4 = 6

p_2\times p_5 = 6

Thus, this permutation satisfies the condition.

Solution in C (GCC 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int acs(const void *a, const void *b) { 
  return *(int*)a - *(int*)b;
} /* 1,2,3,4.. */
int des(const void *a, const void *b) {
  return *(int*)b - *(int*)a;
} /* 8,7,6,5.. */
int cmp_char(const void *a, const void *b) { 
  return *(char*)a - *(char*)b;
} /* a,b,c,d.. */
int cmp_str(const void *a, const void *b) {
  return strcmp(*(const char **)a, *(const char **)b);
} /* aaa,aab.. */
#define min(a,b) (a < b ? a : b)
#define max(a,b) (a > b ? a : b)
#define rep(i, l, r) for (int i = l; i < r; i++)
#define MAX 100001
#define MOD 1000000007
#define INF 1000000009
typedef long long int lli;

typedef struct list {
  int node;
  struct list *next;
} list;

list *graph[MAX];

void initGraph(void) {
  for (int i = 0; i < MAX; i++)
    graph[i] = NULL;
}

void addEdge(int node1, int node2)
{
  list *new1 = (list *)malloc(sizeof(list *));
  new1->node = node2;
  new1->next = graph[node1];
  graph[node1] = new1;

  list *new2 = (list *)malloc(sizeof(list *));
  new2->node = node1;
  new2->next = graph[node2];
  graph[node2] = new2;
}

int N;
int ans[MAX] = {0};
int the = 3;
int two = 2;
int one = 1;

void dfs(int r, int n, int p, int d) {
  if ( d == 3 ) {
    if ( ans[n] && ans[r] ) return;
    if ( ans[n] == 0 && ans[r] == 0 ) {
      if ( the <= N ) {
        ans[r] = the;
        the += 3;
        ans[n] = the;
      } else {
        ans[r] = one;
        ans[n] = two;
        one += 3; two += 3;
      }
    }
    if ( ans[r] != 0 && ans[r] % 3 == 0 ) {
      if ( one > two ) {
        ans[n] = two;
        two += 3;
      } else {
        ans[n] = one;
        one += 3;
      } 
      return;
    }
    if ( ans[n] != 0 && ans[n] % 3 == 0 ) {
      if ( one > two ) {
        ans[r] = two;
        two += 3;
      } else {
        ans[r] = one;
        one += 3;
      } 
      return;
    }
    if ( ans[n] != 0 && ans[n] % 3 == 0 ) {
      if ( one > two ) {
        ans[r] = two;
        two += 3;
      } else {
        ans[r] = one;
        one += 3;
      } 
      return;
    }
    if ( ans[r] % 3 == 1 ) {
      ans[n] = two;
      two += 3;
      return;
    } 
    if ( ans[r] % 3 == 2 ) {
      ans[n] = one;
      one += 3;
      return;
    }
    if ( ans[n] % 3 == 1 ) {
      ans[r] = two;
      two += 3;
      return;
    }
    if ( ans[n] % 3 == 2 ) {
      ans[r] = one;
      one += 3;
      return;
    }
  }
  list *c = graph[n];
  for ( ; c != NULL; c = c->next ) {
    int cn = c->node;
    if ( cn == p ) continue;
    dfs(r, cn, n, d+1);
  }
}

int main(void) {
  scanf("%d", &N);

  int a, b;
  rep(i, 0, N-1) {
    scanf("%d %d", &a, &b);
    addEdge(a, b);
  }

  rep(i, 1, N+1) dfs(i, i, 0, 0);

  rep(i, 1, N+1) {
    if ( ans[i] ) {
      printf("%d ", ans[i]);
      continue;
    }
    if ( the <= N ) {
      printf("%d ", the);
      the += 3;
      continue;
    }
    if ( two <= N ) {
      printf("%d ", two);
      two += 3;
      continue;
    }
    printf("%d ", one);
    one += 3;
  }

  return 0;
}