CodeForces - 805E - Ice cream coloring dfs+贪心

题意:有一棵具有 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';
    else
        cout << 1 << '\n';
    dfs(1, 1);
    for (int i = 1; i <= m; ++i)
        if(color[i])
            printf("%d ", color[i]);
        else
            printf("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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值