http://codeforces.com/contest/805/problem/E

【题意】

染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林需要1种颜色),关键是确定染色方案。

一开始没看出来树有什么用,但其实一句话很关键:Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a connected subgraph.

也就是说在原来的图中,含有相同的冰淇淋的点是一个 联通子图。那么比如1,2在一个点,1,3在一个点,那么2,3便不可能在另一个点了,因为那样将会形成一个环,也就是说这个图不再是树了。

“因为每种冰淇淋所在的所有点在这颗树上是一个连通块,也就是说,沿着树搜索下去,不可能出现已经染过色且父亲中没有的冰淇淋重新出现的问题。于是就可以直接贪心构造一波就行了。” 然而这里还是每太明白,为什么这样构造......

【Accepted】

 #include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
int n,m;
const int maxn=3e5+;
vector<int> s[maxn];
int num[maxn];
int c[maxn];
struct node
{
int to;
int nxt;
}edge[maxn<<];
int head[maxn];
int tot;
int res;
void add(int u,int v)
{
edge[tot].nxt=head[u];
edge[tot].to=v;
head[u]=tot++;
} void Init()
{
res=;
tot=;
memset(c,,sizeof(c));
memset(head,-,sizeof(head));
}
void dfs(int u,int pre)
{
res=max(res,num[u]);
set<int> t;
t.clear();
for(int i=;i<num[u];i++)
{
int v=s[u][i];
if(c[v])
{
t.insert(c[v]);
}
}
int temp=;
for(int i=;i<num[u];i++)
{
int v=s[u][i];
if(!c[v])
{
temp++;
while(t.count(temp))
{
temp++;
}
c[v]=temp;
}
}
for(int i=head[u];i!=-;i=edge[i].nxt)
{
int to=edge[i].to;
if(to!=pre)
{
dfs(to,u);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
Init();
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
s[i].clear();
for(int k=;k<num[i];k++)
{
int x;
scanf("%d",&x);
s[i].push_back(x);
}
}
int u,v;
for(int i=;i<n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,);
for(int i=;i<=m;i++)
{
if(!c[i])
{
c[i]=;
}
}
cout<<res<<endl;
cout<<c[];
for(int i=;i<=m;i++)
{
cout<<" "<<c[i];
}
cout<<endl;
}
return ;
}

【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring的更多相关文章

  1. CodeForces 804C Ice cream coloring

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

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

  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. 理解题意后的UVa340

    之前理解题意错误,应该是每一次game,只输入一组答案序列,输入多组测试序列,而之前的错误理解是每一次输入都对应一组答案序列和一组测试序列,下面是理解题意后的代码,但是还是WA,待修改 #includ ...

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

  9. 树的遍历 | 1079 理解题意+DFS

    这题如果读懂了题意,就很好做,一波操作就结束了.不过题目有点难读,考验耐心和读题的细致. AC代码: #include <stdio.h> #include <memory.h> ...

随机推荐

  1. poj3252Round Numbers

    链接 也算是组合 以前按组合做过一次 忘记怎么做的了 这次按dp写的 dp[i][j][g][k] 表示第i位为k(0|1)而且有j个1,g个0的情况数 貌似写的麻烦了...这一类的题,进行逐位计算就 ...

  2. C#结构体和类的区别(转)

    结构体和类的区别:    在做一个项目时,使用了较多的结构体,并且存在一些结构体的嵌套,即某结构体成员集合包含另一个结构体等,总是出现一些奇怪的错误,才终于下决心好好分析一下到底类和结构体有啥不同,虽 ...

  3. spring mvc URL忽略大小写

    @Configuration public class SpringWebConfig extends WebMvcConfigurationSupport { @Override public vo ...

  4. 日常博客----rem,em的恩怨相杀

    基本知识: 使用 em 和 rem 单位可以让我们的设计更加灵活,能够控制元素整体放大缩小,而不是固定大小. 我们可以使用这种灵活性,使我们在开发期间,能更加快速灵活的调整,允许浏览器用户调整浏览器大 ...

  5. bt设置指定的ip地址

    auto eth0 iface eth0 inet staticaddress 192.168.1.112 IP地址netmask 255.255.255.0 子网掩码network 192.168. ...

  6. Android获取声音长度

    代码 MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever(); metaRetriever.setDataSource( ...

  7. 如何使用capedit分割数据包文件

    wireshark是一个网络数据包的分析工具,主要用来捕获网卡上的数据包并显示数据包的详细内容.在处理一些大的数据包文件时,如果直接用wireshark图形工具打开一些大文件的数据包会出现响应慢甚至没 ...

  8. Gym - 100676G Training Camp (状压dp)

    G. Training Camp[ Color: Yellow ]Montaser is planning to train very hard for ACM JCPC 2015; he has p ...

  9. bind的使用

    bind: 改变this的指向,返回一个新函数(它不会立即执行,需要调用新函数才能执行:apply call方法是立即执行) let obj = { name: 'jason888'} functio ...

  10. OpenCV2:第七章 图像处理

    一.简介 灰度图(灰阶图),把白色到黑色之间分为256阶灰度 彩色图有RGB三个分量,假设图是800*800像素,那么就有三个800*800的矩阵分别代表RGB 二值化处理设定阈值,在阈值中的像素值变 ...