Codeforces Round 403C

Posted brilliant107

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round 403C相关的知识,希望对你有一定的参考价值。

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

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

题意:给你一棵树,距离为1或2的节点颜色不能相同,求最小颜色数方案。

题解:bfs搜点,记录父亲和爷爷节点,儿子与他们的颜色不能相同,注意邻接表的bfs如何写

 1 var
 2 head,next,a,dep,col:array[0..400000]of longint;
 3 i,e,x,y,n,ans:longint;
 4 
 5 procedure add(x,y:longint);
 6 begin
 7  inc(e); a[e]:=y; next[e]:=head[x];head[x]:=e;
 8 end;
 9 procedure dfs(u,m:longint);
10  var j,l,v,i,k:longint;
11   begin
12    k:=1;
13   i:=head[u];
14   while i>0 do
15    begin
16      v:=a[i];
17      if v<>m then
18        begin
19          while (col[u]=k)or(col[m]=k) do inc(k);
20         col[v]:=k;
21         inc(k);
22        end;
23      i:=next[i];
24    end;
25    j:=head[u];
26    while j>0 do
27     begin
28       v:=a[j];
29       if v<>m then dfs(v,u);
30       j:=next[j];
31     end;
32 
33  end;
34 begin
35  readln(n);
36  for i:=1 to n-1 do
37   begin
38     readln(x,y);
39     add(x,y);
40     add(y,x);
41   end;
42   col[1]:=1;
43 dfs(1,0);
44 
45 for i:=1 to n do if ans<col[i] then ans:=col[i];
46   writeln(ans);
47 //for i:=1 to n do writeln(dep[i]);
48 for i:=1 to n do write(col[i], );
49 end.

 














以上是关于Codeforces Round 403C的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #267 (Div. 2) C. George and Job

Codeforces Round #753 (Div. 3) ABCDE

Codeforces Round #746 (Div. 2)(A,B,C)

Codeforces Round #746 (Div. 2)(A,B,C)

Codeforces Round #612

Codeforces Round #738 (Div. 2)