Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons dfs(树)

使用Eggjs做服务端获取微信小程序云开发数据库的access_token 最近自己想写了一个微信小程序,恰巧里面有个云开发,恰巧里面有个数据库,那懒人的我,就直接用云开发里面的数据库了,做测试或者正式也好,对于刚开始来说,免费的配额是够用的。 后来想到在小程序云开发上面的数据怎么看才方便?刚好微信小程序有一个服务端API,那就方便很多了,那就直接敲个服务端来获取云开发上数据库的数据出来到自己的后台上面,那以后管理起来也方便很多。 看了一下服务端api之后,发现有三个重要... 阅读详情

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

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

The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.

Andryusha wants to use as little different colors as possible. Help him to choose the colors!

Input

The first line contains single integer n (3 ≤ n ≤ 2·105) — the number of squares in the park.

Each of the next (n - 1) lines contains two integers x and y (1 ≤ x, y ≤ n) — the indices of two squares directly connected by a path.

It is guaranteed that any square is reachable from any other using the paths.

Output

In the first line print single integer k — the minimum number of colors Andryusha has to use.

In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.

Examples
input
3
2 3
1 3
output
3
1 3 2 
input
5
2 3
5 3
4 3
1 3
output
5
1 3 2 5 4 
input
5
2 1
3 2
4 3
5 4
output
3
1 2 3 1 2 
Note

In the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.

Illustration for the first sample.

In the second example there are following triples of consequently connected squares:

  • 1 → 3 → 2
  • 1 → 3 → 4
  • 1 → 3 → 5
  • 2 → 3 → 4
  • 2 → 3 → 5
  • 4 → 3 → 5
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct.
Illustration for the second sample.

In the third example there are following triples:

  • 1 → 2 → 3
  • 2 → 3 → 4
  • 3 → 4 → 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only.
Illustration for the third sample.
由于只有n-1条边,且图连通,所以一定是一个树

dfs遍历树,形参记录前一个节点和当前节点

对每个点,搜索他连接的且没走的点,因为是树,所以时间很低

对于染色,只要保证每个点和他的父亲,父亲的父亲以及兄弟不同色即可,并且对于一棵树,树上的任何一点都可以做根节点,所以从哪个点开始搜都可以

对于有多少种颜色,可以求完染色数组,统计标记一下求得,也可以记录每个点的度,找到最大再加一,因为只要保证对于每个点所有连的边以及他本身这个点所有颜色都各不相同即可,也就是最大度数加上自己

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define eps 1e-9
#define PI 3.141592653589793
#define bs 1000000007
#define bsize 256
#define MEM(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
vector<int>w[200005];//存图
int ans[200005];//染色数组
void dfs(int zi,int node)
{
	int i,j=1;
	for(i=0;i<w[node].size();i++)
		if(!ans[w[node][i]])
		{
			while(j==ans[zi]||j==ans[node])j++;
			ans[w[node][i]]=j++;//j++作为下一个兄弟的染色初值
			dfs(node,w[node][i]);			
		}
	return;
}
int main()
{
	int n,x,y,i;
	int book[200005];
	MEM(book);
	scanf("%d",&n);
	for(i=0;i<n-1;i++)
	{
		scanf("%d %d",&x,&y);
		w[x].push_back(y);
		w[y].push_back(x);
		book[x]++;
		book[y]++;	
	}
	int maxx=0;
	for(i=1;i<=n;i++)
	maxx=max(maxx,book[i]);
	ans[1]=1;
	dfs(0,1);
	printf("%d\n",maxx+1);
	for(i=1;i<=n;i++)
	printf("%d ",ans[i]);
	return 0;
	
 }







告别社区5级:一个脚本搞定小米/红米BL解锁的账号绑定限制(附资源下载与避坑指南) 本文提供了一种绕过小米/红米设备Bootloader解锁社区等级限制的技术方案,适用于MIUI 12.5-14和HyperOS 1.0系统。通过自动化脚本临时修改系统组件,用户可快速完成账号绑定和设备解锁流程,附详细操作指南和常见问题解决方案。 阅读详情

相关推荐

保姆级教程:用DaVinci Configurator & Developer从零搭建AUTOSAR OS(TC2xx三核实战)

本文提供了一份详细的AUTOSAR OS配置指南,使用DaVinci Configurator和Developer工具从零开始搭建Infineon TC2xx三核系统。涵盖多核配置协同逻辑、版本兼容性陷阱及验证错误的解决方案,帮助开发者在3小时内构建可运行的多核OS框架。

weixin_33940469的博客 315

Codeforces Round #403 C. Andryusha and Colored Balloons

题目链接:点击打开链接 题意:给出n个点,(n-1)条无向边,问能否将点染色使所有点与其邻接的点以及距离为1的点的颜色不同(如:a与b存在一边,b与c存在一边,则a,b,c三点颜色应不同) 题解:将图看做一棵树,对于一个节点的子节点我们先将选一个节点进行染色,染色的颜色选择则是从1开始不断++一直到与其父节点以及父节点的父节点颜色不同为止,然后再对这个子节点进行进一步深搜,因为

t51645的博客 312

从原理图到代码:深入解析SX1278 LoRa模块与STM32的SPI通信驱动(附完整工程)

本文深入解析了SX1278 LoRa模块与STM32的SPI通信驱动实现,从硬件原理图到完整工程代码,详细介绍了LoRa技术的通信原理、寄存器配置、SPI接口设计及低功耗优化策略。适合物联网开发者和嵌入式工程师提升远距离无线通信能力,附完整工程代码助力快速开发。

weixin_42715608的博客 424

#树上DFS# Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons

Codeforces Round #403 C Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them. The park consists of n squares connected

FFOYeah的博客 301

STM32与K210串口通信

这篇文章是为了填上一篇埋下的坑,k210和STM32通过串口通信,我是采用数据包的形式发送数据的,因为K210发送的数据有几种,采用数据包的形式发送比较安全。本文代码运行的平台是STM32F103C8T6和K210 Dock,只涉及到K210发送数据给STM32,stm32将接受到的数据显示到0.96寸OLED屏上。

qq_70715693的博客 3万+

一文梳理金融风控建模全流程(Python)

。借助A卡模型,银行或金融机构能够根据申请人的个人信息和历史数据,对其进行风险评估。这种评估能够帮助金融机构判断借款人是否具备偿还贷款的能力和意愿。

weixin_43420291的博客 4776

Codeforces Round #403 div2 C. Andryusha and Colored Balloons

题目链接:Codeforces Round #403 div2 C. Andryusha and Colored Balloons 题意: 给你一棵n个节点的树,然后让你染色,规定相连的三个 节点不能同色,问需要的最少颜色,并输出其中一种方案。 题解: 因为只有相邻3个节点不同色。 所以直接DFS,每个节点都从1开始。 然后ans[v]!=ans[u]!=ans[fa]就行。 ...

weixin_30786657的博客 107

半导体关键系统之RMS浅谈

这种系统还要求设备在设备上的配方发生变化时通知工厂主机,并需要与EAP(Equipment Automation Process)联结,旨在实时管控自动化系统所用配方与用户当初的设定是否匹配,从而使得自动化系统所用的配方更加趋于安全。例如,制药行业中的药品生产管理系统(GMP),它对药品的生产过程进行严格控制,包括原材料的采购、生产工艺流程、质量控制等方面。这些系统通常与企业的生产系统集成,能够对产品的配方进行精细化管理,包括配方的设计、试验、验证、生产等环节。

阿拉伯梳子的专栏 6158

Codeforces Round #403 (Div. 2)C Andryusha and Colored Balloons (dfs)

C. Andryusha and Colored Balloonstime limit per test:2 secondsmemory limit per test:256 megabytesinput:standard inputoutput:standard outputAndryusha goes through a park each day. The squares and paths

妍恐龙的博客....^~^眼熟我 526

48、住宅抵押贷款支持证券的定价、风险评估与结构化设计

本文深入探讨了住宅抵押贷款支持证券(RMBS)的定价、风险评估与结构化设计。内容涵盖提高定价准确性的方差缩减方法,如准蒙特卡罗序列的应用;分析RMBS面临的利率风险、信用风险和模型风险,并提出相应的度量与应对策略;介绍利用动态规划优化CMO分层结构以最大化发行利润的方法。同时总结了RMBS的基本类型、定价流程及市场发展趋势,为投资者和发行人提供全面的决策支持。

quiet的专栏 154

Linux LT8912b调试记录(MIPI转LVDS/HDMI)

本文记录了在RK3588平台上使用LT8912B芯片实现MIPI转LVDS/HDMI的调试过程。主要包含设备树配置、驱动代码实现及常见问题解决。调试重点包括:1)正确配置1280x800分辨率的LVDS屏幕参数;2)确保I2C总线正常通信;3)解决花屏、图像抖动、黑屏等问题,涉及lane-rate计算、同步信号极性调整及MIPI模式选择。文中提供了详细的寄存器配置代码和故障排查方法,特别强调了I2C通信验证和电压匹配的重要性。通过参数优化和多种模式测试,最终实现了稳定的视频输出转换。

qq_45147279的博客 1088

Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons

C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Andryusha goes through a

catTom的博客 1071

计算机组成原理(第三版)唐朔飞-第七章指令系统-课后习题

机器字长是指CPU一次能处理数据的位数通常与CPU的寄存器位数有关。指令字长是指机器指令中二进制代码的总位数。存储字长是指存储单元中存放二进制代码的总位数。三者可以相等也可以不等视不同机器而定。答:OP(4位)A1(6位)A_1(6位)A1​(6位)A2(6位)A_2(6位)A2​(6位)设二地址指令格式为该指令系统的基本格式,4位操作码共有16种编码,其中13种用来定义二地址指令,还剩3种可用作扩展标志。一地址指令条数=3∗26=192。

weixin_45735391的博客 7万+

Codeforces Round #403 C. Andryusha and Colored Balloons(DFS)

C. Andryusha and Colored BalloonsDescriptionAndryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.The park consists of n sq

DorMOUSENone的博客 667

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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 test 2 seconds memory limit per test 256 megabytes input standard input output s...

weixin_30904593的博客 177

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)   说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放学我呆到十点然后还要跑回家耽误时间....要不然$D$题就写完了 周末一些成绩好的同学单独在艺术楼上课然后晚上下第一节晚自习和他们在回廊里玩开灯之后再关上一片漆黑真好玩   A...

weixin_34357928的博客 117

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Codeforces Round #403 (Div. 2 C. Andryusha and Colored Balloons ( 树上染色问题

C. Andryusha and Colored Balloons 题目描述 Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them. The park consists of n...

Yishui_lovely 279

Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons(DFS 邻接表 数据结构)

C. Andryusha and Colored Balloons time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Andryusha goes through a park each day. The squares and

Prim 458

Codeforces Round #382 (Div. 2)A到D题 A. Ostap and //B. Urbanization//C. Tennis Championship//D. Taxes

A. Ostap and Grasshopper time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output On the way to Rio de Janeiro Ostap

jerans的博客 1287

Gym - 101128C Canvas Painting 逆向思维

https://odzkskevi.qnssl.com/3699857ff0a17d77d1099699cdf4da13?v=1490503337 刚开始读错题意,以为是不变序列,就想着区间dp但数据量肯定会t,,,没想到什么优化方法就放下了,后来队员发现序列是可变的。。。。任意的。。。。。 想着先排序在布拉布拉布拉,一直都没有逆向思维想到哈夫曼。只怪数据结构没认真听啊。。。。 想到哈夫

jerans的博客 945

CodeForces - 361D Levko and Array二分加dp

D. Levko and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Levko has an array that consists of inte

jerans的博客 840

Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C Fountains 乱搞

C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Arkady plays Gardenscapes a lot. Arkady wants

jerans的博客 810

codeforces 8VC Venture Cup 2017 - Elimination Round D PolandBall and Polygon

D. PolandBall and Polygon time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output PolandBall has such a convex poly

jerans的博客 726

B. The Meeting Place Cannot Be Changed

B. The Meeting Place Cannot Be Changed time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output The main road in Byt

jerans的博客 695
上一篇: Bishops Alliance
下一篇: B. The Meeting Place Cannot Be Changed
jerans
博客等级 码龄10年 54粉丝 255原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值