1. 程式人生 > >780C Andryusha and Colored Balloons(思路,bfs)

780C Andryusha and Colored Balloons(思路,bfs)

描述

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!

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.

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 

Note

In the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.

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

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

imgIllustration for the third sample.

思路

一棵樹有n個節點,你需要給它的所有節點圖上顏色。相鄰三個節點不能有相同顏色,問最少需要多少種顏色。
相鄰三個節點的意思是 節點x1與x2相鄰,x2與x3相鄰,那麼x1與x3也相鄰, x1,x2,x3顏色各不相同

我們容易推得,顏色的個數就是所有點的度數最大的度數+1.

使用bfs,每次從1開始塗色,把自己的父親,和祖父標記一下,如果是自己的父親或者祖父就換顏色,直到不是為止.

程式碼

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> e[N];
int vis[N], color[N], in[N], tot = 0;
int fmax(int a, int b, int c)
{
    return max(max(a, b), c);
}
void bfs(int u, int fa)
{

    int num = 1;
    for (auto v : e[u])
    {
        if (v == fa)
            continue;
        while (num == color[u] || num == color[fa])
            num++;
        color[v] = num++;
    }
    for (auto v : e[u])
    {
        if (v == fa)
            continue;
        bfs(v, u);
    }
}
int main()
{
    int n, u, v;
    scanf("%d", &n);
    for (int i = 1; i <= n - 1; i++)
    {
        scanf("%d%d", &u, &v);
        in[u]++, in[v]++;
        tot = fmax(tot, in[u], in[v]);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    color[1] = 1;
    bfs(1, 0);
    tot++;
    printf("%d\n", tot);
    for (int i = 1; i <= n; i++)
        printf("%d ", color[i]);
    puts("");
    return 0;
}

相關推薦

780C Andryusha and Colored Balloons(思路bfs)

描述 Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate

codeforces 781A Andryusha and Colored Balloons(樹上dfs)

 Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabyt

【CF659F】Polycarp and Hay(並查集bfs

jar 上下 等於 b+ ios str %d class style 題意: 構造一個矩陣,使得: 矩陣所有格子中數字都小於等於原矩陣,並且至少有一個元素和原矩陣相等, 構造的矩陣除了0以外的數字必須聯通並且相等,矩陣中元素之和為K。 n,m<=1e3,1<=

[uvalive 7263] Today Is a Rainy Day(暴力BFSdp)

預處理 ini pen sta 現在 修改 ace printf main 題目鏈接:https://vjudge.net/problem/UVALive-7263 題意:給兩個字符串a,b,只包含1~6的數字,現在允許兩種操作:1、修改某一位數字,2、修改整個串的某個數字

python+unittest框架整理(一點點學習前輩們的封裝思路一點點成長。。。)

spa star 世界 ner bytes odi splay 好玩 etime 預期框架整理目標: 1.單個用例維護在單個.py文件中可單個執行,也可批量生成組件批量執行 2.對定位參數,定位方法,業務功能腳本,用例腳本,用例批量執行腳本,常用常量進行分層獨立,各自維護在

勝利大逃亡bfs廣度優先搜索

地圖 cin 策略 mark 時間 -1 bfs 一個 bool 題目描述: Ignatius被魔王抓走了,有一天魔王出差去了,這可是Ignatius逃亡的好機會.魔王住在一個城堡裏,城堡是一個A*B*C的立方體,可以被表示成A個B*C的矩陣,剛開始Ignatius被關在(

POJ 1426 Find The Multiple(DFSBFS

ons pro sum 數字 there lin queue hat 一個數 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal

HDU 1043 Eight八數碼解題思路bfs+hash 打表 IDA* 等)

中間 節點 sca 技巧 length div clu 鏈接 output 題目鏈接 https://vjudge.net/problem/HDU-1043 經典的八數碼問題,學過算法的老哥都會拿它練搜索 題意: 給出每行一組的數據,每組數據代表3*3的八數碼表,要求程序復

1305 Pairwise Sum and Divide(數學 規律)

sum content %d sort bar mco retext 但是 clu HackerRank 1305 Pairwise Sum and Divide 有這樣一段程序,fun會對整數數組A進行求值,其中Floor表

CodeForces 862E Mahmoud and Ehab and the function 暴力二分

push_back fin style name https img print ret esp CodeForces 862E 題意: 給出長度為 n 的數組 a[] 和長度為 m 的數組 b[],有 q 個詢問,每次詢問把區間標號 [l,r] 的 a[] 加

HDFS設計思路HDFS使用查看集群狀態HDFSHDFS上傳文件HDFS下載文件yarn web管理界面信息查看運行一個mapreduce程序mapreduce的demo

b2c 數據系統 set 打包 value map mode format drive 26 集群使用初步 HDFS的設計思路 l 設計思想 分而治之:將大文件、大批量文件,分布式存放在大量服務器上,以便於采取分而治之的方式對海量數據進行運算分析; l 在大數據系

CF 815 A. Karen and Game(暴力模擬)

技術分享 multipl ext single tput names example 輸出 初始 題目鏈接:http://codeforces.com/problemset/problem/815/A 題目: On the way to school, Karen

“鏈”上雄安雄安新區城市發展新思路未來區塊鏈第一城?

部門 架構 發展 sin 高效 支持 廣泛 距離 建設 日前,歐洲金融工具市場指令(MiFiD II)立法設計師兼歐洲議會會員Kay Swinburne在接受外媒Business Insider采訪時表示,如果倫敦金融城還想繼續成為歐洲金融科技中心的話,就應該擁抱區塊鏈技術

判斷圖連通的三種方法——dfsbfs並查集

題目 pan closed 節點 out esp cli div find Description 如果無向圖G每對頂點v和w都有從v到w的路徑,那麽稱無向圖G是連通的。現在給定一張無向圖,判斷它是否是連通的。 Input 第一行有2個整數n和m(0 <

365. Water and Jug Problem (GCD or BFS) TBC

code pos emma get out tin from tip class https://leetcode.com/problems/water-and-jug-problem/description/ -- 365 There are two methods to

求一個整數數組中和最大的連續子數組例如:[1, 2, -4, 4, 10, -3, 4, -5, 1]的最大連續子數組是[4, 10, -3, 4](需寫明思路並編程實現)

class col code pan IT [] 例如 exit arr $arr = [ 1 , 2 , -4 , 4 , 10 , -23 , 4 , -5 , 1]; $max_sum = 0; $sum=0; $new = []; $i =

HDU 1006 Tick and Tick(時鐘分鐘秒鐘角度問題)

c++ for each 垃圾 script con names clock his ike 傳送門: http://acm.hdu.edu.cn/showproblem.php?pid=1006 Tick and Tick Time Limit: 2000/1000 MS

(如數據結構算法題)編程不是難在處理問題的思路而在用代碼將思路描述出來。

思維 正常 一次 cte -- 習慣 加減 想要 not 初學編程總覺得好簡單,不就是加減乘除,或者什麽,思路太簡單了,就這樣這樣做就可以了,代碼就不用寫了,反正這麽簡單。 其實難就難在,如何用代碼這種語言將你的思路書寫下來。 寫代碼會受到很多的限制,你想要這樣幹,但你要如

POJ 3126 Prime Path(素數BFS最短路)

【連結】http://poj.org/problem?id=3126 【題意】給個字串,求滿足形如“E...E...E”這種鬼樣子的E串最大長度,其中“...”可以是任意個數的任意字元(沒有也ok),但是三個E之間不能重疊 【思路】  ① 掏出素數篩的模板,找到1W以內素數

【CodeForces - 589F】Gourmet and Banquet (貪心思維二分)

題幹: A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet knows the schedule: when each of th