codeforces 804B——Ice cream coloring(图论,dfs,数据结构,好题)

46 篇文章 1 订阅
22 篇文章 0 订阅

C. 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 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.


第一个难点在于理解题意。有一句话很巧妙, Vertices which have the  i -th ( 1 ≤ i ≤ m ) type of ice cream form a connected subgraph. 也就是说在原来的图中,含有相同的冰淇淋的点是一个 联通子图。那么比如1,2在一个点,1,3在一个点,那么2,3便不可能在另一个点了,因为那样将会形成一个环,也就是说这个图不再是树了。

这有什么用呢,对于树上的每个节点,节点内的冰淇淋形成的都是完全图,而任何两个节点之间不用考虑会冲突的问题,直接从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");
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值