code force 403C.C. Andryusha and Colored Balloons
2 seconds
256 megabytes
standard input
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!
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.
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.
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
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个节点,n-1条边,让你进行染色,要求是a-b,b-c三点的颜色不同,让你给出配色方案,配色从1开始 初步思路:从1点开始搜,每个节点的染色和父亲,祖父的颜色不相同
*/ #include<bits/stdc++.h>
using namespace std ;
#define pb push_back
int col=;//表示总染色的种类
int color[];
vector<int> edge[];
void dfs(int u,int fa){//父节点,祖父节点
int colv=;//将colv定义在循环外边,神来之笔,这样兄弟的颜色肯定是不会重复的了
for(int i=;i<edge[u].size();i++){//遍历儿子节点
int v=edge[u][i];
if(color[v]) continue;
// cout<<v<<endl;
colv++;
while(colv==color[u]||colv==color[fa]) colv++;
col=max(col,colv);
color[v]=colv;
dfs(v,u);
}
}
int n,u,v;
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<n-;i++){
scanf("%d%d",&u,&v);
edge[u].pb(v);
edge[v].pb(u);
}//建图
color[]=;
dfs(,);
printf("%d\n",col);
for(int i=;i<=n;i++){
printf(i==?"%d":" %d",color[i]);
}
printf("\n");
return ;
}
code force 403C.C. Andryusha and Colored Balloons的更多相关文章
- Codeforces 782C. Andryusha and Colored Balloons 搜索
C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...
- 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 ...
- codeforces781A Andryusha and Colored Balloons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- AC日记——Andryusha and Colored Balloons codeforces 780c
C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...
- C. Andryusha and Colored Balloons
C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...
- CodeForces - 780C Andryusha and Colored Balloons(dfs染色)
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, ...
- CF781A Andryusha and Colored Balloons
题意: Andryusha goes through a park each day. The squares and paths between them look boring to Andryu ...
- 782C. Andryusha and Colored Balloons DFS
Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...
- 【贪心】【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< ...
随机推荐
- Java 使用Axis实现WebService实例
在上一篇WebService实例中,基于jdk1.6以上的javax.jws 发布webservice接口.这篇博文则主要用eclipse/myeclipse 使用axis插件进行发布和调用WebSe ...
- 使用phpmailer插件发邮件失败提示:SMTP -> ERROR: Failed to connect to server: Connection timed out (110) smtp connect() failed;
一个邮件发送问题,整整弄了我一周时间,起因是这样的,之前弄的一个网站,需要在邮箱里面认证之后才可以注册成功.网站上线了差不多一年之后,客户突然跟我说,网站不能注册了,然后我就查看了一下代码. 发现报这 ...
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- How many Knight Placing? UVA - 11091
How many Knight Placing? Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- 【重点突破】——使用Express创建一个web服务器
一.引言 在自学node.js的过程中有一个非常重要的框架,那就是Express.它是一个基于NodeJs http模块而编写的高层模块,弥补http模块的繁琐和不方便,能够快速开发http服务器.这 ...
- [原创]KVM虚拟化管理平台的实现
KVM虚拟化管理平台的实现 源码链接:https://github.com/wsjhk/IaaS_admin.git 根据KVM虚拟化管理的要求,设计并实现网页操作管理KVM虚拟机.设计原理架构如下图 ...
- nginx配置文件作用介绍
######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...
- 对python编程的初步理解
一直以来零零散散有听过python,这周终于下定决心学python了.在网上了买个套视频教程,内容分周次学习,有详细的讲解.本人觉得非常好.这里谈谈一下第一周的学习的笔记.望路过的大神给予指正,不胜感 ...
- ASP.NET没有魔法——ASP.NET MVC & 分层
上一篇文章简要说明了MVC所代表的含义并提供了详细的项目及其控制器.视图等内容的创建步骤,最终完成了一个简单ASP.NET MVC程序. 注:MVC与ASP.NET MVC不相等,MVC是一种开发模式 ...
- cellForItemAtIndexPath没有调用
前几天碰到cellForItemAtIndexPath这个数据源方法没有被调用.这是一个collectionView返回cell(item)的数据源方法. 它没有被调用的原因有下: 1.没有设置del ...