Codeforces 782C. Andryusha and Colored Balloons 搜索
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.
3
2 3
1 3
3
1 3 2
5
2 3
5 3
4 3
1 3
5
1 3 2 5 4
5
2 1
3 2
4 3
5 4
3
1 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.
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<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
const int MAXN=2e5+,INF=0x3f3f3f3f,MOD=1e9+;
int n,col[MAXN];
map<int,vector<int> >G;
int ans;
dfs(int now,int sign,int fa,int flag)
{
//cout<<now<<" "<<sign<<" "<<fa<<" "<<flag<<endl;
int tmp=;
for(int i=; i<G[now].size(); i++)
{
int to=G[now][i];
if(to==fa) continue;
while(++tmp)
{
if(tmp!=sign&&tmp!=flag)
{
col[to]=tmp;
if(tmp>ans) ans=tmp;
dfs(to,tmp,now,sign);
break;
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
col[]=;
ans=;
dfs(,,-,-);
cout<<ans<<endl;
for(int i=; i<=n; i++) cout<<col[i]<<" ";
cout<<endl;
return ;
}
搜索
Codeforces 782C. Andryusha and Colored Balloons 搜索的更多相关文章
- CodeForces - 780C Andryusha and Colored Balloons(dfs染色)
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, ...
- 782C. Andryusha and Colored Balloons DFS
Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons
地址:http://codeforces.com/contest/782/problem/C 题目: C. Andryusha and Colored Balloons time limit per ...
- AC日记——Andryusha and Colored Balloons codeforces 780c
C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...
- code force 403C.C. Andryusha and Colored Balloons
C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...
- codeforces781A Andryusha and Colored Balloons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- C. Andryusha and Colored Balloons
C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...
- 【codeforces 782C】Andryusha and Colored Balloons
[题目链接]:http://codeforces.com/contest/782/problem/C [题意] 给你一棵树 让你满足要求 ->任意相连的3个节点的颜色不能相同 的情况下进行染色 ...
- Codeforces 781A:Andryusha and Colored Balloons(DFS染色)
http://codeforces.com/contest/782/problem/C 题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色. 思路:比 ...
随机推荐
- workerman Channel组件全局广播
<?phpuse Workerman\Worker; require_once '../../web/Workerman/Autoloader.php';require_once '../../ ...
- tomcat 修改jdk版本号
set JAVA_OPTS=-Djute.maxbuffer=2048000 set console_log=true set CATALINA_OPTS=-server -Xdebug -Xnoag ...
- 函数式编程语言(Functional Program Language)
(一) 什么是函数编程语言 简单说,"函数式编程"是一种"编程范式"(programming paradigm),也就是如何编写程序的方法论. 是一种编程典范, ...
- pta6-17(另类堆栈)
题目链接:https://pintia.cn/problem-sets/1101307589335527424/problems/1101313244872126464 题意:一种新的堆栈,用Top表 ...
- PAT1021(dfs 连通分量)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- TOJ4439微积分――曲线积分(数学,模拟)
传送门:点我 格林公式P,Q为关于x,y的函数. 现在为了方便起见,现给出x的积分上限1,积分下限0, y的积分上限x,积分下限0. P只是关于Y的函数,Q只是关于X的函数. 输入 开始输入为测试组数 ...
- Beginning C# Programming with Unity
Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...
- 【svn】服务器搭建和迁移
导语 svn客户端大部分开发都会用到,但是为什么我们仍然需要svn服务端呢? 理由可能有: 1,我们想存放一些属于自己的文档,而不像被其他人发现(在自己的网络环境中,安全性更高,更易用,不依赖于公司, ...
- 【网络编程一】主机字节序与网络字节序以及ip地址转换函数
在计算机设计之初,对内存中数据的处理也有不同的方式,(低位数据存储在低位地址处或者高位数据存储在低位地址处),然而,在通信的过程中(ISO/OSI模型和TCP/IP四层模型中),数据被一步步封装(然后 ...
- 自编辑列的gridview,分页,删除,点击删除提示“确认”
分页: gridview的属性中:AllowPaging="True" PageSize="2" 找到gridview的PageIndexChan ...