zoukankan      html  css  js  c++  java
  • codeforces 782C 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 
    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 
    题意是给你一棵树,让你给树图上颜色。树上距离为3 的点要求颜色不同。
    从树的祖先开始涂色,祖先和树高是2 的涂上不同的颜色。对于树高为3或者更加靠后的,它的颜色只与它的兄弟和父亲节点和爷爷节点有关。
    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    //typedef long long LL;
    //typedef __int64 Int;
    typedef pair<int, int> PAI;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-6;
    const double PI = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = 200000 + 10;
    int par[MAXN];
    bool vis[MAXN];
    int clor[MAXN];
    vector<int> G[MAXN];
    bool cvis[MAXN];
    bool flag;
    int ans;
    struct {
        int f, g;
    }v[MAXN];
    void init() {
        memset(vis, false, sizeof(vis));
        memset(cvis, false, sizeof(cvis));
        for (int i = 0; i < MAXN; i++) G[i].clear();
    }
    void dfs_tree(int x) {
        cvis[x] = true;
        for (int i = 0; i < G[x].size(); i++) {
            int son = G[x][i];
            if (cvis[son]) continue;
            v[son].f = x;
            for (int j = 0; j < G[son].size(); j++) {
                int gs = G[son][j];
                if (cvis[gs]) continue;
                v[gs].f = son;
                v[gs].g = x;
            }
            dfs_tree(son);
        }
    }
    void init_tree(int x) {
        clor[x] = 1; vis[x] = true;
        for (int j = 0; j < G[x].size(); j++) {
            int t = G[x][j];
            clor[t] = j + 2;
            vis[t] = true;
        }
        int maxx = G[x].size() + 1;
        ans = max(ans, maxx);
    }
    void dfs(int x) {
        int cnt = 0;
        vis[x] = true;
        for (int i = 0; i < G[x].size(); i++) {
            int son = G[x][i];
            if (son == v[x].f) continue;
            if (!vis[son]) {
                while (++cnt) {
                    if (cnt != clor[v[son].f] && cnt != clor[v[son].g]) {
                        clor[son] = cnt; ans = max(cnt, ans);
                        break;
                    }
                }
            }
            dfs(son);
        }
    }
    int main() {
        int N, x, y;
        while (scanf("%d", &N) != EOF) {
            init();
            for (int i = 0; i < N - 1; i++) {
                scanf("%d%d", &x, &y);
                G[x].push_back(y);
                G[y].push_back(x);
            }
            ans = 0;
            vis[1] = true;
            init_tree(1);
            dfs_tree(1);
            dfs(1);
            printf("%d
    ", ans);
            printf("%d", clor[1]);
            for (int i = 2; i <= N; i++) {
                printf(" %d", clor[i]);
            }
            printf("
    ");
        }
        return 0;
    }

  • 相关阅读:
    allocator类
    智能指针shared_ptr
    字面值常量类
    转换构造函数
    委托构造函数
    访问说明符&封装
    const成员函数
    函数指针
    constexper和常量表达式
    函数返回数组指针
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770744.html
Copyright © 2011-2022 走看看