这有什么用呢,对于树上的每个节点,节点内的冰淇淋形成的都是完全图,而任何两个节点之间不用考虑会冲突的问题,直接从1到s染色即可。暴力dfs一下。
#include<cstdio>
#include<cstring>
#include<queue>
#include <cstdlib>
#include<map>
#include <set>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
#define _ std::ios::sync_with_stdio(false)
const int MAXN = 300010;
long long INF = 0x7fffffffffffffff;
const long long MODE = 1e9 + 7;
int n, m;
vector<int> s[MAXN];
int num[MAXN];
int head[MAXN];
int tot = 0;
struct edge {
int v, next;
}e[MAXN<<1];
void add(int u, int v) {
e[tot].v = v;
e[tot].next = head[u];
head[u] = tot++;
}
int c[MAXN];
int res = 1;
void dfs(int u,int pre) {
res = max(num[u], res);
int temp = 0;
set<int> t;
t.clear();
for (int i = 0; i < num[u]; i++) {
int x = s[u][i];
if (c[x]) {
t.insert(c[x]);
}
}
for (int i = 0; i < num[u]; i++) {
int x = s[u][i];
if (!c[x]) {
temp++;
while (t.count(temp)) {
temp++;
}
c[x] = temp;
}
}
for (int k = head[u]; k != -1; k = e[k].next) {
int v = e[k].v;
if (v != pre)
dfs(v, u);
}
}
int main() {
_;
memset(head, -1, sizeof(head));
memset(c, 0, sizeof(c));
cin >> n >> m;
for (int i = 1; i <= n; i++) {
s[i].clear();
cin >> num[i];
for (int j = 0; j < num[i]; j++) {
int x;
cin >> x;
s[i].push_back(x);
}
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
dfs(1, 0);
for (int i = 1; i <= m; i++) {
if (!c[i])
c[i] = 1;
}
cout << res << endl;
for (int i = 1; i <= m; i++) {
if (i == m) {
cout << c[i] << endl;
}
else {
cout << c[i] << " ";
}
}
//system("pause");
}