前言

完了,完了,咕值要没了,赶紧写题解QAQ。

题意简述

给相邻的三个节点颜色不能相同的树染色所需的最小颜色数。

题解

这道题目很显然可以用深搜。

考虑题目的限制,如果当前搜索到的点为u,

显然u的父亲节点,u本身和u的所有儿子不能同色(因为兄弟之间相差为2)。

由于DFS解题,所以搜索到u时,u本身和u的父亲肯定已经有颜色了,那么现在要结局的使u的所有儿子的颜色,那么根据贪心的思想对于每个u的儿子,颜色能往小取就往小取,在选取颜色的时候应当注意,选取的颜色不能与u和u的父亲节点的颜色相同。

最后的答案就是DFS中使用过的最大的颜色。

代码

去掉fread快读其实很短

#include <cstdio>
#include <algorithm> using namespace std; namespace fast_IO{
const int IN_LEN = 10000000, OUT_LEN = 10000000;
char ibuf[IN_LEN], obuf[OUT_LEN], *ih = ibuf + IN_LEN, *oh = obuf, *lastin = ibuf + IN_LEN, *lastout = obuf + OUT_LEN - 1;
inline char getchar_(){return (ih == lastin) && (lastin = (ih = ibuf) + fread(ibuf, 1, IN_LEN, stdin), ih == lastin) ? EOF : *ih++;}
inline void putchar_(const char x){if(oh == lastout) fwrite(obuf, 1, oh - obuf, stdout), oh = obuf; *oh ++= x;}
inline void flush(){fwrite(obuf, 1, oh - obuf, stdout);}
int read(){
int x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar_();
if (ch == '-') zf = -1, ch = getchar_();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar_(); return x * zf;
}
void write(int x){
if (x < 0) putchar_('-'), x = -x;
if (x > 9) write(x / 10);
putchar_(x % 10 + '0');
}
} using namespace fast_IO; struct Edge{
int to, next;
} edges[400005]; int head[200005], edge_num = 0; inline void addEdge(int u, int v){
edges[++edge_num] = (Edge){v, head[u]};
head[u] = edge_num;
} int clr[200005];
int ans = 0; void DFS(int u, int fa){
int v, cnt = 1;
for (int c_e = head[u]; c_e; c_e = edges[c_e].next){
v = edges[c_e].to;
if (v != fa){
while (cnt == clr[u] || cnt == clr[fa])
++cnt;
clr[v] = cnt++;
DFS(v, u);
}
}
ans = max(ans, cnt - 1);
} int main(){
int n = read();
for (int i = 1; i < n; ++i){
int u = read(), v = read();
addEdge(u, v), addEdge(v, u);
}
clr[1] = 1; DFS(1, 1);
write(ans); putchar_('\n');
for (int i = 1; i <= n; ++i)
write(clr[i]), putchar_(' ');
flush(); return 0;
}

[CF780C]Andryusha and Colored Balloons 题解的更多相关文章

  1. 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 ...

  2. Codeforces 782C. Andryusha and Colored Balloons 搜索

    C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...

  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. codeforces781A Andryusha and Colored Balloons

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. AC日记——Andryusha and Colored Balloons codeforces 780c

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

  6. C. Andryusha and Colored Balloons

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

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

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

  8. CF781A Andryusha and Colored Balloons

    题意: Andryusha goes through a park each day. The squares and paths between them look boring to Andryu ...

  9. 【codeforces 782C】Andryusha and Colored Balloons

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

随机推荐

  1. CF486B OR in Matrix(构造+思维)

    CF486B 一道有趣的思维题 由于or的性质可知只要a[i][j]为1那么b中第i行,第j列将都变成1 相反的,如果b[i][j]是0那么a中第i行,第j列都必须是0 根据第二个性质我们可以构造出a ...

  2. [转帖]「白帽黑客成长记」Windows提权基本原理(上)

    「白帽黑客成长记」Windows提权基本原理(上) https://www.cnblogs.com/ichunqiu/p/10949592.html 我们通常认为配置得当的Windows是安全的,事实 ...

  3. win10的64位操作系统安装Oracle、Sql数据库遇到的问题及解决

    因为工作需要,在重新安装操作系统(Win10)不久的电脑上安装Oracle的客户端(win32_11g)和PLSQL,这个本来就比较复杂,下面记录一下遇到的问题及解决方法. 我有储备多个Oracle的 ...

  4. 【6.12校内test】T1单词序列

    [问题描述] 给出两个单词(开始单词和结束单词)以及一个词典.找出从开始单词转换到结束单词, 所需要的最短转换序列.转换的规则如下: 1.每次只能改变一个字母 2.转换过程中出现的单词(除开始单词和结 ...

  5. BZOJ 1053 反素数 题解

    题面 引理1:  1~n中的最大反质数,就是1~n中约数个数最多的数中最小的一个(因为要严格保证g(x)>g(i)): 引理2:1~n中任何数的不同因子不会超过10个,因为他们的乘积大于2,00 ...

  6. 部署 laravel项目404错误

    1.nginx 下部署出现404错误 (1)打开php.ini中的php_openssl.dll这个扩展: (2)修改nginx 下的站点目录配置文件(我的是配置在vhost.conf)为: loca ...

  7. WAMP中的mysql设置密码

    为WAMP中的mysql设置密码密码 WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 1.首先,通过WAMP打开mysql控制台. ...

  8. 推荐几本Python书

    Python的书很多,由于python本身应用的领域太多,涉及方方面面的,因此书籍的种类也很多,下面是我推荐一些比较好的python书给大家,大家可以找一两本修炼,定能让你的功力大增. 1.A byt ...

  9. python-day15(正式学习)

    目录 递归 函数自我嵌套 调用 直接调用 间接调用 为什么要用递归呢 如何使用递归 内置函数 掌握 了解 面向对象方法 面向过程编程 注册 分层实现功能 递归 递归的本质就是函数调用自身,当然也会有一 ...

  10. PythonDay09

    第九章函数 今日内容 函数定义 函数调用 函数返回值 函数的参数 函数定义 # 通过定义一个计算数据长度的函数,def为关键字,count_len是函数名def count_len(): lst = ...