Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons

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

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

Examples
input
3
2 3
1 3
output
3
1 3 2 
題意:給一棵樹,用最少的顏色上色,使得任意滿足a連接b,b連接c,的三點顏色不同,打印顏色種類,及每個點的上色方案。

思路:樹,想着用分層搜索的方式,開始想着用bfs,每個點的顏色是除之前搜索過兩層顏色外同層中最小可以獲取的

顏色。後來覺得bfs記錄的東西太多,用dfs代替,新思路是當前節點顏色是除父親節的,祖父節點顏色外,

當前深度(樹的深度,其實也是層次)下最小可以獲取的顏色。但比賽時候提交wa了,後來想明白了,

不能是當前深度,應該是同一父親節點的當前深度,否則公共祖先不是父親節點顏色是沒有要求的,按照

要求處理就會錯。

總之最終思路:新思路是當前節點顏色是除父親節,祖父節點顏色外,當前節點的父節點下的當前未使用顏色的

最小值。

代碼有註釋,看一下就懂了:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
const int N = 200010;
using namespace std;
typedef long long ll;
int ans, n;
int color[N];
int cid[N];//記錄當前節點的兒子中最小可以使用的顏色值
vector<int> G[N];
void dfs(int u, int fa, int ffa)
{
    int fcol = 0, ffcol = 0;
    if (fa != 0)
        fcol = color[fa];
    if (ffa != 0)
        ffcol = color[ffa];

    if (cid[fa] == 0)
        cid[fa]++;

    if (cid[fa] == fcol)
        cid[fa]++;

    if (cid[fa] == ffcol)
        cid[fa]++;

    if (cid[fa] == fcol)//多寫一遍,因爲存在祖父節點顏色加1又跟父節點顏色衝突的情況
        cid[fa]++;


    color[u] = cid[fa];
    ans = max(ans, cid[fa]);

    cid[fa]++;
    for (int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if (v != fa)
            dfs(v, u, fa);
    }
}

int main()
{
#ifndef ONLINE_JUDGE  
    freopen("in.txt","r" ,stdin);
#endif 
   
    while (~scanf("%d", &n))
    {
       memset(cid, 0, sizeof(cid));
       ans = 0;
       for (int i = 1; i <= n; i++)G[i].clear();
        for (int i = 1; i < n; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        dfs(1, 0, 0);
        printf("%d\n", ans);
        for (int i = 1; i <= n; i++)
            printf("%d%c", color[i], i == n? '\n': ' ');
    }
    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章