Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C

来源:互联网 发布:淘宝店铺自然流量 编辑:程序博客网 时间:2024/06/01 08:55
C. Andryusha and Colored Balloons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ab 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
input
32 31 3
output
31 3 2 
input
52 35 34 31 3
output
51 3 2 5 4 
input
52 13 24 35 4
output
31 2 3 1 2 
题意:给出一个连通图,连接的三个点(意思是例如A和B相连,B和C相连,那么A、B、C这三个点就是连接的)不能相同颜色。现在给每个点染色,问最少要几种颜色。

思路:我用的是bfs,点1染颜色1,从点1开始bfs,在遍历的过程中,用一个数组存该点前一个点(即bfs到达这个点的点)的颜色,然后颜色选择不等于前一个点的颜色的最小值。然后bfs遍历其连通的点,颜色从该点的颜色值+1开始遍历。我表达能力不太好,大家还是直接看我的代码吧。。。

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<map>#include<vector>#include<iostream>#include<queue>using namespace std;vector<int>vv[200005];int ans;int tip[200005];int color[200005];int lcolor[200005];void bfs(){    queue<int>temp;    temp.push(1);    int lc,nc;    lc=0; nc=1;    int i,j,k;    color[1]=1;    lcolor[1]=0;    tip[1]=1;    while(!temp.empty())    {        k=temp.front();        temp.pop();        for(i=0;i<vv[k].size();i++)        {            if(tip[vv[k][i]])                continue;            temp.push(vv[k][i]);            while(nc==lcolor[k]||nc==color[k])            nc++;            color[vv[k][i]]=nc;            lcolor[vv[k][i]]=color[k];            if(ans<nc)                ans=nc;            nc++; tip[vv[k][i]]++;        }        nc=1;    }}int main(){    int n;    while(~scanf("%d",&n))    {        int i,j,k,a,b;        ans=0;        memset(tip,0,sizeof(tip));        memset(lcolor,0,sizeof(lcolor));        color[1]=1;        for(i=0;i<n+2;i++)            vv[i].clear();        for(i=1;i<n;i++)        {            scanf("%d%d",&a,&b);            vv[a].push_back(b);            vv[b].push_back(a);        }        bfs();        printf("%d\n",ans);        printf("%d",color[1]);        for(i=2;i<=n;i++)            printf(" %d",color[i]);        printf("\n");    }    return 0;}


0 0
原创粉丝点击