搜索写的不熟练,一看到树就害怕,抽自己一巴掌
扪心自问。。。。你都在干什么鬼事情
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 2e5+10;
const int inf = 0x3f3f3f3f;
vector<int> save[maxn];
int color[maxn];
int n,res;
void dfs(int now, int father){
int i,k;
k = 0;
for(i=0;i<save[now].size();i++){
if(save[now][i]==now||color[save[now][i]]!=inf){
continue;
}
k++;
while(color[now]==k||color[father]==k){
k++;
}
color[save[now][i]] = k;
dfs(save[now][i], now);
}
res = res<k?k:res;
}
int main(){
int i,a,b;
scanf("%d",&n);
for(i=1;i<=n-1;i++){
scanf("%d%d",&a,&b);
save[a].push_back(b);
save[b].push_back(a);
}
memset(color, inf, sizeof(color));
color[0] = 0;
color[1] = 1;
dfs(1, 0);
printf("%d\n",res);
i = 1;
while(color[i]!=inf){
printf("%d ",color[i++]);
}
return 0;
}