题意:有一棵具有 n 个节点的树,每个节点代表一个集合,表示集合内两两元素的颜色不同,同时保证集合中拥有某个元素的所有的节点附带着边提取出来,能够形成一个连通图,即不可能出现有三个节点 v1,v2,v3 , v1  与 v2  有树边, v2 和 v3 有树边,而 v1,v3 集合中有元素 x 而 v2 集合中没有。

思路: dfs 遍历一遍整棵树,在遍历的过程染色。由于单一元素所处的集合都相邻,那么我们可以贪心得直接给一个元素染上未出现在集合中得颜色。

ps:每次把已经染色的节点加入栈中,标记其颜色,染完集合中所有元素后,按栈弹出,取消颜色的标记。
原文:https://blog.csdn.net/a302549450/article/details/88936772

代码:

#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
#define NUM 300005
int n, m;
int num[NUM] = {};
vector<int> s[NUM];
vector<int> d[NUM];
int ans = 0, color[NUM] = {};
int head[NUM] = {}, tot = 0;
int sta1[NUM], top1 = 0, sta2[NUM], top2 = 0;
bool check[NUM] = {};
struct edge
{int to, next;
} e[NUM << 1];
void dfs(int v, int fa)
{for (int i = 0; i < num[v]; ++i){if (color[s[v][i]]){check[color[sta2[++top2] = s[v][i]]] = true;continue;}sta1[++top1] = s[v][i];}int c = 1;while (top1){while(check[c])++c;color[sta1[top1--]] = c++;}while (top2)check[color[sta2[top2--]]] = false;for (int i = head[v]; i != 0; i = e[i].next){if (e[i].to == fa)continue;dfs(e[i].to, v);}
}
int main()
{cin >> n >> m;int x, y;for (int i = 1; i <= n; ++i){scanf("%d", num + i);if (num[ans] < num[i])ans = i;for (int j = 1; j <= num[i]; ++j){scanf("%d", &x);s[i].push_back(x);}}for (int i = 1; i < n; ++i){scanf("%d%d", &x, &y);e[++tot] = edge{y, head[x]}, head[x] = tot;e[++tot] = edge{x, head[y]}, head[y] = tot;}if (num[ans])cout << num[ans] << '\n';elsecout << 1 << '\n';dfs(1, 1);for (int i = 1; i <= m; ++i)if(color[i])printf("%d ", color[i]);elseprintf("1 ");return 0;
}

题目:

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 ihas 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 Gwith m vertices. We put an edge between the v-th and the u-th (1 ≤ u, v ≤ mu ≠ v) vertices in Gif 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 sidistinct 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 3
1 1
2 2 3
1 2
1 2
2 3

Output

2
1 1 2 

Input

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

Output

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

CodeForces - 805E - Ice cream coloring dfs+贪心相关推荐

  1. 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring

    http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...

  2. Codeforces 846 B Math Show DFS + 贪心

    题目链接: http://codeforces.com/contest/846/problem/B 题目描述: 有N个节点, 每个节点有相同的K个子节点, 每个子节点有时间花费,完成一个子节点获得1分 ...

  3. Gym 101194D Ice Cream Tower

    被一道数位DP折磨得欲仙欲死之后,再做这道题真是如同吃了ice cream一样舒畅啊 #include<bits/stdc++.h> using namespace std; #defin ...

  4. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  5. C. Code a Trie(Trie+dfs+贪心)

    C. Code a Trie 大佬题解,代码基本就是抄的 对于每一个值计算所有串的LCA,也就是最长公共前缀,将该节点(Trie树的节点)标记,对于这些字符串在LCA下面的点一定不存在(如果存在他们不 ...

  6. codeforces#320(div2) D Or Game 贪心

    codeforces#320(div2) D  "Or" Game  贪心 D. "Or" Game time limit per test 2 seconds ...

  7. Android P (2)---Android 9.0 “Pistachio Ice Cream”新功能和特性

    据有关消息报道,下一版本安卓(9.0)的初步代号已经确定为"Pistachio Ice Cream"(开心果冰淇淋),并将带来极大的功能变化. 当然了按照Google的惯例,如此长 ...

  8. CodeForces - 1395D - Boboniu Chats with Du 贪心

    CodeForces - 1395D - Boboniu Chats with Du 贪心 题意:如果ai>ma_i>mai​>m,并且当天可以说话,则接下来ddd天不能说话.其余所 ...

  9. Android 9.0 “Pistachio Ice Cream”新功能和特性

    热文导读 | 点击标题阅读 一份年薪30万的Android面试宝典,附答案 吊炸天!74款APP完整源码! 一个 IT 青年北漂四年的感悟 来自:开源中国 内容整理自 快科技:http://news. ...

最新文章

  1. Android:dagger2让你爱不释手-基础依赖注入框架篇
  2. 架构设计的目标与衡量
  3. CI/CD大幅减少甩锅!
  4. jboss数据源配置
  5. spring junit单元测试
  6. java服务器和linux_在Linux下开一个Java服务器(使用CatServer Pro)
  7. 在asp.net 2.0中使用SqlBulkCopy类迁移数据
  8. php pdo mysql 乱码,php pdo连接数据库 解决中文乱码问题(wordpress mysql 问号?? ??)...
  9. 面向普通人的 PHP 加密
  10. 扎克伯格不要「脸」了?Facebook正式更名为“Meta”
  11. 在cmd命令行运行一个python脚本
  12. P63-前端基础CSS-电影卡片练习图文布局
  13. C++使用模板重载vector的加减法实现矩阵向量加减法
  14. 【window】解决word,excel,PowerPoint 等office图标不显示问题
  15. JointJS+新的MindMap应用程序
  16. CSS z-index属性层重叠顺序
  17. Jackknife 刀切法
  18. JAVAweb开发(一)javaweb概述
  19. EXP9 web安全基础实践
  20. 作为一名IT狗,天天加班,快变秃子了,我决定去植发……

热门文章

  1. C# 之 FileStream类介绍
  2. oracle主键重复报错,【诺达手札】关于Oracle 的常用命令大全
  3. 安全多方计算:安全定义
  4. wifi6速率对照表及计算方法
  5. 计算机随机存储器缩写,“RAM”是“Random Access Memory”的缩写,意思是“随机存取存储器”...
  6. 微信小程序免费SSL证书及SpringBoot服务
  7. 2022打游戏的笔记本电脑推荐哪款?来看看外星人!
  8. 用Python制作时间七段数码管
  9. web前端开发入门笔记(更新中)
  10. AI 选择、移动、对齐