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

UDS自动化测试-Service 0x27(CAPL调用dll实现key计算) 相信读者基于Diagnostic/ISO TP Confighration中cdd、dll文件的测试并不陌生,本文基于其的基础上,介绍在capl中调用内置的函数diagGenerateKeyFromSeed实现27服务解锁,从而进行诊断自动化测试。本文基于4个byte的Seed、16个byte的Key的安全访问,实现27服务的自动化测试。需要加载项目使用的cdd文件,如果没有的情况下可以使用canoe例程中默认的cdd文件。 阅读详情

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;
	
 }







软件可靠性测试与评价 本文系统介绍了软件可靠性工程的核心内容,包括可靠性概念、模型分类、测试方法和评价体系。重点解析了可靠性测试的三大步骤:定义运行剖面、设计测试用例和实施测试收集数据,并以智能安防系统为例说明具体应用。可靠性评价需结合业务场景,通过量化指标(MTBF、失效率等)和模型预测持续优化系统。文章强调可靠性需贯穿软件全生命周期,通过闭环反馈实现持续改进,为关键系统提供质量保障。 阅读详情

相关推荐

VisionPro脚本实战:用C#替代QuickBuild,让复杂定位项目代码更清爽

本文探讨了如何通过C#脚本重构VisionPro复杂定位项目,替代臃肿的QuickBuild架构。通过实战案例展示了脚本化开发如何减少60%代码量并提升35%运行效率,使项目更易维护。文章详细介绍了环境搭建、多模板定位重构及高级技巧,帮助开发者突破QuickBuild限制,实现更高效的视觉应用开发。

weixin_30417487的博客 508

Codeforces Round #403 C. Andryusha and Colored Balloons

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

t51645的博客 312

【分布式系统学习】CAP原理详解

概述:互联网最重要任务莫过于海量数据处理,即大规模分布式系统,分布式是互联网的核心技术!!!如上所述,分布式系统是无法同时满足CAP三个特性的。1.Atomicity(原子性)一个事务中的所有操作,要么全部完成,要么全部不完成,不会在中间某个环节结束。事务在执行过程中发生错误,会被回滚到事务开始前的状态,就像这个事务从来没有执行过一样。2.Consistency(一致性)在事务开始之前和事务结束以后,数据库的完整性没有被破坏。3.Isolation(隔离性)

大龄烤红薯的博客 5027

#树上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

第三方系统(异构系统)基于token身份认证方式访问Ecology9的Restful接口Ecology9服务器配置部分

通过Token的认证方式调用Ecology9系统Restful业务接口服务器配置部分

svsou的专栏 2183

实测对比:DINOv2、CLIP、ResNet在图像相似度任务上谁更强?(含Python代码与避坑指南)

本文通过实测对比DINOv2、CLIP和ResNet在图像相似度任务中的表现,提供Python代码实现与避坑指南。实验结果显示,DINOv2在精度上表现最佳,CLIP在多模态场景中优势明显,而ResNet则在计算效率上更优。文章还针对不同业务场景给出了具体选型建议,帮助开发者在图像搜索和版权检测等项目中做出更明智的技术决策。

weixin_30588381的博客 295

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的博客 109

比亚迪唐/秦开发者必看:253个车辆接口实战指南(附常用功能调用示例)

本文为比亚迪唐/秦开发者提供了一份详尽的车辆接口实战指南。文章聚焦于比亚迪开放的253个车辆接口,从开发环境搭建、核心状态监控到高频控制功能(如空调与充电管理),结合真实代码示例,帮助开发者快速上手并规避常见问题,高效开发车载应用。

weixin_28327051的博客 622

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

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

几率大的网络安全面试题(含答案)

其他篇章:Java校招极大几率出的面试题(含答案)----汇总 本文面试题汇总: 防范常见的 Web 攻击 重要协议分布层 arp协议的工作原理 rip协议是什么?rip的工作原理 什么是RARP?工作原理 OSPF协议?OSPF的工作原理 TCP与UDP区别总结 什么是三次握手四次挥手? tcp为什么要三次握手? dns是什么?dns的工作原理 一次完整的HTTP请求过程 Cookies和ses...

睶 6万+

S7-1200控制S120变频器

②:硬件目录下,添加实物S120设备进行组态(其它现场设备--PROFINET IO--DRIVES......②:双击S120进行IP地址和设备名称设置(注:此处要与STATER软件设置S120一致)M10.0接通时,电机开始运行,M10.0断开,电机停止,调试完成。①:将S7-1200与S120进行通讯连线。④:STATER软件下设置与组态一致的报文。①:点击添加新设备,按实物进行设备组态。②:打开OB1,编写启动停止程序。①:查看S120报文地址。②:模块下添加DO矢量。③:子模块下添加报文。

weixin_38261173的博客 4874

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的博客 1073

Google Earth Engine(GEE)——MOD09A1/MOD11A2/MOD13A1不同的QA波段去云进行分析

​​​​​​​大多数光学卫星图像产品都带有一个或多个 QA 波段,允许用户评估每个像素的质量并提取满足其要求的像素。QA 波段最常见的应用是提取有关多云像素的信息并对其进行屏蔽。但是 QA 波段包含大量其他信息,可以帮助您从分析中删除低质量数据。通常,QA 频带中包含的信息存储为按位标志。在这篇文章中,我将介绍与位运算相关的基本概念,以及如何使用位掩码提取和屏蔽特定的质量指标。

此星光明博客 4578

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的博客 669

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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的博客 179

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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 282

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 460

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的博客 1289

Gym - 101128C Canvas Painting 逆向思维

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

jerans的博客 947

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的博客 811

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的博客 727

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的博客 697
上一篇: 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、付费专栏及课程。

余额充值