*【codeforce】782C 【dfs】
来源:互联网 发布:明道办公软件官网 编辑:程序博客网 时间:2024/05/21 19:21
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 ifa, b andc are distinct squares that a and b have a direct path between them, andb 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 integern (3 ≤ n ≤ 2·105) — the number of squares in the park.
Each of the next (n - 1) lines contains two integers x andy (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 integerk — 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 thei-th square. Each of these numbers should be within range from1 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
In the first sample the park consists of three squares:1 → 3 → 2. Thus, the balloon colors have to be distinct.
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
In the third example there are following triples:
- 1 → 2 → 3
- 2 → 3 → 4
- 3 → 4 → 5
#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<map>using namespace std;pair<int,int> p[200001];vector<int> v[200001];int n,x,y,m,color[200001];void dfs(int a,int b){int t=1;for(int i=0,g;i<v[a].size();i++){g=v[a][i];if(g!=b) {while(color[a]==t||color[b]==t) t++;m=max(m,t);color[g]=t;t++;dfs(g,a);}}}int main(){while(~scanf("%d",&n)){for(int i=1;i<=n;i++)v[i].clear();memset(color,-1,sizeof(color));m=0;for(int i=1;i<=n-1;i++){scanf("%d%d",&p[i].first,&p[i].second);x=p[i].first;y=p[i].second;v[x].push_back(y);v[y].push_back(x);}color[1]=1;dfs(1,-1);printf("%d\n",m);for(int i=1;i<n;i++)printf("%d ",color[i]);printf("%d\n",color[n]);}return 0; }
- *【codeforce】782C 【dfs】
- Codeforce 893C Rumor (dfs)
- codeforce C. Valera and Elections (DFS)
- CodeForce 505C Mr. Kitayuta(DP+DFS)
- codeforce 550c Divisibility by Eight (DFS)
- Codeforce 839C Journey (dfs+概率)
- codeforce 6B dfs
- codeforce New Problem(dfs)
- DFS&&codeforce 598d
- codeforce 659E (dfs 水~)
- codeforce 842C. Ilya And The Tree(dfs用set存储到每种可能)
- Codeforce # C. Average Score
- CodeForce 279C
- codeforce 237 C D
- Codeforce C. Barcode
- Codeforce 57C Array
- Codeforce 240C Practice
- Codeforce 332C
- 【Spring】使用Spring发送邮件
- 浮点类型
- 循环神经网络(RNN)模型与前向反向传播算法
- [BZOJ]4404: [Neerc2015]Binary vs Decimal
- Cmake总结
- *【codeforce】782C 【dfs】
- 天池大赛 道路匹配试题
- jzoj 5000. 【NOI2017模拟3.4】保镖 hall定理+搜索
- redis集群和Sentinel功能
- iocp原理
- C++ stringstream clear()和.str("")
- Retrofit + RxJava 的结合使用
- 洛谷 P2964 [USACO09NOV] 硬币的游戏A Coin Game
- 【BZOJ 1019】 [SHOI2008]汉诺塔