CF804C.Ice cream coloring

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?"

We have a tree TT with nn vertices and mm types of ice cream numerated from 11 to mm . Each vertex ii has a set of sis_{i} types of ice cream. Vertices which have the ii -th ( 1<=i<=m1<=i<=m ) type of ice cream form a connected subgraph. We build a new graph GG with mm vertices. We put an edge between the vv -th and the uu -th ( 1<=u,v<=m1<=u,v<=m , uvu≠v ) vertices in GG if and only if there exists a vertex in TT that has both the vv -th and the uu -th types of ice cream in its set. The problem is to paint the vertices of GG with minimum possible number of colors in a way that no adjacent vertices have the same color.

Please note that we consider that empty set of vertices form a connected subgraph in this problem.

As usual, Modsart don't like to abandon the previous problem, so Isart wants you to solve the new problem.

输入格式

The first line contains two integer nn and mm ( 1<=n,m<=31051<=n,m<=3·10^{5} ) — the number of vertices in TT and the number of ice cream types.

nn lines follow, the ii -th of these lines contain single integer sis_{i} ( 0<=si<=31050<=s_{i}<=3·10^{5} ) and then sis_{i} distinct integers, each between 11 and mm — the types of ice cream in the ii -th vertex. The sum of sis_{i} doesn't exceed 51055·10^{5} .

n1n-1 lines follow. Each of these lines describes an edge of the tree with two integers uu and vv ( 1<=u,v<=n1<=u,v<=n ) — the indexes of connected by this edge vertices.

输出格式

Print single integer cc in the first line — the minimum number of colors to paint the vertices in graph GG .

In the second line print mm integers, the ii -th of which should be the color of the ii -th vertex. The colors should be between 11 and cc . If there are some answers, print any of them.

输入输出样例

  • 输入#1

    3 3
    1 1
    2 2 3
    1 2
    1 2
    2 3
    

    输出#1

    2
    1 1 2 
  • 输入#2

    4 5
    0
    1 1
    1 3
    3 2 4 5
    2 1
    3 2
    4 3
    

    输出#2

    3
    1 1 1 2 3 

说明/提示

In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.

In the second example the colors of the second, the fourth and the fifth ice cream should obviously be distinct.

首页