CodeForces - 780C Andryusha and Colored Balloons ( dfs )

Andryusha and Colored Balloons

Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.

The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if ab and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.

Andryusha wants to use as little different colors as possible. Help him to choose the colors!

Input

The first line contains single integer n (3 ≤ n ≤ 2·105) — the number of squares in the park.

Each of the next (n - 1) lines contains two integers x and y (1 ≤ x, y ≤ n) — the indices of two squares directly connected by a path.

It is guaranteed that any square is reachable from any other using the paths.

Output

In the first line print single integer k — the minimum number of colors Andryusha has to use.

In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.

Examples

Input

3
2 3
1 3

Output

3
1 3 2 

Input

5
2 3
5 3
4 3
1 3

Output

5
1 3 2 5 4 

Input

5
2 1
3 2
4 3
5 4

Output

3
1 2 3 1 2 

Note

In the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.

 Illustration for the first sample.

In the second example there are following triples of consequently connected squares:

  • 1 → 3 → 2
  • 1 → 3 → 4
  • 1 → 3 → 5
  • 2 → 3 → 4
  • 2 → 3 → 5
  • 4 → 3 → 5

We can see that each pair of squares is encountered in some triple, so all colors have to be distinct.

 Illustration for the second sample.

In the third example there are following triples:

  • 1 → 2 → 3
  • 2 → 3 → 4
  • 3 → 4 → 5

We can see that one or two colors is not enough, but there is an answer that uses three colors only.

 Illustration for the third sample.

题意:给你一颗树,然后给树上颜色,要求 一个节点本身 和 以及 此节点所有 儿子的颜色都不相同,问最后需要的最少颜色总数,输出每一个节点的颜色。

思路:dfs 遍历整个树。 很显然,dfs 遍历树的时候只能遍历自己的儿子,而对于当前遍历到的点, 其父亲节点也可说是 此点的儿子结点,但是  其父亲是先上的颜色, 所以又要与  此点的儿子结点颜色不同, 所以递归 还需要一个变量就是  当前结点的父亲节点的颜色。 

AC代码:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
vector<int>s[maxn];         /// vector [i]  用来存放 i 节点的儿子节点
int vis[maxn];
int n;

void dfs(int p,int fa,int col){             /// col 为其父亲的颜色
    int k = vis[p],tmp = 1;                 /// tmp 为颜色,因为要最少,除开固定条件外,颜色都是可重复用的。故从1开始
    for(int i = 0;i < s[p].size();i ++){
        int son = s[p][i];
        if(son == fa) continue;
        if(tmp == k || tmp == col) ++ tmp;  /// 当前此节点的儿子节点使用到的颜色 如果被 此节点父亲或此节点用过了,tmp 往下一个颜色 
        if(tmp == k || tmp == col) ++ tmp;  /// 因为 要判断 颜色是否被此节点父亲或此节点用过, 所以得判断两次
        
        vis[son] = tmp ++;
    }
    for(int i = 0;i < s[p].size();i ++){
        int son = s[p][i];
        if(son == fa) continue;
        dfs(son,p,vis[p]);
    }
}

int main()
{
    while(~scanf("%d",&n)){
        memset(vis,0,sizeof(vis));
        for(int i = 1;i < maxn;i ++) s[i].clear();
        int u,v;
        for(int i = 1;i < n;i ++){
            scanf("%d%d",&u,&v);
            s[u].push_back(v);
            s[v].push_back(u);
        }
        vis[1] = 1;
        dfs(1,-1,0);
        set<int> se;
        for(int i = 1;i <= n;i ++)
            se.insert(vis[i]);
        printf("%d\n",se.size());
        for(int i = 1;i <= n;i ++)
            printf("%d ",vis[i]);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/82054245
今日推荐