您的位置:首页 > 其它

CF - 782C. Andryusha and Colored Balloons - DFS染色问题

2017-03-10 17:03 375 查看
1.题目描述:

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
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


Note

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.

2.题意概述:

给你一个连通图,对它进行染色,某节点的颜色和他的相邻节点颜色不能相同,且对于每个父亲,它所有儿子的颜色都不能相同,问你最少需要多少颜色

3.解题思路:

很经典的染色问题,建图方法很多,可以vector也可以链式前向星,从任意节点开始dfs,每代记录和当代和父代的节点标号,这样可以保证当代的子代颜色与两者都不相同,接下来就是染色了,当时想的时候是vis记录颜色使用情况,现在想想大可不必,直接用cnt记录就好,注意的是,把当代所有儿子都染色完以后再去对儿子进行dfs。感觉bfs也是可以做的,以后再想咯。

更新:

这道题性质是对于每个父亲,所有儿子的颜色不同,而bfs性质又是要对父亲进行bfs,这样就不能保证当前是父亲,下一步对儿子染色时能有传递性了,除非另外记录上一步转移过来的颜色,最好做法是DFS。而如果只要求相邻节点颜色不同,则做法可以是bfs贪心地选颜色,具体题型参见UVA1613。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 200100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
vector<int> G[maxn];
int color[maxn];
void dfs(int cur, int pre)
{
int cnt = 1;
for (int i = 0; i < (int)G[cur].size(); i++)
if (G[cur][i] != pre) //子父颜色不同
{
while (cnt == color[cur] || cnt == color[pre])
cnt++;
color[G[cur][i]] = cnt++; //染色
}
//先把当代儿子染完色再dfs
for (int i = 0; i < (int)G[cur].size(); i++)
if (G[cur][i] != pre)
dfs(G[cur][i], cur);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n;
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
G[i].clear();
for (int i = 0; i < n - 1; i++)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(color, 0, sizeof(color));
color[1] = 1;
dfs(1, 0);
int ans = 0;
for (int i = 1; i <= n; i++)
ans = max(ans, color[i]);
printf("%d\n", ans);
for (int i = 1; i <= n; i++)
if (i == 1)
printf("%d", color[i]);
else
printf(" %d", color[i]);
puts("");
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 算法 codeforces