• CodeForces


    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.

    题意:给出n个节点,让你给这棵树染色,要求用的颜色最少,然后要求任意三个相连的颜色不能相同___________________________________________________________________________________
    思路:最原始的染色是黑白染色,只要求相邻不相同就行,而这个要求三个相连不相同,其实我们只要保存前两个节点颜色是什么,然后再去找即可,有一个要理解的地方,就是由两个节点推导过来的颜色,那个点相邻的几个节点肯定颜色都是不同的,如图

    从上图中可以看到这是错误的,说明由两点推出来的点肯定都不一样,所以上图那三点应该分别染色 3,4,5,详细见代码

    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    using namespace std;
    vector<int> mp[200001]; 
    int n,x,y;
    int vis[200001];
    void dfs(int x,int y)
    {
        int cnt=1;//上面已解释
        for(int i=0;i<mp[x].size();i++)
        {
            if(mp[x][i]==y) continue;
            while(cnt==vis[x]||cnt==vis[y])//寻找可以赋值的颜色
                cnt++;
            vis[mp[x][i]]=cnt++;//给节点染色
        }
        for(int i=0;i<mp[x].size();i++)//使用新的两个节点
        {
            if(mp[x][i]!=y)
                dfs(mp[x][i],x);
        }
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n-1;i++)//领接表建图
        {
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        vis[1]=1;
        dfs(1,0);
        int mx=vis[1];
        for(int i=1;i<=n;i++)//取最大颜色
            mx=max(mx,vis[i]);
        printf("%d
    ",mx);
        for(int i=1;i<=n;i++)
        {
            printf("%d ",vis[i]);
        }
    }
  • 相关阅读:
    Lua大整数的实现
    std::allocator在stl容器中使用问题
    深度学习框架安装
    Tensorflow安装使用一段时间后,import时出现错误:ImportError: DLL load failed
    论文解读:SIFA
    多位微软MVP推荐,第一本ASP.NET Core 3.1的书来了
    ASP.NET Core 进程内与进程外的性能对比
    基于Netty的程序主动发送消息
    dbroot文件结构解析(一)
    qtree文件结构解析(二)
  • 原文地址:https://www.cnblogs.com/Lis-/p/9826848.html
Copyright © 2020-2023  润新知