前言

完了,完了,咕值要没了,赶紧写题解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. hdu3829 二分匹配 最大独立集

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Problem ...

  2. MySQL总结(5)

    视图 SELECT cust_name,cust_contact FROM customers,orders,orderitems WHERE customers.cust_id=orders.cus ...

  3. 深入理解let和var的区别

    首先我们应该知道js引擎在读取js代码时会进行两个步骤: 第一个步骤是解释. 第二个步骤是执行. 所谓解释就是会先通篇扫描所有的Js代码,然后把所有声明提升到顶端,第二步是执行,执行就是操作一类的. ...

  4. 常见SMT极性元器件识别方法

    极性元件在整个PCBA加工过程中需要特别注意,因为方向性的元件错误会导致批量性事故和整块PCBA板的失效,因此工程及生产人员了解SMT极性元件极为重要. 1.片式电阻(Resistor)无极性 2.电 ...

  5. Centos7 配置LAMP+fastcgi(Centos7.2+php7.0+mariadb+httpd)

    环境:阿里云centos7.3 一.安装并配置数据库 1.安装数据库 #yum install mariadb-server  mariadb -y 2.启动服务并设置开机自启 # systemctl ...

  6. EasyTest-接口自动化测试平台部署上线问题记录

    平台url:   http://easytest.xyz 花巨资搞了个阿里云服务器,哈哈,有想体验指导的大佬私聊我~~~ 部署环境 云服务器:Ubuntu Server 16.04.1 LTS 64位 ...

  7. SVM支持向量机(2)

    零.各种概念 1.硬间隔最大化   2.硬间隔支持向量机 3.线性支持向量机 软间隔最大化 软间隔支持向量机 4.非线性支持向量机 核函数kernel function 一.理解支持向量机SVM的原理 ...

  8. css实现div水平垂直居中

    中秋快到了,祝大家中秋快乐. 平时大家写bug过程中肯定会遇到让div框水平或者垂直居中,然而有时候能居中,有时候不能居中.我把平时遇到的一些方法写出来,如果对你有用,那便是晴天. 1.text-al ...

  9. 记录-- vue+element树节点的标注

    html(背景:状态标注3钟颜色红黄绿对应0,1,2,) <el-tree @check="slclasscheck" v-if="treeShow" : ...

  10. 基于Apache服务在centos7上搭建文件列表

    参考文献: https://www.cnblogs.com/snake553/p/8856729.html https://blog.csdn.net/yejinxiong001/article/de ...