Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C
来源:互联网 发布:淘宝店铺自然流量 编辑:程序博客网 时间:2024/06/01 08:55
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!
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.
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.
32 31 3
31 3 2
52 35 34 31 3
51 3 2 5 4
52 13 24 35 4
31 2 3 1 2
#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;}
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)【A,B,C】
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A,B,C
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C (dfs+vector)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A.B.C
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C
- [Updating]Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)解题报告
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B.(三分)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B.(三分)
- 【解题报告】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A
- Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals) A. Andryusha and Colored Balloons
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 题解(ABCDE) (二分,dfs序,数据结构)
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Change
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Change
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017
- C# 阻塞方式Socket接受异常处理(不定期更新)
- sdut2164——Binomial Coeffcients
- OC打印指针地址和arc下的retaincount
- 基于北洋OPOS SDK二次开发包,支持EPSON和北洋、佳博、商祺等支持标准ESC/POS指令的POS打印机
- Spring Boot学习笔记-零xml配置jpa
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C
- 哈希表(散列表)详解及代码实现
- Rational Grading UVALive
- ASP.NET获取客户端IP及MAC地址
- easyUI datagrid数据导出为Excel
- 2014年以来高被引论文
- java ------华为上机机试--计算字符串个数
- Problem D: 线性表的基本操作
- ZCMU—1907