C. 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
题意:有n个节点,有n-1条边并且保证全部联通(是一棵生成树),相连的三个节点不能同色,例如A与B相连,B与C相连,那么ABC不能同色,输出颜色的最小种类和一种解决方案。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
vector<int> v[];
int k=;
int ans[];
void dfs(int x,int q)
{
int d=;
for(int i=;i<v[x].size();i++)
{
if(ans[v[x][i]]==)
{
while(d==ans[x]||d==ans[q]) d++;
ans[v[x][i]]=d++;
k=max(ans[v[x][i]],k);
dfs(v[x][i],x);
}
}
}
int main()
{
int n,i,j,a,b;
scanf("%d",&n);
for(i=; i<n-; i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
ans[]=;
dfs(,);
printf("%d\n",k);
for(i=;i<=n;i++)
{
if(i!=)
printf(" %d",ans[i]);
if(i==)
printf("%d",ans[i]);
}
return ;
}

解析:利用vector开辟动态数组,储存每个点互相链接的点,从1开始dfs遍历,并且传两个参数x,q,前者代表当前遍历的点,后者代表前一个的点,只要参考这两个点就能找的第三点的颜色。

cf780c的更多相关文章

  1. [CF780C]Andryusha and Colored Balloons 题解

    前言 完了,完了,咕值要没了,赶紧写题解QAQ. 题意简述 给相邻的三个节点颜色不能相同的树染色所需的最小颜色数. 题解 这道题目很显然可以用深搜. 考虑题目的限制,如果当前搜索到的点为u, 显然u的 ...

随机推荐

  1. logback配置每天生成一个日志文件,保存30天的日志文件

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 文件输出格 ...

  2. 64、具有过渡动画效果的布局Layout( 2 )

    [ CoordinatorLayout-与手势完美结合的滑动视图 ] [ AppBarLayout-可以随手势滑动的AppBar ] <android.support.design.widget ...

  3. JAVA环境变量配置备忘

    jdk1.6以上就不需要配置classpath了:系统会自动帮你配置好 选择“高级”选项卡,点击“环境变量”:在“系统变量”中,设置3项属性,JAVA_HOME,PATH,CLASSPATH(大小写无 ...

  4. 7.15;linux命令

    麦克维瀑布 https://farm5.staticflickr.com/4269/34749113172_d6c1ba274a_k.jpg ----------------------------- ...

  5. SQL server-自增主键

    1.CREATE   TABLE   表名(     字段名   [int]   IDENTITY   (1,   1)   NOT   NULL   ,    //--(seed = 1,incre ...

  6. QQ视频直播架构及原理 流畅与低延迟之间做平衡 音画如何做同步?

    QQ视频直播架构及原理 - tianyu的专栏 - CSDN博客 https://blog.csdn.net/wishfly/article/details/53035342 作者:王宇(腾讯音视频高 ...

  7. Android Studio "佛祖保佑 永无bug" 注释模板设置详解(仅供娱乐)

    1.注释模板效果图 今天在网上看到一段有趣的注释,佛祖保佑 永无bug, 效果如下图所示: 代码如下所示: /** * _ooOoo_ * o8888888o * 88" . "8 ...

  8. 玩玩nmap

    ---恢复内容开始--- [root@miyan ~]# nmap -v Starting Nmap 7.12 ( https://nmap.org ) at 2016-04-04 15:34 CST ...

  9. php foreach函数的用法

    php foreach函数用法举例.  Foreach 函数(PHP4/PHP5) foreach 语法结构提供了遍历数组的简单方式. foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类 ...

  10. nginx规则总结

    location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的 ...