Codeforces Round #411 E.Ice cream coloring (dfs染色)

E.Ice cream coloring

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

题意:有一颗树 有n个结点和n-1条边,每个结点有s个冰淇淋,同一个结点的冰淇淋相邻。(完全图)
现在要给这些冰淇淋染色,相邻的不能同色,问最少要多少个颜色,怎么染。
题目还给出一个条件,相同冰淇淋的结点在一个连通块中,所以类似于
3 3
2 1 2
2 2 3
2 1 3
1 2
2 3
这样的数据是不符合题意的。

分析:
根据题目描述建树,然后逐个结点dfs,因为有冰淇淋相同就有连边的条件
所以如果前面确定了 第i种冰淇淋的涂色的话后面就涂一样的颜色就可以啦
那么当遍历到点v的时候 可以利用像博弈论找sg的方法,去寻找那个离0最近且当前结点不纯在的颜色,涂再没有涂色的冰淇淋上就可以了。

这里还要注意 没有出现的结点也要涂色,涂1就可以了。

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
vector<int>v1[350000],v2[350000];
map<int,int>q;
int col[350000];
void dfs(int now,int pre)
{
    q.clear();
    for(int i=0;i<v1[now].size();i++)
    {
        if(col[v1[now][i]]) 
        q[col[v1[now][i]]]=1;
    }
    int t=0;
    for(int i=0;i<v1[now].size();i++)
    {
        if(col[v1[now][i]])
        continue;
        while(q[++t]){}
        col[v1[now][i]]=t;
    }
    for(int i=0;i<v2[now].size();i++)
    {
        if(v2[now][i]!=pre)
        dfs(v2[now][i],now);
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int ans=0;
    if(m)
    ans=1;
    memset(col,0,sizeof(col));
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        ans=max(ans,x);
        for(int j=0;j<x;j++)
        {
            int a;
            scanf("%d",&a);
            v1[i].push_back(a);
        }
    }
    for(int i=1;i<n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        v2[a].push_back(b);
        v2[b].push_back(a);
    }
    dfs(1,-1);
    printf("%d\n",ans);
    for(int i=1;i<=m;i++)
    {
        if(col[i]!=0)
        printf("%d ",col[i]);
        else
        printf("1 ");
    }
    printf("\n");
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值