CodeForces - 780C Andryusha and Colored Balloons(dfs染色)
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
3
2 3
1 3
3
1 3 2
5
2 3
5 3
4 3
1 3
5
1 3 2 5 4
5
2 1
3 2
4 3
5 4
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,4,5,详细见代码
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
vector<int> mp[];
int n,x,y;
int vis[];
void dfs(int x,int y)
{
int cnt=;//上面已解释
for(int i=;i<mp[x].size();i++)
{
if(mp[x][i]==y) continue;
while(cnt==vis[x]||cnt==vis[y])//寻找可以赋值的颜色
cnt++;
vis[mp[x][i]]=cnt++;//给节点染色
}
for(int i=;i<mp[x].size();i++)//使用新的两个节点
{
if(mp[x][i]!=y)
dfs(mp[x][i],x);
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n-;i++)//领接表建图
{
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
vis[]=;
dfs(,);
int mx=vis[];
for(int i=;i<=n;i++)//取最大颜色
mx=max(mx,vis[i]);
printf("%d\n",mx);
for(int i=;i<=n;i++)
{
printf("%d ",vis[i]);
}
}
CodeForces - 780C Andryusha and Colored Balloons(dfs染色)的更多相关文章
- Codeforces 782C. Andryusha and Colored Balloons 搜索
C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...
- 782C. Andryusha and Colored Balloons DFS
Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...
- AC日记——Andryusha and Colored Balloons codeforces 780c
C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...
- 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 ...
- 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 ...
- codeforces781A Andryusha and Colored Balloons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- C. Andryusha and Colored Balloons
C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces 781A:Andryusha and Colored Balloons(DFS染色)
http://codeforces.com/contest/782/problem/C 题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色. 思路:比 ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
随机推荐
- ASA与N6K对接
ASA5545配置interface GigabitEthernet0/0 channel-group 10 mode active no nameif no security-level no ip ...
- C# wave mp3 播放器探寻
C# wave mp3 播放器探寻 最近无聊,想听听歌曲.可怜新电脑上歌曲就两三首,要听其它的就得在旧电脑上播放.可是,那台古董但不失健壮的本本被老婆无情的霸占了.无奈. 思来想去,得,写个程序播 ...
- 看到篇博文,用python pandas改写了下
看到篇博文,https://blog.csdn.net/young2415/article/details/82795688 需求是需要统计部门礼品数量,自己简单绘制了个表格,如下: 大意是,每个部门 ...
- Linux系统中文件定位与查找
Linux系统中文件查找 关键词 文件查找 | find | locate 本文主要介绍有关文件查找的两个命令——find和locate,以及压缩打包的命令——compress, gzip,bzip2 ...
- 如何在VMware系统中的ubuntu16.04中建立与win7系统的共享文件夹
点击虚拟机设置一次得到如图所示 系统默认放在了 /mnt/hgfs文件夹 点击虚拟机安装vmware tools 解压vmware tools压缩包 运行sudo ./vmware-install.p ...
- String.Format 格式化例子
//格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);替换符// Label2.Text ...
- cookie VS localstorage
http://jerryzou.com/posts/cookie-and-web-storage/ cookie: 1. 数据上限4KB左右 2. 一般由服务器生成,可设置失效时间.如果在浏览器端生成 ...
- Vue keep-alive总结
<keep-alive>是Vue的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM. <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是 ...
- Analytic Functions in Oracle
Contents Overview and IntroductionHow Analytic Functions WorkThe SyntaxExamplesCalculate a running T ...
- 数组Array.sort()排序的方法
数组sort排序 sort比较次数,sort用法,sort常用 描述 方法sort()将在原数组上对数组元素进行排序,即排序时不创建新的数组副本.如果调用方法sort()时没有使用参数,将按字母顺序( ...