zoukankan      html  css  js  c++  java
  • CF781A 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 ab and c are distinct squares that aand 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!

    Input

    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.

    Output

    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.

    Examples
    input
    3
    2 3
    1 3
    output
    3
    1 3 2
    input
    5
    2 3
    5 3
    4 3
    1 3
    output
    5
    1 3 2 5 4
    input
    5
    2 1
    3 2
    4 3
    5 4
    output
    3
    1 2 3 1 2

    大意是说给一棵无根树染色,相邻的三个节点颜色不能相同,问至少需要多少种颜色。

    思路:

    有趣的搜索。

    实现:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <vector>
     4 using namespace std;
     5 
     6 vector<int> G[200005];
     7 int color[200005];
     8 int ans = 1;
     9 void dfs(int now, int f)
    10 {
    11     int x = 0;
    12     for (int i = 0; i < G[now].size(); i++)
    13     {
    14         if (G[now][i] == f || color[G[now][i]])
    15             continue;
    16         x++;
    17         while (x == color[now] || x == color[f])
    18             x++;
    19         color[G[now][i]] = x;
    20         dfs(G[now][i], now);
    21     }
    22     if (x > ans)
    23         ans = x;
    24 }
    25 int n, x, y;
    26 int main()
    27 {
    28     cin >> n;
    29     for (int i = 1; i <= n - 1; i++)
    30     {
    31         cin >> x >> y;
    32         G[x].push_back(y);
    33         G[y].push_back(x);
    34     }
    35     color[1] = 1;
    36     dfs(1, 0);
    37     cout << ans << endl;
    38     for (int i = 1; i <= n; i++)
    39     {
    40         cout << color[i] << " ";
    41     }
    42     cout << endl;
    43     return 0;
    44 }
  • 相关阅读:
    【转】你必须知道的EF知识和经验
    【转】ASP.NET MVC中错误日志信息记录
    js获取当前iframe的ID
    【转】Code First 属性详解
    【转】MVC中code first方式开发,数据库的生成与更新(Ef6)
    【转】MVC form提交实体接收参数时空字符串值变成null
    【转】别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework
    Jquery中.attr()和.data()的区别
    关于在用curl函数post网页数据时,遇上表单提交 type为submit 类型而且没有name和id时可能遇到的问题及其解决方法
    浅谈 php 采用curl 函数库获取网页 cookie 和 带着cookie去访问 网页的方法!!!!
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6516551.html
Copyright © 2011-2022 走看看