meaning:就是给n个点,这n个点可以从任意点到任
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.
![](https://i-blog.csdnimg.cn/blog_migrate/809d5f1ec559f214cc57d99c87b790bf.png)
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
![](https://i-blog.csdnimg.cn/blog_migrate/a009479ebc6b863e933bd52203695ef4.png)
In the third example there are following triples:
- 1 → 2 → 3
- 2 → 3 → 4
- 3 → 4 → 5
![](https://i-blog.csdnimg.cn/blog_migrate/95015de3466944c1c8b3e8330c2c8fe4.png)
thinking: dfs找点,然后涂色,如1-2-3,从1开始找到2,然后2的颜色不跟1一样且不跟1连着的所有点颜色相同即可,那么就可以用一个j来记录1连有什么颜色的点了,
如果j=2或者1颜色的点,那么j++,找完2又从3找下去即可。
vector的二维不定长数组是从0开始的!!!!!!
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
int g1[200500];
int walked[200500];
int walk[200500];
vector<int>qq[200500];
int max1;
int dfs(int x,int y){
int i,j;
j=1;
for(i=0;i<qq[y].size();i++){
//cout << "y= " << y <<" i= " <<i << " " <<walked[y] << endl;
if(qq[y][i]!=x){
while(g1[x]==j||g1[y]==j){
j++;
}
g1[qq[y][i]]=j;
if(j>max1)max1=j;
j++;
}
}
for(i=0;i<qq[y].size();i++)
if(qq[y][i]!=x)
dfs(y,qq[y][i]);
}
int main(){
//freopen("in.txt","r",stdin);
int i,j,k,l,f1,f2,f3,t1,t2,t3;
int n;
cin >> n;
memset(g1,0,sizeof(g1));
memset(walked,0,sizeof(walked));
memset(walk,0,sizeof(walk));
for(i=1;i<n;i++){
scanf("%d %d",&f1,&f2);
//cout << f1 <<f2 <<endl;
qq[f1].push_back(f2);
//printf("%d\n",qq[f1][0]);
qq[f2].push_back(f1);
//walk[f1]++;
//walk[f2]++;
}
g1[1]=1;
//cout << qq[1][1] <<endl;
j=2;
for(i=0;i<qq[1].size();i++){
g1[qq[1][i]]=j;
walked[i]=j;
if(j>max1)max1=j;
j++;
}
for(i=0;i<qq[1].size();i++)
dfs(1,qq[1][i]);
cout << max1 <<endl;
for(i=1;i<=n;i++)
if(i==1)
cout << g1[i];
else
cout << " " <<g1[i];
cout << endl;
//cout << walked[3] <<endl;
return 0;
}