C. Andryusha and Colored Balloons
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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个节点,n-1条边,让你进行染色,要求是a-b,b-c三点的颜色不同,让你给出配色方案,配色从1开始 初步思路:从1点开始搜,每个节点的染色和父亲,祖父的颜色不相同
*/ #include<bits/stdc++.h>
using namespace std ;
#define pb push_back
int col=;//表示总染色的种类
int color[];
vector<int> edge[];
void dfs(int u,int fa){//父节点,祖父节点
int colv=;//将colv定义在循环外边,神来之笔,这样兄弟的颜色肯定是不会重复的了
for(int i=;i<edge[u].size();i++){//遍历儿子节点
int v=edge[u][i];
if(color[v]) continue;
// cout<<v<<endl;
colv++;
while(colv==color[u]||colv==color[fa]) colv++;
col=max(col,colv);
color[v]=colv;
dfs(v,u);
}
}
int n,u,v;
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<n-;i++){
scanf("%d%d",&u,&v);
edge[u].pb(v);
edge[v].pb(u);
}//建图
color[]=;
dfs(,);
printf("%d\n",col);
for(int i=;i<=n;i++){
printf(i==?"%d":" %d",color[i]);
}
printf("\n");
return ;
}

code force 403C.C. Andryusha and Colored Balloons的更多相关文章

  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. 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 ...

  3. codeforces781A Andryusha and Colored Balloons

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

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

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

  5. C. Andryusha and Colored Balloons

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

  6. CodeForces - 780C Andryusha and Colored Balloons(dfs染色)

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

  7. CF781A Andryusha and Colored Balloons

    题意: Andryusha goes through a park each day. The squares and paths between them look boring to Andryu ...

  8. 782C. Andryusha and Colored Balloons DFS

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

  9. 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons

    从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...

随机推荐

  1. 这家IT教育公司太拼了:毕业生找不到工作就全额退学费!

    乐橙谷为了让更多的学生有工作,有高薪工作,已经决定尝试一种更刺激的游戏规则:完成课时的学员如果毕业找不到工作,公司将全额退还学费.这家公司秉承着自己的使命:以尊重的文化,敬畏的心态去努力帮助每个学生实 ...

  2. 分享一个图片上传插件(TP5.0)

    效果预览图: 该插件主要功能是:可预览裁剪图片和保存原图片,执行裁剪图片后会删除 裁剪的原图片目录,以便减少空间.一.下载附件 地址:https://pan.baidu.com/s/1bpxZhM3 ...

  3. [Node.js] 2、利用node-git-server快速搭建git服务器

    本文用到了node-git-server 1.检测本地git版本 该包的使用需要机器上本来就安装git,且git的版本大于等于2.7: ╭─root@lt /home/workspace ╰─# gi ...

  4. sessionStorage、localStorage 存储及如何存储数组与对象

    1.存储,获取,清楚 sessionStorage.setItem("key",val) sessionStorage.getItem("key") sessi ...

  5. 强大的桌面用 PDF 重排工具:K2pdfopt 简明教程

    用 Kindle 阅读 PDF 一直以来都遭到小伙伴们的无限吐槽,在那 Kindle 还能越狱的时代,我们有 Koreader 之类优秀的 Kindle 第三方插件实现 PDF 文档的实时重排,但是随 ...

  6. SpringBoot文档翻译系列——29.SQL数据源

    原创作品,可以转载,但是请标注出处地址: 因为需要使用到这方面内容,所有对这一部分进行了翻译. 29  使用SQL数据源 SpringBoot为SQL数据源提供了广泛支持,从直接使用JdbcTempl ...

  7. js自执行函数写法

    (1)写法1 (function(){ //函数内容 })() (2)写法2 (function(){ //函数内容 }())

  8. [原创]KVM虚拟化管理平台的实现

    KVM虚拟化管理平台的实现 源码链接:https://github.com/wsjhk/IaaS_admin.git 根据KVM虚拟化管理的要求,设计并实现网页操作管理KVM虚拟机.设计原理架构如下图 ...

  9. Redis缓存项目应用架构设计一

    一些项目整理出的项目中引入缓存的架构设计方案,希望能帮助你更好地管理项目缓存,作者水平有限,如有不足还望指点. 一.基础结构介绍 项目中对外提供方法的是CacheProvider和MQProvider ...

  10. Log4j按级别输出日志到不同文件配置分析 (转:projava)

    关于LOG4J 按照级别输出日志,并按照级别输出到不同文件中的说法有很多, 网上贴的最多的log4j.properties的设置是这样的 log4j.rootLogger=info,stdout,in ...