Codeforces Round #403

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

32 31 3

output

Copy

31 3 2 

input

Copy

52 35 34 31 3

output

Copy

51 3 2 5 4 

input

Copy

52 13 24 35 4

output

Copy

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

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

时间: 2024-10-12 12:07:38

Codeforces Round #403的相关文章

Codeforces Round #403 div2 C. Andryusha and Colored Balloons

题目链接:Codeforces Round #403 div2 C. Andryusha and Colored Balloons 题意: 给你一棵n个节点的树,然后让你染色,规定相连的三个 节点不能同色,问需要的最少颜色,并输出其中一种方案. 题解: 因为只有相邻3个节点不同色. 所以直接DFS,每个节点都从1开始. 然后ans[v]!=ans[u]!=ans[fa]就行. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i&

Codeforces Round #403 D. Innokenty and a Football League

题目链接:Codeforces Round #403 D. Innokenty and a Football League 题意: 某人需要给若干球队选择队名缩写.已知每个球队的名字必然是 <team name> <hometown name> 的形式.取队名缩写的规则是固定的,只有两种: 选择 team name 的前三个字母作为队名缩写 选择 team name 的前两个字母与 hometown name 的首字母组成队名缩写. 每个队的队名缩写都可以从两种中任选一种,问能够使

Codeforces Round #403(div 2)

A =w= B 题意:一个数轴上有n个整点,每个点都有一个速度,选一个点让他们集合,使得时间最少. 分析: 直接三分 C 题意:给定一棵树,任意两个距离小等于二的点不能染相同的颜色,求最小颜色数和染色方案. n<=2*10^5 分析: 容易知道答案就是最大的度数+1 至于方案直接暴搜出方案就行 D 题意:有n个足球队,每个队有两个选名字的方案,也就是直接给定了两个名字.所有球队不能有相同的名字.特殊的,如果有两个球队第一个名字相同,一个球队选了第二个名字,另一个球队不能选第一个名字.n<=10

Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)

Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Meeting Place Cannot Be Changed 题目大意:n个人,第i个人位于xi,速度为vi,找到一个点使得所有人到这个点的耗时最小,输出耗时.(n<=60000) 思路:二分答案,知道耗时后可以求出每个人能到达的区间,如果所有区间有交则合法,复杂度O(nlog). #include<

Codeforces Round #403 (Div. 2)解题报告

A. 1 #include <iostream> 2 #include<bits/stdc++.h> 3 #include <stack> 4 #include <queue> 5 #include <map> 6 #include <set> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 #include &l

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿