浮生事、”无根树"、事正宗、花正幽——一道“无根树”BFS

原题链接

Codeforces 782C

Andryusha and Colored Balloons(BFS)

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.

Example

Input

3
2 3
1 3

Output

3
1 3 2 

Input

5
2 3
5 3
4 3
1 3

Output

5
1 3 2 5 4

Input

5
2 1
3 2
4 3
5 4

Output

3
1 2 3 1 2 

//这是一道有趣的搜索,我初次接触,现在终于理解BFS里面队列的作用了,队列就是用来储存当前节点所在层的全部节点,树的遍历是以层优先的;对于这道题目,我们以一个结构体来保存每一个节点的序号,颜色和爸爸^_^(父)节点的颜色,用一个vector来保存要输入的每个广场的关系(edge,就是连通的路径);然后就非常方便了。
//下面讲一下如何实现搜索:首先我们还需要四个变量,ans(保存答案),pos保存当前的节点(爸爸节点,取出pop),curr(此时此刻正在访问的vector里面的广场编号),assigncolor;准备好之后就可以按照BFS的基本格式写了,需要注意的是assigncolor从1
开始,写在while里面,每次遍历完一层之后都要将assigncolor重置为1,如果在同一层里面遍历,就在当前的基础上++;最后的答案ans取颜色数字k和ans中较大的一个,不解释;

代码如下:

include <bits/stdc++.h>  
using namespace std;
//bfs
#define MAX 200020
int n;
int x,y,ans,pos,curr,tempc;
vector<int>a[MAX];
typedef struct{
    int i;
    int color;
    int lastc;
}node;
node s[MAX];
void bfs(){
    for(int i=0;i<=n;i++){
        s[i].color=0;
    }
    ans=1;
    queue<node>Q;
    s[1].i=1;s[1].color=1;s[1].lastc=1;
    Q.push(s[1]);
    while(!Q.empty()){
        tempc=1;
        pos=Q.front().i;
        Q.pop();
        for(int k=0;k<a[pos].size();k++){
            curr=a[pos][k];
            s[curr].i=curr;//特别注意序号要赋值,不些答案会是 
            if(!s[curr].color){
                s[curr].lastc=s[pos].color;
while(tempc==s[pos].color||tempc==s[pos].lastc)tempc++;
                s[curr].color=tempc;
                tempc++;//同一层
                Q.push(s[curr]);
            }
        }
        ans=max(tempc-1,ans);
    }
}
void output(){
    cout<<ans<<endl;
    for(int i=1;i<=n;i++){
        a[i].clear();
        cout<<s[i].color<<" ";
        if(i==n)cout<<endl;
    }
}
int main(){
    cin>>n;
    for(int i=0;i<n-1;i++){
        cin>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);//节点关系嘛 x-y  y-x 懂?
    }
    bfs();
    output();
    return 0;
}

GO TOOOOO BED 困高!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值