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[];
int n,x,y;
int vis[];
void dfs(int x,int y)
{
int cnt=;//上面已解释
for(int i=;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=;i<mp[x].size();i++)//使用新的两个节点
{
if(mp[x][i]!=y)
dfs(mp[x][i],x);
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n-;i++)//领接表建图
{
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
vis[]=;
dfs(,);
int mx=vis[];
for(int i=;i<=n;i++)//取最大颜色
mx=max(mx,vis[i]);
printf("%d\n",mx);
for(int i=;i<=n;i++)
{
printf("%d ",vis[i]);
}
}

CodeForces - 780C Andryusha and Colored Balloons(dfs染色)的更多相关文章

  1. Codeforces 782C. Andryusha and Colored Balloons 搜索

    C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...

  2. 782C. Andryusha and Colored Balloons DFS

    Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...

  3. AC日记——Andryusha and Colored Balloons codeforces 780c

    C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...

  4. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons

    地址:http://codeforces.com/contest/782/problem/C 题目: C. Andryusha and Colored Balloons time limit per ...

  5. code force 403C.C. Andryusha and Colored Balloons

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  6. codeforces781A Andryusha and Colored Balloons

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. C. Andryusha and Colored Balloons

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  8. Codeforces 781A:Andryusha and Colored Balloons(DFS染色)

    http://codeforces.com/contest/782/problem/C 题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色. 思路:比 ...

  9. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

随机推荐

  1. 全站从http升级到https(WordPress博客)

    最近几年HTTPS取代HTTP已经成为大趋势,HTTP是超文本传输协议,信息是明文传输的,而HTTPS是安全超文本传输协议,需要证书和提供安全连接,换句话说,HTTPS是嵌套了SSL加密的HTTP连接 ...

  2. Vue-router中的导航钩子

    vue-router中的导航钩子,主要用来作用是拦截导航,让他完成跳转或取消.(路由守卫) 原文指路:https://blog.csdn.net/weixin_41399785/article/det ...

  3. php url处理

    http_build_query() $data = array("name"=>"callback" , "value"=>& ...

  4. P3784 [SDOI2017]遗忘的集合

    非常神仙的一道题! 题意:给出某n个数字跑完全背包m容量的dp数组,求满足要求的字典序最小的n个元素,不知道n是多少. 首先考虑付公主的背包这个题. 对dp数组求一个ln,设它为F. 已知 e^(G1 ...

  5. chmod 没有x权限怎么办

    解决方法1:    # /lib64/ld-linux-x86-64.so.2 /bin/chmod 755 /bin/chmod   //linux动态命令库   解决方法2:方法2提到的两种方法形 ...

  6. day11 - 15(装饰器、生成器、迭代器、内置函数、推导式)

    day11:装饰器(装饰器形成.装饰器作用.@语法糖.原则.固定模式) 装饰器形成:最简单的.有返回值的.有一个参数的.万能参数 函数起的作用:装饰器用于在已经完成的函数前后增加功能 语法糖:使代码变 ...

  7. 4.1.3 Euclid's Game (POJ 2348)

    Problem description: 以辗转相除法为基础,给定两个整数a和b,Stan和Ollie轮流从较大的数字中减去较小数字的倍数(整倍数),并且相减后的结果不能为零.Stan先手,在自己的回 ...

  8. Bipartite Segments CodeForces - 901C (区间二分图计数)

    大意: 给定无向图, 无偶环, 每次询问求[l,r]区间内, 有多少子区间是二分图. 无偶环等价于奇环仙人掌森林, 可以直接tarjan求出所有环, 然后就可以预处理出每个点为右端点时的答案. 这样的 ...

  9. 第一阶段——站立会议总结DAY10

    1.昨天做了什么:找到了一些模板,把自己的修改了修改,排版了一下. 2.今天准备做什么:做最后的整理,添加一些小图标一些的.还要把按钮的字体换成红色. 3.遇到的困难:一般定义的文字和下拉菜单的文字的 ...

  10. UI界面按钮增强

    (我是菜鸟,知道的少) 1.有HTML页面代码的,可以编辑 <%@page language="abap" %> <%@extension name=" ...