直觉,构造。

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

#include <cstdio>
#include <cmath>
#include <set>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; int n,m;
const int maxn = 300010;
vector<int>g[maxn];
vector<int>d[maxn];
int f[maxn];
int ans[maxn],sz; int t[maxn]; void dfs(int x)
{
f[x]=1; int now = 1;
for(int i=0;i<d[x].size();i++)
{
int col = d[x][i];
t[ans[col]]=1;
} for(int i=0;i<d[x].size();i++)
{
int col = d[x][i];
if(ans[col]) continue; while(1)
{
if(t[now]) now++;
else
{
ans[col] = now;
t[now]=1;
now++;
break;
}
} } for(int i=0;i<d[x].size();i++)
{
int col = d[x][i];
t[ans[col]]=0;
} for(int i=0;i<g[x].size();i++)
{
int to = g[x][i];
if(f[to]) continue;
dfs(to);
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
int sz; scanf("%d",&sz);
while(sz--)
{
int w; scanf("%d",&w);
d[i].push_back(w);
}
} for(int i=1;i<n;i++)
{
int u,v; scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
} dfs(1); for(int i=1;i<=m;i++)
{
if(ans[i]==0) ans[i] = 1;
} for(int i=1;i<=m;i++) sz = max(sz,ans[i]);
printf("%d\n",sz);
for(int i=1;i<=m;i++) printf("%d ",ans[i]);
printf("\n"); return 0;
}

CodeForces 805E Ice cream coloring的更多相关文章

  1. CodeForces 804C Ice cream coloring

    Ice cream coloring 题解: 这个题目中最关键的一句话是, 把任意一种类型的冰激凌所在的所有节点拿下来之后,这些节点是一个连通图(树). 所以就不会存在多个set+起来之后是一个新的完 ...

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

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

  3. 【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 ...

  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. bootstrap 栅格calss

    container container-fluid row col-xs- col-sm- col-md- col-lg- col-md-offset- col-md-push- col-md-pul ...

  2. OpenCV---图像直方图

    一:直方图的直接使用 from matplotlib import pyplot as plt def plot_demo(image): print(image.ravel()) plt.hist( ...

  3. ios 字符串截取

    NSString *str = @"my name is jiemu"; 1.从第三个字符开始,截取长度为4的字符串 NSString *str2 = [str substring ...

  4. [Thu Summer Camp 2015]解密运算

    4104: [Thu Summer Camp 2015]解密运算 Time Limit: 10 Sec  Memory Limit: 512 MB Description 对于一个长度为N的字符串,我 ...

  5. 2015/9/21 Python基础(17):绑定和方法调用

    绑定和方法调用现在我们需要再次阐述Python中绑定(binding)的概念,它主要与方法调用相关联.方法是类内部定义的函数,这意味着方法是类属性而不是实例属性.其次,方法只有在其所属的类拥有实例时, ...

  6. Flex用HTTPService调用servlet返回中文乱码解决

    servlet中使用URLEncoder.encode对输出内容进行编码 Flex中使用decodeURIComponent进行解码

  7. LintCode 394: First Will Win

    LintCode 394: First Will Win 题目描述 有n个硬币排成一条线.两个参赛者轮流从右边依次拿走1或2个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是 ...

  8. 【CodeForces】915 F. Imbalance Value of a Tree 并查集

    [题目]F. Imbalance Value of a Tree [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...

  9. 74.VS2013和opencv3.1.0安装教程

    一.先下载文件 1.VS2013 VS2013有很多版本,专业版,旗舰版,中文英文之类的,所对应的密钥也不一样.我选择的是简体中文专业版.下载链接如下. http://www.musnow.com/t ...

  10. VS2015_动态链接库学习

    非MFC动态链接库 创建一个名为ex1的Win32项目 创建一个DLL项目,保留预编译的头文件   默认文件 创建完成项目之后,包含几个默认的文件   stdafx.h文件用于包含标准系统包含的头文件 ...