cf780c
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!
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.
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.
3
2 3
1 3
3
1 3 2
5
2 3
5 3
4 3
1 3
5
1 3 2 5 4
5
2 1
3 2
4 3
5 4
3
1 2 3 1 2
题意:有n个节点,有n-1条边并且保证全部联通(是一棵生成树),相连的三个节点不能同色,例如A与B相连,B与C相连,那么ABC不能同色,输出颜色的最小种类和一种解决方案。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
vector<int> v[];
int k=;
int ans[];
void dfs(int x,int q)
{
int d=;
for(int i=;i<v[x].size();i++)
{
if(ans[v[x][i]]==)
{
while(d==ans[x]||d==ans[q]) d++;
ans[v[x][i]]=d++;
k=max(ans[v[x][i]],k);
dfs(v[x][i],x);
}
}
}
int main()
{
int n,i,j,a,b;
scanf("%d",&n);
for(i=; i<n-; i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
ans[]=;
dfs(,);
printf("%d\n",k);
for(i=;i<=n;i++)
{
if(i!=)
printf(" %d",ans[i]);
if(i==)
printf("%d",ans[i]);
}
return ;
}
解析:利用vector开辟动态数组,储存每个点互相链接的点,从1开始dfs遍历,并且传两个参数x,q,前者代表当前遍历的点,后者代表前一个的点,只要参考这两个点就能找的第三点的颜色。
cf780c的更多相关文章
- [CF780C]Andryusha and Colored Balloons 题解
前言 完了,完了,咕值要没了,赶紧写题解QAQ. 题意简述 给相邻的三个节点颜色不能相同的树染色所需的最小颜色数. 题解 这道题目很显然可以用深搜. 考虑题目的限制,如果当前搜索到的点为u, 显然u的 ...
随机推荐
- c++ new(不断跟新)
1.基础知识 /* 可以定义大小是0的数组,但不能引用,因为没有指向任何对象 new string[10]调用类的默认构造函数 new int[10]没有初始化,但new int[10]()会将数组初 ...
- jupyter 修改工作路径
在所需打开的目录中新建一个runJupyter.bat文件 将内容修改为: cd ......jupyter notebook 注1:上述两行中,第一行的......为路径(可以不添加,可空着不填), ...
- Poj1426
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25721 Accepted: 106 ...
- jQuery与Zepto
jQuery和Zepto是我比较常用的插件.其实用法差不太多,可以说Zepto是jQuery的轻量级替代品,但是不要认为Zepto就没有jQuery好用,因为Zepto有jQuery没有的功能,就是移 ...
- oracle mysql sqlserver 基本操作命令
1.oracle (1) 启动 监听 lsnrctl start: (2)进入sqlplus界面 sqlplus /nolog SQL>conn sys/jiaxiaoai@orcl as s ...
- Powershell About Active Directory Server
一.获取域控制器服务器清单 (Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | select hos ...
- java获取地址全路径
String basePath = request.getScheme()+"://"+request.getServerName()+":"+reques ...
- 自动适应label
CGFloat btnH = 300; NSString *text=@"你在这是NSString的对象方法,一个字符串实例调用该方法时,方法会通过传入的参数返回一个CGRect型数据,这个 ...
- cmake window下 sh.exe was found in your PATH, here
在window下 mingw环境下 用 camke 编译Cpp程序 CMake Error at D:/Program Files/CMake/share/cmake-3.8/Modules/CMak ...
- Ad Exchange
品友互动-基于大数据技术的人工智能决策平台 http://www.ipinyou.com.cn/about?flag=milestones