Codeforces Round #411 (Div. 1) C. Ice cream coloring

C. Ice cream coloring
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ m, u ≠ 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.

对于原图:
如果任意2点有相同的雪糕,那2点之间有边
又因为原图为树,所以,任意一种雪糕,最多只可能出现2次

原图中u点有{a,b,c}雪糕
那图G中a,b,c的颜色互不相同

SO:
从原图任意一点开始dfs
对点u中的雪糕按从1开始,从小到大标颜色
对u的儿子v, v中与u相同的雪糕在u中已经被标过了,跳过,未标号的,也是从1开始标,其中要跳过u与v相同的雪糕的标号

#include<stdio.h>
#include <iostream>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<queue>
#include<math.h>
#include<string.h>
#include<string>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int inf = 1e9 + 7;

const int N = 2e6+5;

struct S{
    int type,next;
}s[N];
int sHead[N];
struct Edge{
    int to,next;
}edge[N];
int head[N];

inline void addEdge(int k,int u,int v){
    edge[k].to=v;
    edge[k].next=head[u];
    head[u]=k;
}

inline void add(int k,int u,int v){
    s[k].type=v;
    s[k].next=sHead[u];
    sHead[u]=k;
}

int col[N];
int colNum=0;
bool vis[N];
bool used[N];
int sSize[N];
void dfs(int u){
    vis[u]=true;
    int colIdx=1;
    fill(used,used+sSize[u]+1,false);
    for(int i=sHead[u];i!=-1;i=s[i].next){
        int type=s[i].type;
        if(col[type]!=-1){
            used[col[type]]=true;
        }
    }

    for(int i=sHead[u];i!=-1;i=s[i].next){
        int type=s[i].type;
        if(col[type]==-1){
            while(used[colIdx]){
                ++colIdx;
            }
            col[type]=colIdx;
            used[colIdx]=true;
        }
        colNum=max(colNum,colIdx);
    }
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(vis[v]==false){
            dfs(v);
        }
    }
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        fill(head,head+n+1,-1);
        fill(sHead,sHead+n+1,-1);
        fill(col,col+m+1,-1);
        fill(vis,vis+n+1,false);
        colNum=0;
        int nume=0;
        for(int i=1;i<=n;++i){
            int num;
            scanf("%d",&num);
            sSize[i]=num;
            while(num--){
                int type;
                scanf("%d",&type);
                add(nume++,i,type);
            }
        }
        nume=0;
        for(int i=0;i<n-1;++i){
            int u,v;
            scanf("%d%d",&u,&v);
            addEdge(nume++,u,v);
            addEdge(nume++,v,u);
        }
        for(int i=1;i<=n;++i){
            if(vis[i]==false){
                dfs(i);
            }
        }
        if(col[1]==-1){
            col[1]=1;
        }
        colNum=max(1,colNum);
        printf("%d\n%d",colNum,col[1]);
        for(int i=2;i<=m;++i){
            col[i]=max(1,col[i]);
            printf(" %d",col[i]);
        }
        putchar('\n');
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值