http://codeforces.com/contest/782/problem/C

题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色。

思路:比赛的时候没想到是找度最大的一个点并+1就是总颜色数,一直想怎么构造。

最后的总颜色数是度最大的一个点的度数+1。因为我们选的这个点到儿子的距离为1,因此其某一个儿子到另一个儿子的距离为2,就是这些儿子都要染成不同的颜色,+1是因为自己本身也要是不同的颜色。

然后确定了总颜色数,就可以DFS染色了。

参数记录一个上上个结点和上个结点染了什么颜色,只要儿子染成不同的就可以了,注意儿子之间颜色也要不同。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define N 200010
  4. struct Edge {
  5. int v, nxt;
  6. } edge[N*];
  7. int head[N], tot, col[N], deg[N], ans;
  8.  
  9. void Add(int u, int v) {
  10. edge[tot] = (Edge) {v, head[u]}; head[u] = tot++;
  11. edge[tot] = (Edge) {u, head[v]}; head[v] = tot++;
  12. }
  13.  
  14. void dfs(int u, int fa, int c1, int c2) {
  15. int c = ;
  16. for(int i = head[u]; ~i; i = edge[i].nxt) {
  17. int v = edge[i].v;
  18. if(v == fa) continue;
  19. if(col[v]) continue;
  20. for( ; c <= ans; c++)
  21. if(c != c1 && c != c2) {
  22. col[v] = c; break;
  23. }
  24. c++;
  25. dfs(v, u, col[v], col[u]);
  26. }
  27. }
  28.  
  29. int main() {
  30. int n;
  31. scanf("%d", &n);
  32. memset(head, -, sizeof(head));
  33. for(int i = ; i < n; i++) {
  34. int u, v;
  35. scanf("%d%d", &u, &v);
  36. deg[u]++; deg[v]++;
  37. Add(u, v);
  38. }
  39. ans = ;
  40. for(int i = ; i <= n; i++) if(ans < deg[i] + ) ans = deg[i] + ;
  41.  
  42. col[] = ;
  43. dfs(, -, , -);
  44. printf("%d\n", ans);
  45. for(int i = ; i <= n; i++) printf("%d ", col[i]);
  46. return ;
  47. }

Codeforces 781A:Andryusha and Colored Balloons(DFS染色)的更多相关文章

  1. Codeforces 782C. Andryusha and Colored Balloons 搜索

    C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...

  2. CodeForces - 780C Andryusha and Colored Balloons(dfs染色)

    Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, ...

  3. 782C. Andryusha and Colored Balloons DFS

    Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...

  4. 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 ...

  5. AC日记——Andryusha and Colored Balloons codeforces 780c

    C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...

  6. 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 ...

  7. codeforces781A Andryusha and Colored Balloons

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. C. Andryusha and Colored Balloons

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

随机推荐

  1. Android数组和开发List之间的转换

    1.List转换到一个数组.(这里List它是实体是ArrayList) 转让ArrayList的toArray方法. toArray public <T> T[] toArray(T[] ...

  2. hibernate关于多对多注解配置

    Game实体类配置关系 @Entity @Table(name = "game") public class Game { @Id @GeneratedValue private ...

  3. WPF特效-绘图

    原文:WPF特效-绘图                  WPF玩起来还是挺炫酷的.我实现的效果:不同色块交叉,交叉部分颜色叠加显示.(叠加部分暂时用随机颜色代替).单独色块点击弹出以色块颜色为主的附 ...

  4. Html 空格与换行

    空格   换行 <br/>   调行距 <div style="line-height:10px"></div>

  5. MVC 行为过滤器

    using FilterExam.Fiter;using System;using System.Collections.Generic;using System.Linq;using System. ...

  6. BGP路由的手动汇总

    网络拓扑 XRV1 ========================================================== !hostname XRV1!interface Loopba ...

  7. PySide——Python图形化界面入门教程(二)

    PySide——Python图形化界面入门教程(二) ——交互Widget和布局容器 ——Interactive Widgets and Layout Containers 翻译自:http://py ...

  8. textblock的LineHeight的调整

    原文:textblock的LineHeight的调整 <TextBlock Width="113.594" Height="73.667" Text=&q ...

  9. 《芒果TV》UWP版利用Windows10通用平台特性,率先支持Xbox One平台

    在Windows开发者中心开放提交Xbox平台应用之后,<芒果TV>UWP版迅速更新v3.1.2版,通过升级兼容目标,利用Windows10通用平台特性,率先覆盖Xbox平台用户. 芒果T ...

  10. 腾讯QQ 8.9.3体验版发布 在线文档多端同步实时保存

    感谢N软网的投递 腾讯体验中心迎来QQ8.9.3首个维护体验版发布,详细版本号为v8.9.3.21006,上一个体验版v8.9.2.20717发布于4月20日,时隔34天又迎来了更新.本次升级主要是在 ...