Codeforces 782C Andryusha and Colored Balloons(BFS)

原题链接

Problem Description

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.

Sample Input

3
2 3
1 3

Sample Output

3
1 3 2

题目大意

有一棵树,给出边的连接情况,现在要给树上的结点染色,需要保证相连的三个点不被染成相同的颜色,问最少的染色数,并给出染色方案。

解题思路

最少染色数很好回答,只需要找到度最大的结点,输出度数加一就好,接下来是给出方案。从这个点开始BFS,队列中的结点保存结点的编号,当前结点的颜色以及前驱结点的颜色。当取出某个结点时,需要找到所有与之相连的结点,给这些结点染色时要保证它们的颜色互不相同且不能和当前结点以及前驱结点的颜色相同。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
//#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define rep(i,n) for(int i = 0;i < n; i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define mp make_pair
#define PI 3.1415927
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define eps 1e-7
#define mod 1000000007ll
using namespace std;
vector<int> a[200005];
int maxid=1;
int maxnum=-1;
int num[200005];
int vis[200005];
int res[200005];
map<int,int> mpa;
struct node
{
    int id;
    int color;
    int precolor;

    node(int _id,int _color,int _precolor)
    {
        id=_id;
        color=_color;
        precolor=_precolor;
    }

};
int main(void)
{
    int n;
    int x,y;
    cin>>n;
    for(int i=1;i<=n-1;++i)
    {
        scanf("%d%d",&x,&y);
        a[x].pb(y);
        a[y].pb(x);
        num[x]++;
        num[y]++;
        if(num[x]>maxnum)
        {
            maxnum=num[x];
            maxid=x;
        }
        if(num[y]>maxnum)
        {
            maxnum=num[y];
            maxid=y;
        }
    }
    printf("%d\n",maxnum+1);
    queue<node> qq;
    int color=1;
    res[maxid]=1;
    vis[maxid]=1;
    qq.push(node(maxid,1,0));
    while(!qq.empty())
    {
        node tmp=qq.front();
        qq.pop();
        mpa.clear();
        int j=1;
        for(int i=0;i<a[tmp.id].size();++i)
        {   

            if(!vis[a[tmp.id][i]])
            {
                vis[a[tmp.id][i]]=1;

                for(;j<=n;++j)
                {
                    if(j!=tmp.color&&j!=tmp.precolor&&mpa[j]==0)
                    {
                        res[a[tmp.id][i]]=j;
                        qq.push(node(a[tmp.id][i],j,tmp.color));
                        mpa[j]=1;
                        break;
                    }
                }
            }
        }
    }
    for(int i=1;i<=n;++i)
    {
        printf("%d ",res[i]);
    }
    cout<<endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值