Andryusha and Colored Balloons CodeForces - 780C

题目

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
Copy

3
2 3
1 3

Output
Copy

3
1 3 2

Input
Copy

5
2 3
5 3
4 3
1 3

Output
Copy

5
1 3 2 5 4

Input
Copy

5
2 1
3 2
4 3
5 4

Output
Copy

3
1 2 3 1 2

翻译

一棵树有n个节点,你需要给它的所有节点图上颜色。相邻三个节点不能有相同颜色,问最少需要多少种颜色。
相邻三个节点的意思是 节点x1与x2相邻,x2与x3相邻,那么x1与x3也相邻, x1,x2,x3颜色各不相同
Input

第一行一个n (3 <= n <= 200000)
接下来n - 1行,每行两个整数x,y(1 <= x, y <= n),表示节点x和节点y连接。保证给出的树合法。
Output

第一行输出一个整数,表示最小需要的颜色种类
接下来n行每行一个整数,第i个整数表示第i个节点的颜色,每个节点的颜色数值在1~k范围内

解释

该图为无向连通图,该问题的答案为度数最大的点的度maxx加1。可以设想;你找到度数最大点之后,该点是一种颜色,与其相连的点染上不同的颜色。然后每个相连点除去自己与最大点的1度,还剩相邻点点最多为maxx-1,剩颜色maxx+1-2,两者相等,所以一定够涂色。
在已知颜色数后,可以选择最大度数的点往外涂,也可以任选一点往外涂色。
只要注意当前涂色节点要与父亲节点和祖父节点不同就好,利用dfs完成涂色。输出顺序编号对应涂色的编号即可。

#include <cstdio>
#include <algorithm>
#include<vector>
int const maxn = 2e5+1;
using namespace std;

int n;
vector<int> G[maxn];
int deg[maxn];
int clor[maxn];
void dfs(int pos, int fa, int gra){
	int cont = 1;
	for(int i = 0; i < G[pos].size(); i++){
		if(!clor[G[pos][i]]){
			while(cont == fa||cont == gra)
				cont++;
			clor[G[pos][i]] = cont;
			dfs(G[pos][i],cont,fa);
			cont++;
		}
	} 
	return ;
}

int main(){

	int a, b;
	scanf("%d", &n);
	for(int i = 1; i<=n-1; i++){
		scanf("%d %d", &a, &b);
		G[a].push_back(b);
		G[b].push_back(a);
		deg[a]++;
		deg[b]++;
	}
	int maxx = 0;
	int start = 0;
	for(int i = 1; i<= n; i++)
		if(deg[i] > maxx){
			maxx = deg[i];
			start = i;
		}
	printf("%d\n", maxx+1);
	//start = 1 其实任意起点dfs搜索都是可以的 只是染色编号不一样 本质等价 
	clor[start] = 1;
	int cont = 2;
	for(int i = 0; i < G[start].size(); i++){
		if(!clor[G[start][i]]){
			clor[G[start][i]] = cont;
			dfs(G[start][i],cont++,1);
		}
	} 
	for(int i = 1; i <= n; i++){
		printf("%d%c", clor[i], i == n?'\n':' ');
	}

	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值