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. linux机器之间配置ssh无密访问

    首先确认已安装了ssh服务,没装的自行百度一下. A机器:192.168.1.1 B机器:192.168.1.2 使A无密访问B,步骤如下[root@localhost ~]# cd .ssh 如果没 ...

  2. 数据库读写分离(aop方式完整实现)

    http://blog.csdn.net/machunlin2010/article/details/46471983

  3. JZOJ.3769【NOI2015模拟8.14】A+B

    Description 对于每个数字x,我们总可以把它表示成一些斐波拉切数字之和,比如8 = 5 + 3,  而22 = 21 + 1,因此我们可以写成  x = a1 * Fib1 + a2 * F ...

  4. 初识yeoman

    最近开始新项目,在项目构建上面寻找合适的东西,grunt,bower到发现yeoman;学习了下,把一些东西记录下来然留着以后用. 1.什么是Yeoman Yeoman是Google的团队和外部贡献者 ...

  5. struts2的配置文件为什么可以使用${}符号?

    转自:https://www.cnblogs.com/sharpest/p/6030265.html 一.#符号的用途一般有三种. “#”主要有三种用途: 1. 访问OGNL上下文和Action上下文 ...

  6. svn 插件去除已经保存的密码方法

    删除掉C:\Documents and Settings\hao\Application Data\Subversion\auth\svn.simple文件夹下的文件即可. 再次访问时,会弹出要求输入 ...

  7. Commit message 的写法规范。本文介绍Angular 规范(

    Commit message 的写法规范.本文介绍Angular 规范( http://www.ruanyifeng.com/blog/2016/01/commit_message_change_lo ...

  8. Java IO异常处理方式

    public class IOException{ // 获取系统默认的行分隔符 private static final String LINE_SEPARATOR = System.getProp ...

  9. Compilation failed: internal java compiler error

    在Idea中编译时出现这个错误:Error:java: Compilation failed: internal java compiler error. Information:Using java ...

  10. Python生成器是什么

    生成器是 Python 初级开发者最难理解的概念之一,虽被认为是 Python 编程中的高级技能,但在各种项目中可以随处见到生成器的身影,你得去理解它.使用它.甚至爱上它. 提到生成器,总不可避免地要 ...