782C. Andryusha and Colored Balloons DFS
题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同
思路: 直接DFS。由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结点的颜色不同,只需考虑当前节点是否与父亲结点和父亲的父亲结点颜色是否相同。
/** @Date : 2017-05-09 22:37:25
* @FileName: 782C DFS.cpp
* @Platform: Windows
* @Author : Lweleth (SoundEarlf@gmail.com)
* @Link : https://github.com/Lweleth
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; vectorvt[2*N];
int ans[2*N];
int n, ma; int dfs(int x, int pre)
{
int l = vt[x].size();
int c = 0;
for(int i = 0; i < l; i++)
{
int nw = vt[x][i];
if(ans[nw] || nw == pre)
continue;
c++;
while(c == ans[pre] || c == ans[x])
c++;
ans[nw] = c;
ma = max(c, ma);
dfs(nw, x);
}
} int main()
{
while(cin >> n)
{
MMF(ans);
for(int i = 0; i <= n; i++) vt[i].clear();
for(int i = 0; i < n - 1; i++)
{
int x, y;
scanf("%d%d", &x, &y);
vt[x].PB(y);
vt[y].PB(x);
}
ma = 0;
ans[1] = 1;
dfs(1, 1);
printf("%d\n", ma);
for(int i = 1; i <= n; i++)
printf("%d%s", ans[i], i==n?"\n":" ");
}
return 0;
}
782C. 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 ...
- 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 ...
- 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, ...
- 【贪心】【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< ...
- 【codeforces 782C】Andryusha and Colored Balloons
[题目链接]:http://codeforces.com/contest/782/problem/C [题意] 给你一棵树 让你满足要求 ->任意相连的3个节点的颜色不能相同 的情况下进行染色 ...
随机推荐
- “我爱淘”第二冲刺阶段Scrum站立会议4
完成任务: 完成了首页中的推荐功能,推荐的是最近添加的需要卖的书,注册功能实现了它,可以对数据库进行添加. 计划任务: 在客户端实现分类功能,通过学院的分类查看书籍. 遇到问题: 分类功能,根据不同学 ...
- mysql hql异常
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested ex ...
- 我是IT小小鸟(读后感)
序 1.兴趣,这本书第一个点讲兴趣,可是在中国填鸭式的教育下,有兴趣也被这种教育给泯灭了. 2.他山之石,可以攻玉.但不可照搬.这点我非常赞同作者的看法.别人东西你拿来,一定要在他的基础上进行创 ...
- 《我是一只IT小小鸟》心得
虽然读这本书是老师布置的作业,但是读了几页后就被书中的内容所吸引住了.或许是因为我也是学这个专业的,所以书中的一些内容让我觉得非常的有兴趣.作为一个学习软件工程的大一学生还没真正的认识到这个专业的深奥 ...
- erlang中检查内存泄露
最近项目内存占用过多,检查一下erlang的内存使用情况. 1. 通过etop可以很方便得出erlang内存使用的情况 spawn(fun() -> etop:start([{output, t ...
- 新手必备!11个强大的 Visual Studio 调试技巧
简介 调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总 ...
- 第202天:js---原型与原型链终极详解
一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object .Function 是 JS 自带的函数对象.下面举例说明 var o1 = ...
- grub引导启动 win10 Ubantu 凤凰OS 三系统
在Ubantu OS下,用文件管理器打开系统磁盘下的 boot文件夹,然后用管理员身份打开grub文件夹,然后打开grub.cfg(用记事本打开) 4. 在grub.cfg文件里面找到下一段内容(比较 ...
- Day19内容回顾
1,Django请求的生命周期 路由系统-视图函数(获取模板+数据->渲染)->字符串返回给用户 2,路由系统 /index/ 函数或类.as_view() /detail(\d+)/ 函 ...
- [AT2363] [agc012_c] Tautonym Puzzle
题目链接 AtCoder:https://agc012.contest.atcoder.jp/tasks/agc012_c 洛谷:https://www.luogu.org/problemnew/sh ...