CF781A Andryusha and Colored Balloons

Posted 王宜鸣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF781A 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 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 ab and c are distinct squares that aand 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

大意是说给一棵无根树染色,相邻的三个节点颜色不能相同,问至少需要多少种颜色。

思路:

有趣的搜索。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 using namespace std;
 5 
 6 vector<int> G[200005];
 7 int color[200005];
 8 int ans = 1;
 9 void dfs(int now, int f)
10 {
11     int x = 0;
12     for (int i = 0; i < G[now].size(); i++)
13     {
14         if (G[now][i] == f || color[G[now][i]])
15             continue;
16         x++;
17         while (x == color[now] || x == color[f])
18             x++;
19         color[G[now][i]] = x;
20         dfs(G[now][i], now);
21     }
22     if (x > ans)
23         ans = x;
24 }
25 int n, x, y;
26 int main()
27 {
28     cin >> n;
29     for (int i = 1; i <= n - 1; i++)
30     {
31         cin >> x >> y;
32         G[x].push_back(y);
33         G[y].push_back(x);
34     }
35     color[1] = 1;
36     dfs(1, 0);
37     cout << ans << endl;
38     for (int i = 1; i <= n; i++)
39     {
40         cout << color[i] << " ";
41     }
42     cout << endl;
43     return 0;
44 }

 

以上是关于CF781A Andryusha and Colored Balloons的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 780C Andryusha and Colored BalloonsDFS

Codeforces 780G Andryusha and Nervous Barriers 线段树套set || 线段树套单调栈

(CF#257)B. Jzzhu and Sequences

CF | Alyona and Numbers

Vika and Segments - CF610D

CF 862C Mahmoud and Ehab and the xor(异或)