[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个节点的颜色不能相同 的情况下进行染色 ...
随机推荐
- Solr 4.4.0安装
软件包准备 JDK http://download.oracle.com/otn/java/jdk/6u45-b06/jdk-6u45-linux-x64.bin Solr https://archi ...
- jira:恢复数据:AO_187CCC_SIDEBAR_LINK
JIRA 恢复数据时报错 ,关键词是找不到 AO_187CCC_SIDEBAR_LINK. 经网上查为 mysql connect jar 包 的版本过高所致. 降低版本后,成功导入数据.
- sqlserver with(nolock)而mysql 不需nolock
nolock 是 SQL Server 特有的功能. 例如:对于一个表 A,更新了一行,还没有commit,这时再select * from A 就会死锁.用select * from A(noloc ...
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- html之input
<input>在w3c中解释的是用于搜集用户的信息 它其实就是一个输入框,根据type的不同这个框可以用作不同的功能可以输入一个文本,可以定义一个按钮什么的. 属性type一些值的介绍 1 ...
- springBoot2.0配置profile
1. 使用yaml来配置,直接配置application.yml文件 server: port: 8888 spring: profiles: active: dev # 激活生产环境 --- # 测 ...
- svn下载项目的时候出现 Path to certificate
svn关联的时候出现这种情况,并且有svn的账号的时候,可以找setting中Version Control 中的Subversion中celar 一下即可,然后再重新下载就会让你重新输入用户名和密码 ...
- PythonDay14
第十四章装饰器 装饰器 # 开放封闭原则- 1.对扩展是开放的- 2.对修改是封闭的# 在不修改源代码和调用方式的情况下,对函数进行扩展# 第一版装饰器def times(func): def ...
- sql删除重复行和删除字段首位
删除重复行 user_info: -- 单字段筛选重复行 SELECT *from user_info WHERE NAME in ( SELECT NAME from user_info GROUP ...
- vue中如何开发插件
1.vue中提供了install方法用来开发插件 官方:Vue.js 的插件应该有一个公开方法 install.这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象. 2.我的插件目 ...