码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round 403C

时间:2018-08-02 10:21:33      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:codeforce   direct   ssi   wing   nim   sam   range   end   output   

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

标签:codeforce   direct   ssi   wing   nim   sam   range   end   output   

原文地址:https://www.cnblogs.com/brilliant107/p/9404993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!