Codeforces Round #403
Posted brilliant107
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #403相关的知识,希望对你有一定的参考价值。
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!
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.
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.
3
2 3
1 3
3
1 3 2
5
2 3
5 3
4 3
1 3
5
1 3 2 5 4
5
2 1
3 2
4 3
5 4
3
1 2 3 1 2
In the first sample the park consists of three squares: 1?→?3?→?2. Thus, the balloon colors have to be distinct.
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
In the third example there are following triples:
- 1?→?2?→?3
- 2?→?3?→?4
- 3?→?4?→?5
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 #403的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #403 (Div. 2)解题报告
[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段
Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)