[CF780C]Andryusha and Colored Balloons 题解
前言
完了,完了,咕值要没了,赶紧写题解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 题解的更多相关文章
- 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 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 ...
- 【codeforces 782C】Andryusha and Colored Balloons
[题目链接]:http://codeforces.com/contest/782/problem/C [题意] 给你一棵树 让你满足要求 ->任意相连的3个节点的颜色不能相同 的情况下进行染色 ...
随机推荐
- Qt - 基于HTTP的网络编程
HTTP(超文本传输协议 Hyper Text Transfer Protocol) 基于TCP/IP通信协议,属于应用层协议. 使用情况: HTTP是无连接(无连接的含义是限制每次连接只处理一个请求 ...
- 来自 Vue 3.0 的 Composition API 尝鲜
来自 Vue 3.0 的 Composition API 尝鲜:https://segmentfault.com/a/1190000020205747
- pair常见用法
pair的使用 关于pair 什么是pair 可以将pair看做一个内部有两个元素的结构体,且两个元素的类型是可以指定的. struct pair{ typename1 first; typename ...
- redis 小结 一
1.redis 是什么? 它是一个key-value存储系统,也被称为数据结构服务器,它的值是字符串(String),哈希(Hash),列表(list),集合(sets)和有序集合(sorted se ...
- Codeforces 1220B. Multiplication Table
传送门 冷静分析容易发现,我们只要能确定一个数的值,所有值也就可以确定了 确定一个数的值很容易,$a_ia_j=M_{i,j},a_ia_k=M_{i,k},a_ja_k=M_{j,k}$ 然后就可以 ...
- 使用Servlet实现验证码
没有验证码带来的问题 对特定用户不断登录破解密码. 对某个网站创建账户. 对某个网站提交垃圾数据. 对某个网站刷票. 通过验证码由用户肉眼识别其中的验证码信息,从而区分用户是人还是计算机. 定义: ...
- openapi
https://www.breakyizhan.com/swagger/2810.html https://www.cnblogs.com/serious1529/p/9318115.html htt ...
- java并发编程:线程同步和锁
一.锁的原理 java中每个对象都有一个内置锁.当程序运行到非静态的synchronized同步方法上时,自动获得与正在执行代码类的当前实例(this)有关的锁.获得一个对象的锁也称为获取锁,当程序运 ...
- python-ssh-远程服务器+远程docker执行命令
在python语言中实现远程服务器执行命令+远程dcoker执行命令 def ssh_exec_command(ip, username, password, cmd=None): "&qu ...
- 查看 MySQL 数据库的编译参数
grep CONFIGURE_LINE /app/mysql/bin/mysqlbug 提示:还发现很多人先 cat,在 grep,很不专业,应杜绝. 范例 3: [root@VM-001~]# gr ...