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++ std::ifstream
#include <iostream> #include <plug/plug.h> using namespace std; //使用宽字符,我猜是为了适应那些要使用宽字符的 ...
- 每一行最后添加文字python脚本
比较简单的在pycharm上跑的脚本 #_*_coding:utf-8_*_ #普通版 file = open("oldfile.txt","r",newlin ...
- JZOJ.5235【NOIP2017模拟8.7】好的排列
Description 对于一个1->n的排列 ,定义A中的一个位置i是好的,当且仅当Ai-1>Ai 或者Ai+1>Ai.对于一个排列A,假如有不少于k个位置是好的,那么称A是一个好 ...
- oracle批量update
我个人觉得写的很好 http://blog.csdn.net/wanglilin/article/details/7200201 需求: 将t2(t_statbuf)表中id和t1(T_Mt)表相同的 ...
- Minecraft Forge编程入门一 “环境搭建”
什么是Forge Minecraft Forge is a Minecraft application programming interface (API) which allows almost ...
- php 正则表达式三.模式修正
1.贪婪模式和懒惰模式, 贪婪模式:php中正则默认是贪婪模式,匹配尽可能多 的字符,比如 $pattern='/a+b/'; $subject='aaaaaaaaab,那么可能会preg_match ...
- Java基础 - 面向对象 - 类的定义
package mingri.chapter_6; import java.util.Scanner; public class Person { /* * 类变量 * 定义方法: * 数据类型 变量 ...
- 第14章—数据库连接池(C3P0)
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...
- 解决 pip 安装opendr包 卡住的问题
使用豆瓣的源(已经确认过了该源中有opendr包),pip安装opendr,结果卡在了下载完成的位置,什么提示也没有.(如下图) 既然安装包已经下载下来了又安装不上,则应该是安装代码中有什么问题,只不 ...
- 查找文件路径find
查找文件路径find 1.按照文件名查找 (1)find / -name httpd.conf #在根目录下查找文件httpd.conf,表示在整个硬盘查找 (2)find ...