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个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
随机推荐
- Android数组和开发List之间的转换
1.List转换到一个数组.(这里List它是实体是ArrayList) 转让ArrayList的toArray方法. toArray public <T> T[] toArray(T[] ...
- hibernate关于多对多注解配置
Game实体类配置关系 @Entity @Table(name = "game") public class Game { @Id @GeneratedValue private ...
- WPF特效-绘图
原文:WPF特效-绘图 WPF玩起来还是挺炫酷的.我实现的效果:不同色块交叉,交叉部分颜色叠加显示.(叠加部分暂时用随机颜色代替).单独色块点击弹出以色块颜色为主的附 ...
- Html 空格与换行
空格 换行 <br/> 调行距 <div style="line-height:10px"></div>
- MVC 行为过滤器
using FilterExam.Fiter;using System;using System.Collections.Generic;using System.Linq;using System. ...
- BGP路由的手动汇总
网络拓扑 XRV1 ========================================================== !hostname XRV1!interface Loopba ...
- PySide——Python图形化界面入门教程(二)
PySide——Python图形化界面入门教程(二) ——交互Widget和布局容器 ——Interactive Widgets and Layout Containers 翻译自:http://py ...
- textblock的LineHeight的调整
原文:textblock的LineHeight的调整 <TextBlock Width="113.594" Height="73.667" Text=&q ...
- 《芒果TV》UWP版利用Windows10通用平台特性,率先支持Xbox One平台
在Windows开发者中心开放提交Xbox平台应用之后,<芒果TV>UWP版迅速更新v3.1.2版,通过升级兼容目标,利用Windows10通用平台特性,率先覆盖Xbox平台用户. 芒果T ...
- 腾讯QQ 8.9.3体验版发布 在线文档多端同步实时保存
感谢N软网的投递 腾讯体验中心迎来QQ8.9.3首个维护体验版发布,详细版本号为v8.9.3.21006,上一个体验版v8.9.2.20717发布于4月20日,时隔34天又迎来了更新.本次升级主要是在 ...