zoukankan      html  css  js  c++  java
  • cf780c

                                                                                             C. 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 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
    题意:有n个节点,有n-1条边并且保证全部联通(是一棵生成树),相连的三个节点不能同色,例如A与B相连,B与C相连,那么ABC不能同色,输出颜色的最小种类和一种解决方案。
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    vector<int> v[200010];
    int k=0;
    int ans[200010];
    void dfs(int x,int q)
    {
         int d=1;
         for(int i=0;i<v[x].size();i++)
         {
             if(ans[v[x][i]]==0)
             {
                 while(d==ans[x]||d==ans[q]) d++;
                 ans[v[x][i]]=d++;
                 k=max(ans[v[x][i]],k);
                 dfs(v[x][i],x);
             }
         }
    }
    int main()
    {
        int n,i,j,a,b;
        scanf("%d",&n);
        for(i=0; i<n-1; i++)
        {
            scanf("%d%d",&a,&b);
            v[a].push_back(b);
            v[b].push_back(a);
        }
        ans[1]=1;
        dfs(1,0);
        printf("%d
    ",k);
        for(i=1;i<=n;i++)
        {
            if(i!=1)
             printf(" %d",ans[i]);
             if(i==1)
                  printf("%d",ans[i]);
        }
        return 0;
    }

    解析:利用vector开辟动态数组,储存每个点互相链接的点,从1开始dfs遍历,并且传两个参数x,q,前者代表当前遍历的点,后者代表前一个的点,只要参考这两个点就能找的第三点的颜色。

  • 相关阅读:
    项目开发问题笔记
    HDU 1800——Flying to the Mars——————【字符串哈希】
    FZU 2122 ——又见LKity——————【KMP字符串匹配】
    FZU 2122——又见LKity——————【字符串匹配、暴力】
    POJ 3468——A Simple Problem with Integers——————【线段树区间更新, 区间查询】
    HRBUST 1909——理工门外的树——————【离线处理,差分前缀和】
    HRBUST 1161——Leyni——————【线段树单点更新,区间查询】
    用Gvim建立IDE编程环境 (Windows篇)
    FZU 2207 ——以撒的结合——————【LCA + 记录祖先】
    HDU 5635 ——LCP Array ——————【想法题】
  • 原文地址:https://www.cnblogs.com/yuanbo123/p/6544898.html
Copyright © 2011-2022 走看看