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 a, b 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个节点的颜色不能一样。输出使用颜色种类最少的结果。
思路:dfs搜索。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
const int MAXN=2e5+,INF=0x3f3f3f3f,MOD=1e9+;
int n,col[MAXN];
map<int,vector<int> >G;
int ans;
dfs(int now,int sign,int fa,int flag)
{
//cout<<now<<" "<<sign<<" "<<fa<<" "<<flag<<endl;
int tmp=;
for(int i=; i<G[now].size(); i++)
{
int to=G[now][i];
if(to==fa) continue;
while(++tmp)
{
if(tmp!=sign&&tmp!=flag)
{
col[to]=tmp;
if(tmp>ans) ans=tmp;
dfs(to,tmp,now,sign);
break;
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
col[]=;
ans=;
dfs(,,-,-);
cout<<ans<<endl;
for(int i=; i<=n; i++) cout<<col[i]<<" ";
cout<<endl;
return ;
}

搜索

Codeforces 782C. Andryusha and Colored Balloons 搜索的更多相关文章

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

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

  2. 782C. Andryusha and Colored Balloons DFS

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

  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. AC日记——Andryusha and Colored Balloons codeforces 780c

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

  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 782C】Andryusha and Colored Balloons

    [题目链接]:http://codeforces.com/contest/782/problem/C [题意] 给你一棵树 让你满足要求 ->任意相连的3个节点的颜色不能相同 的情况下进行染色 ...

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

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

随机推荐

  1. WEB常用前端开发调试工具介绍

    只要是设计开发,就需要进行调试,尽管相对来说,前端的调试要简单一些,但使用一些调试工具或插件还是能提高你的工作效率.下面是一些主要用于IE浏览器环境和Firefox浏览器环境等的调试工具简介. 一.I ...

  2. 为什么要用MarkDown?

    [为什么要用MarkDown?] 大部分作家用 Word 或 Pages 写作,过去的文档也大都以 .doc, .docx 格式或是 Pages 格式储存.还有人为了保证文稿发给谁都能正常打开,会用 ...

  3. pecan API调用

    1.在PasteDeploy部署caesar-api服务时,setup_app中建立app,app中设置hooks,hooks负责加载conf配置文件和数据库连接 self.storage_conne ...

  4. hibernate mysql视图操作

    hibernate对视图操作,首先建立数据库视图 视图v_invite: create view pintu.v_invite asselect cp.user_id as be_user_id,ca ...

  5. STL::map/multimap

    map: 默认根据 key 排序(从小到大),能够通过 backet operator(operator [ ]) 来获取元素,内部由二叉搜索树来实现(binary search trees). mu ...

  6. pta l1-49(天梯赛座位分配)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805081289900032 题意:给定n个高校,每个高校m[i ...

  7. .net 报错access to the path c:\tempimagefiles\msc_cntr_0.txt is denied

    报错信息: 解决方法: 在 Web.Config 的 <System.Web> 里加 <identity impersonate="true"/> 节点即可 ...

  8. unity的inputField文本框赋值问题

    GameObject t = GameObject.Find("InputFieldT"); Text tt = t.transform.Find("Text" ...

  9. VCSA 6.5, 初始化设置root密码失败can't set root password 或 安装时卡在80%

    是因为下载的非官方的包密码过期了,如果是Windows引导安装: 安装完虚拟机的时候就马上执行下面步骤,修改密码有效期即可. 重启vcsa,在引导界面(photon的图形界面)里按e编辑启动项 在co ...

  10. 破解myeclipse 2014

    用网上的教程的确可以,但是他似乎写的有点少.....试了很多次,说说他少的: http://jingyan.baidu.com/article/fdbd42771039bfb89e3f4838.htm ...