C. Andryusha and Colored Balloons

来源:互联网 发布:三菱plc编程实例100 编辑:程序博客网 时间:2024/04/29 20:26
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 
Note

In the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.

Illustration for the first sample.

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
We can see that each pair of squares is encountered in some triple, so all colors have to be distinct.
Illustration for the second sample.

In the third example there are following triples:

  • 1 → 2 → 3
  • 2 → 3 → 4
  • 3 → 4 → 5
We can see that one or two colors is not enough, but there is an answer that uses three colors only.
Illustration for the third sample.


这道题不知道为什么,我自己写的广搜就爆内存,恕我能力尚低还不能找出错误,先把代码贴上以后找。


#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 1000000007llusing namespace std;vector<int> a[200005];int maxid=1;int maxnum=-1;int num[200005];int vis[200005];  //vis标记某个点已经被染过了 int res[200005];  //保存点的染色 map<int,int> mpa;  //这个map相当于在对每一个点的邻接点染色时,标记那个颜色已经染过了。 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].push_back(y);        a[y].push_back(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<=maxnum+1;++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;}

#include<iostream>#include<queue>#include<string.h>#include<stdlib.h> #include<stdio.h>using namespace std;int n;struct node{int x;int icolor;int ucolor;};int main(){scanf("%d",&n);int map[n+1][n+1];int book[n+1];memset(book,0,sizeof(book));int col[n+1];int vis[n+1];memset(vis,0,sizeof(vis));memset(col,0,sizeof(col));int i,j,max=-1;for(i=1;i<=n;i++)for(j=1;j<=n;j++)map[i][j]=0;int tx,ty;for(i=0;i<n-1;i++){scanf("%d %d",&tx,&ty);map[tx][ty]=map[ty][tx]=1;book[tx]++;book[ty]++;if(book[tx]>max) max=book[tx];  //记录最大连通结点 if(book[ty]>max) max=book[ty]; }  cout<<max+1<<endl; queue<node> que;struct node start,temp;int count=0;start.x=1;start.icolor=1;start.ucolor=-1;col[1]=1;count++;vis[1]=1; //已经染色 que.push(start);while(!que.empty()){//int book[max+5];//memset(book,0,sizeof(book));start=que.front();//book[start.icolor]=1;que.pop();if(start.x==1)  //第一个点需要特殊处理 {int c=2;for(i=2;i<=n;i++){if(map[start.x][i]){vis[i]=1;  //已经染色 col[i]=c;count++;//printf("countrrr=%d\n",count);temp.x=i;temp.icolor=c;temp.ucolor=1;c++;que.push(temp);if(count==n){for(i=1;i<=n;i++)printf("%d ",col[i]);return 0;}}}}else{ int c=1; for(i=1;i<=n;i++) { if(map[start.x][i] && vis[i]==0) { vis[i]=1; while(c==start.icolor || c==start.ucolor) c++; col[i]=c; count++; //printf("count=%d\n",count); temp.x=i; temp.icolor=c; temp.ucolor=start.icolor; c++; que.push(temp); if(count==n) { for(i=1;i<=n;i++) printf("%d ",col[i]); return 0; } } }} } return 0;}


0 0