Codeforces 781A:Andryusha and Colored Balloons(DFS染色)
http://codeforces.com/contest/782/problem/C
题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色。
思路:比赛的时候没想到是找度最大的一个点并+1就是总颜色数,一直想怎么构造。
最后的总颜色数是度最大的一个点的度数+1。因为我们选的这个点到儿子的距离为1,因此其某一个儿子到另一个儿子的距离为2,就是这些儿子都要染成不同的颜色,+1是因为自己本身也要是不同的颜色。
然后确定了总颜色数,就可以DFS染色了。
参数记录一个上上个结点和上个结点染了什么颜色,只要儿子染成不同的就可以了,注意儿子之间颜色也要不同。
#include <bits/stdc++.h>
using namespace std;
#define N 200010
struct Edge {
int v, nxt;
} edge[N*];
int head[N], tot, col[N], deg[N], ans; void Add(int u, int v) {
edge[tot] = (Edge) {v, head[u]}; head[u] = tot++;
edge[tot] = (Edge) {u, head[v]}; head[v] = tot++;
} void dfs(int u, int fa, int c1, int c2) {
int c = ;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v == fa) continue;
if(col[v]) continue;
for( ; c <= ans; c++)
if(c != c1 && c != c2) {
col[v] = c; break;
}
c++;
dfs(v, u, col[v], col[u]);
}
} int main() {
int n;
scanf("%d", &n);
memset(head, -, sizeof(head));
for(int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
deg[u]++; deg[v]++;
Add(u, v);
}
ans = ;
for(int i = ; i <= n; i++) if(ans < deg[i] + ) ans = deg[i] + ; col[] = ;
dfs(, -, , -);
printf("%d\n", ans);
for(int i = ; i <= n; i++) printf("%d ", col[i]);
return ;
}
Codeforces 781A:Andryusha and Colored Balloons(DFS染色)的更多相关文章
- Codeforces 782C. Andryusha and Colored Balloons 搜索
C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...
- 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 Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
随机推荐
- Xamarin 弹窗
包括通知类弹窗和选择类弹窗,以下是安卓手机的显示效果 关键代码 DisplayAlert("Alert", "You have been alerted", & ...
- C# ToString() 数据格式
double[] numbers= {1054.32179, -195489100.8377, 1.0437E21, -1.0573e-05}; string[] specifiers = { &qu ...
- CentOS 7.3 源码安装apache 2.4.16配置基于域名的虚拟主机
主配置文件末尾添加一条配置: [root@vm2 ~]# vim /usr/local/apache/conf/httpd.conf Include conf/vhosts.conf 在conf目录下 ...
- MVC基架生成的Index视图
@model IEnumerable<MyMusicStore.Models.Album> @{ ViewBag.Title = "Index"; } < ...
- JS trim函数
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- DOM解析xml实现读、写、增、删、改
qt提供了三种方式解析xml,不过如果想实现对xml文件进行增.删.改等操作,还是DOM方式最方便. 项目配置 pro文件里面添加QT+=xml include <QtXml>,也可以in ...
- 为什么使用useLegacyV2RuntimeActivationPolicy?
原文:为什么使用useLegacyV2RuntimeActivationPolicy? 参考:https://msdn.microsoft.com/zh-cn/library/bbx34a2h.asp ...
- VS2010调试X86汇编程序
今天突然无聊了一下,想起之前想的用VS来调试汇编程序.之前只是想了一下,没有去做,好吧,今天搜索一下,找到了一个教程,相当的详细具体,我喜欢 按照http://blog.csdn.net/jinson ...
- 改善C#程序的建议8:避免锁定不恰当的同步对象
原文:改善C#程序的建议8:避免锁定不恰当的同步对象 在C#中让线程同步的另一种编码方式就是使用线程锁.所谓线程锁,就是锁住一个资源,使得应用程序只能在此刻有一个线程访问该资源.可以用下面这句不是那么 ...
- 百度网盘web端项目总结
项目背景 网盘作为一个在线备份存储,共享文件的工具类产品,给人们的工作和生活带来了很大的帮助和便利.百度网盘是目前国内使用量最大的网盘产品,至今发展已有4年,总用户数超4亿,为了让用户有着更好的使用体 ...