题意:

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

大意是说给一棵无根树染色,相邻的三个节点颜色不能相同,问至少需要多少种颜色。

思路:

有趣的搜索。

实现:

 #include <iostream>
#include <cstdio>
#include <vector>
using namespace std; vector<int> G[];
int color[];
int ans = ;
void dfs(int now, int f)
{
int x = ;
for (int i = ; i < G[now].size(); i++)
{
if (G[now][i] == f || color[G[now][i]])
continue;
x++;
while (x == color[now] || x == color[f])
x++;
color[G[now][i]] = x;
dfs(G[now][i], now);
}
if (x > ans)
ans = x;
}
int n, x, y;
int main()
{
cin >> n;
for (int i = ; i <= n - ; i++)
{
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
color[] = ;
dfs(, );
cout << ans << endl;
for (int i = ; i <= n; i++)
{
cout << color[i] << " ";
}
cout << endl;
return ;
}

CF781A Andryusha and Colored Balloons的更多相关文章

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

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

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

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

  4. codeforces781A Andryusha and Colored Balloons

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

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

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

  6. C. Andryusha and Colored Balloons

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

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

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

  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. 【转】使用git 工具下载android.jar Source Code

    为了开发android应用,在开发时发现sdk没有源代码,这样在开发时太麻烦了,下面说说如何下载源代码,以及如何配置. 下载源代码需要git,先下载一个git.下面的操作都是在windows下完成的. ...

  2. springboot在eclipse实现热部署

    eclipse使用spring-tool-suite插件创建springboot项目,项目创建完成后. 选中项目,右键 Spring Tools  --> Add Boot Devtools 点 ...

  3. 转:Oracle:删除表空间

    原文:http://space.itpub.net/40239/viewspace-365948 OMF和非OMF管理的数据文件在DROP TABLESPACE时是否会自动删除,做了测试: SQL&g ...

  4. JS截取与分割字符串常用技巧总结

    本文实例讲述了JS截取与分割字符串的常用方法.分享给大家供大家参考,具体如下: JS截取字符串可使用 substring()或者slice() 函数:substring() 定义:substring( ...

  5. python批量读取txt文件为DataFrame

    我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作.比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢? 首先我们要 ...

  6. 【转】[钉钉通知系列]Jenkins发布后自动通知

    转载请注明出处:https://www.cnblogs.com/jianxuanbing/p/7211006.html 阅读目录 一.前言 二.使用钉钉推送的优势 三.配置 一.前言 最近使用Jenk ...

  7. Bootstrap-CSS:图片

    ylbtech-Bootstrap-CSS:图片 1.返回顶部 1. Bootstrap 图片 在本章中,我们将学习 Bootstrap 对图片的支持.Bootstrap 提供了三个可对图片应用简单样 ...

  8. 【旧文章搬运】分析了一下360安全卫士的HOOK(二)——架构与实现

    原文发表于百度空间及看雪论坛,2009-10-14 看雪论坛地址:https://bbs.pediy.com/thread-99460.htm 刚发这篇文章的时候,因为内容涉及360的核心产品,文章被 ...

  9. 一个不当使用fclose引发的异常

    最近服务器上一个后台传输文件的服务,经常会报出异常来,只能强行终止并重启. 昨天刚好有空,现场抓了一下dump,再把程序扔到IDA里看了一下,很快就找出原因了,原来是调用fclose时出错的. 使用C ...

  10. eclipse+PyDev里面import win32api报错的问题解决

    windows下面eclipse+PyDev的开发环境,安装了pywin32,写import win32api时老提示错误,在idle里正常执行. 原来是安装python库时,python安装路径下面 ...