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
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
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.
題意:給出n個節點,讓你給這棵樹染色,要求用的顏色最少,然後要求任意三個相連的顏色不能相同___________________________________________________________________________________思路:最原始的染色是黑白染色,只要求相鄰不相同就行,而這個要求三個相連不相同,其實我們只要儲存前兩個節點顏色是什麼,然後再去找即可,有一個要理解的地方,就是由兩個節點推導過來的顏色,那個點相鄰的幾個節點肯定顏色都是不同的,如圖
從上圖中可以看到這是錯誤的,說明由兩點推出來的點肯定都不一樣,所以上圖那三點應該分別染色 3,4,5,詳細見程式碼
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
vector<int> mp[];
int n,x,y;
int vis[];
void dfs(int x,int y)
{
int cnt=;//上面已解釋
for(int i=;i<mp[x].size();i++)
{
if(mp[x][i]==y) continue;
while(cnt==vis[x]||cnt==vis[y])//尋找可以賦值的顏色
cnt++;
vis[mp[x][i]]=cnt++;//給節點染色
}
for(int i=;i<mp[x].size();i++)//使用新的兩個節點
{
if(mp[x][i]!=y)
dfs(mp[x][i],x);
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n-;i++)//領接表建圖
{
scanf("%d%d",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
vis[]=;
dfs(,);
int mx=vis[];
for(int i=;i<=n;i++)//取最大顏色
mx=max(mx,vis[i]);
printf("%d\n",mx);
for(int i=;i<=n;i++)
{
printf("%d ",vis[i]);
}
}