E. Ice cream coloring (图论 染色 DFS)

来源:互联网 发布:highcharts.js 下载 编辑:程序博客网 时间:2024/04/28 23:46
E. Ice cream coloring
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a connected subgraph. We build a new graph G with m vertices. We put an edge between the v-th and the u-th (1 ≤ u, v ≤ mu ≠ v) vertices in G if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set. The problem is to paint the vertices of G 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.

Input

The first line contains two integer n and m (1 ≤ n, m ≤ 3·105) — the number of vertices in T and the number of ice cream types.

n lines follow, the i-th of these lines contain single integer si (0 ≤ si ≤ 3·105) and then si distinct integers, each between 1 and m — the types of ice cream in the i-th vertex. The sum of si doesn't exceed 5·105.

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

Output

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

In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.

Examples
input
3 31 12 2 31 21 22 3
output
21 1 2 
input
4 501 11 33 2 4 52 13 24 3
output
31 1 1 2 3 
Note

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.


这道题题意就是:含有n个节点的树,每个节点含有si个冰淇凌,这si个冰淇凌构成一个完全图,这个完全图的各个冰淇凌的颜色不能相同,也就是说相邻的冰淇凌颜色不同。问最少需要几种颜色可以将G染色。


#include<bits/stdc++.h>#define MAXN 400000using namespace std;int n,m;set<int> vertic[MAXN];  //保存第i个节点的冰淇凌种类 vector<int> Geogra[MAXN];  //保存图G的联通情况 set<int>::iterator it;int color[MAXN];int ans=0;void dfs(int pos,int pre){int i,j;if(pre==-1){for(it=vertic[pos].begin();it!=vertic[pos].end();it++)color[(*it)]=++ans;}else{vector<int> used;for(it=vertic[pos].begin();it!=vertic[pos].end();it++)if(color[(*it)])used.push_back(color[*it]); int dis=0,now=1;int len=used.size();sort(used.begin(),used.end());for(it=vertic[pos].begin();it!=vertic[pos].end();it++){if(color[*it]==0){while(dis<len){if(used[dis]==now) now++,dis++;  //这是个技巧,先排序,然后对比 elsebreak;}color[*it]=now++;ans=max(ans,now-1);}}}for(i=0;i<Geogra[pos].size();i++){if(Geogra[pos][i]!=pre)dfs(Geogra[pos][i],pos);}}int main(){scanf("%d %d",&n,&m);int i,j;for(i=1;i<=n;i++){//ertic[i].clear();int temp;scanf("%d",&temp);for(j=0;j<temp;j++){int s;scanf("%d",&s);vertic[i].insert(s);} }   for(i=1;i<n;i++) { int u,v; scanf("%d %d",&u,&v); Geogra[u].push_back(v); Geogra[v].push_back(u); } memset(color,0,sizeof(color)) ; ans=0; dfs(1,-1); ans=max(1,ans); printf("%d\n",ans); for(i=1;i<=m;i++) { if(color[i]==0) printf("1 ");  //这个if就是为了下面这个例子 /*3 30001 22 3*/  else  printf("%d ",color[i]);} printf("\n"); return 0; }