题解:

这个题目中最关键的一句话是, 把任意一种类型的冰激凌所在的所有节点拿下来之后,这些节点是一个连通图(树)。

所以就不会存在多个set+起来之后是一个新的完全图。

所以只要直接去做就好了。

对于每个节点来说,染色。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 3e5 + ;
vector<int> vc[N], e[N];
int col[N];
int vis[N];
int mx = ;
void dfs(int o, int u){
for(int v : vc[u]){
vis[col[v]]++;
}
int b = ;
for(int v : vc[u]){
if(col[v]) continue;
while(vis[b]) ++b;
vis[b] = ;
col[v] = b;
}
mx = max(mx, b);
for(int v : vc[u]){
vis[col[v]] = ;
}
for(int v : e[u]){
if(v == o) continue;
dfs(u, v);
}
}
int main(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i){
int si, tv;
scanf("%d", &si);
for(int j = ; j <= si; ++j){
scanf("%d", &tv);
vc[i].pb(tv);
}
}
int u, v;
for(int i = ; i < n; ++i){
scanf("%d%d", &u, &v);
e[u].pb(v); e[v].pb(u);
}
dfs(, );
printf("%d\n", mx);
for(int i = ; i <= m; ++i){
if(!col[i]) col[i] = ;
printf("%d%c", col[i], " \n"[i==m]);
}
return ;
}

CodeForces 804C Ice cream coloring的更多相关文章

  1. 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)

    传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...

  2. 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring

    http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...

  3. CodeForces 805E Ice cream coloring

    直觉,构造. 画了几个样例,发现可以随便构造......先构造根节点的完全图,每个点置为不同的颜色,然后构造儿子节点的完全图...... #include <cstdio> #includ ...

  4. codeforces 805 E. Ice cream coloring(dfs)

    题目链接:http://codeforces.com/contest/805/problem/E 题意:你有n个节点,这个n个节点构成一棵树.每个节点拥有有si个类型的ice,同一个节点的ice互相连 ...

  5. 【DFS】【贪心】Codeforces Round #411 (Div. 1) C. Ice cream coloring

    对那个树进行dfs,在动态维护那个当前的冰激凌集合的时候,显然某种冰激凌仅会进出集合各一次(因为在树上形成连通块). 于是显然可以对当前的冰激凌集合贪心染色.暴力去维护即可.具体实现看代码.map不必 ...

  6. CodeForces 686A-Free Ice Cream

    题目: 儿童排队领冰激凌,给你两个数n,x分别代表接下来有n行与初始的冰激淋数:接下来n行,每行有一个字符('+'or‘-’),还有一个整数d,+d表示新增的冰激 凌数(由搬运工搬运到此),-d表示儿 ...

  7. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  8. codeforces 686A A. Free Ice Cream(水题)

    题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...

  9. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

随机推荐

  1. 【iOS】Masonry 自动布局 MASViewConstraint.m:207 错误

    问题详情: Assertion failure 报错原因: make.right.equalTo([_imageView superview]).right.with.offset(-); make. ...

  2. superset安装文档

    1 安装python3.6 yum install epel-release -y yum install https://centos7.iuscommunity.org/ius-release.r ...

  3. poj 1068 模拟

    题目链接 大概题意就是告诉你有个n个小括号,每一个")"左边有多少个"("都告诉你了,然后让你求出每一对括号之间有多少对括号(包含自己本身). 思路: 我先计算 ...

  4. Currency Exchange POJ1860

    Description Several currency exchange points are working in our city. Let us suppose that each point ...

  5. 夯实Java基础(五)——==与equals()

    1.前言 我们在学习Java的时候,看到==.equals()就认为比较简单,随便看了一眼就过了,其实你并没有深入去了解二者的区别.这个问题在面试的时候出现的频率比较高,而且据统计有85%的人理直气壮 ...

  6. MyBatis 核心配置综述之 ParameterHandler

    目录 ParameterHandler 简介 ParameterHandler 创建 ParameterHandler 中的参数从何而来 ParameterHandler 解析 MyBatis 四大核 ...

  7. 【游记】NOIP2019前传

    声明 我的游记是一个完整的体系,如果没有阅读过往届文章,阅读可能会受到障碍. ~~~上一篇游记的传送门~~~ 前言 比完赛后,我沉浸在胜利中长达半个月,而后才清醒过来,意识到自己需要为NOIP2019 ...

  8. springboot-jsp打jar问题

    [**前情提要**]最近做了一个项目,项目是springboot+jsp结构的,但是在发布生产环境的时候又需要用maven打成jar包,但是一开始的默认配置都不成功.下面的文章就是具体的解决过程. - ...

  9. 使用webstorm搭建vue-cli项目

    前言 随着vue在前端不断的壮大,越来越多的前端工程师使用vue了,作为大型项目的开发,vue-cli是不二之选,所以这篇博客是为搭建vue-cli所写,想要搭建vue-cli项目就必须先有git,n ...

  10. ccf 201903-5 317号子任务(60分)

    看到这题,第一印象,用dijkstra算法求n次单源最短路,时间复杂度O(n^3),超时30分妥妥的. 于是用优先队列优化,O(n*mlogm),快很多,但依然30. 那么不妨换一种思路,题目要求的是 ...