Codeforces780C
题解:n个气球 从1到n染色,如果a、b和c是不同的正方形,a和b在它们之间有一条直接的路径,b和c之间有一条直接的路径,然后在这三个方块上的气球颜色是不同的。
AC代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
using namespace std;
const int maxn= ;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int color[maxn];
std::vector<int> v[maxn];
int n,ans=;
void dfs(int now,int father)
{
int i,k;
k=;
for(i=; i<v[now].size(); i++)
{
if(color[v[now][i]]!=inf)
{
continue; //该点已经访问过,直接下一个邻接点
}
k++;
while(color[now]==k||color[father]==k) //确定颜色
{
k++;
}
color[v[now][i]]=k;
dfs(v[now][i],now);
}
ans=ans<k?k:ans;
}
int main(int argc, char const *argv[])
{
int a,b;
cin>>n;
for(int i=; i<n-; i++)
{
cin>>a>>b;
v[a].push_back(b); //存图的边
v[b].push_back(a);
}
memset(color,inf,sizeof(color));
color[]=;
color[]=;
dfs(,);
cout<<ans<<endl;
for(int i=; i<n; i++)
{
cout<<color[i]<<" ";
}
cout<<color[n]<<endl;
return ;
}
2 seconds
256 megabytes
standard input
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!
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
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.
Codeforces780C的更多相关文章
随机推荐
- GitLab配置ssh key
一.背景 当前很多公司都选择git作为代码版本控制工具,然后自己公司搭建私有的gitlab来管理代码,我们在clone代码的时候可以选择http协议,当然我们亦可以选择ssh协议来拉取代码.但是网上很 ...
- DeepLearning.ai学习笔记(四)卷积神经网络 -- week1 卷积神经网络基础知识介绍
一.计算机视觉 如图示,之前课程中介绍的都是64* 64 3的图像,而一旦图像质量增加,例如变成1000 1000 * 3的时候那么此时的神经网络的计算量会巨大,显然这不现实.所以需要引入其他的方法来 ...
- 2018第一发:记一次【Advanced Installer】打包之旅
一.前言 2017年最后几天,你们都高高兴兴的跨年,博主还在加班制作.net安装包.因为年前要出来第一版的安装包,所以博主是加班加点啊.本来想用VS自带的制作工具,不过用过的人都知道,真是非常好(to ...
- grep 与 find 简单命令
在使用linux的时候,经常会用到查找文件或者查找文本,下面介绍两个命令. grep 使用方法: // 在当前目录下递归查找class字符串 grep "string" -r . ...
- [编织消息框架][网络IO模型]Netty Reactor
严格来讲Netty Reactor是一种设计模式,一听模式两字就知道了吧,套路哈哈 Reactor中文译为“反应堆”. 看图netty处理流程 1.netty server 至少有两组reactor. ...
- python3 python2 import 的区别
https://stackoverflow.com/questions/12172791/changes-in-import-statement-python3
- ASP.NET网页发布以及相关问题的解决
今天做了一个统计站点的网页,想要发布一下,中间碰到不少问题,现在和大家分享一下! 这是我想要最终的网页结果: 1.发布站点到桌面(任意路径) 2.安装IIS 3.安装好后,打开IIS, ...
- MySQL 数据类型和约束(外键是重点🙄)
数据类型 1. 数字(默认都是由符号,宽度表示的是显示宽度,与存储无关).tinyint 括号里指定宽度 七位2进制数最大数就是2**7 -1=127 最小是-128 验证: create tabel ...
- vue 全局插槽 全局插座
场景: slot 能够让父组件内容插入到子组件中,但是子孙组件不能够使用slot直接插入内容.在弹窗的时候,全屏弹窗需要直接在组件最上层显示,如果父组件级别不够,弹出就可能不是全屏的. 知识点: 1: ...
- 非常好用的弹出层 layer,常用功能demo,快速上手!
功能强大,实用,操作方便,文档齐全. 参数灵活,丰富.可以作为开发项目的公共模块,多处使用.老文档地址:http://layer.layui.com/api.html 已经停止维护 新文档地址:htt ...