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. Sqlserver调用WebApi

    原文地址   http://www.cnblogs.com/lflyq/p/6065160.html sp_configure 'show advanced options', 1;GORECONFI ...

  2. AJPFX关于Timer类的学习

    * Timer类:计时器public class Demo1 { public static void main(String[] args) throws InterruptedException ...

  3. [转]合理使用ArrayMap代替HashMap

    合理使用ArrayMap代替HashMap 2016年07月08日 15:34:44 阅读数:5938 转载请标注: 披萨大叔的博客 http://blog.csdn.net/qq_27258799/ ...

  4. 字符串、数组、json

    一.字符串 string 1.字符串的定义: (1).var s="haha"; (2).var s=new string ("hello") 对象形式定义 2 ...

  5. Android学习笔记--Intent

    Intent是android四大组件之间交互的一种重要方式.Intent可以指明当前要执行的动作,也可以指明要传递的数据.Intent可以用来启动活动,启动服务,发送广播. Intent分为两种:1. ...

  6. JSP和Servlet性能优化经验谈

    你的J2EE应用是不是运行的很慢?它们能不能承受住不断上升的访问量?本文讲述了开发高性能.高弹性的JSP页面和Servlet的性能优化技术.其意思是建立尽可能快的并能适应数量增长的用户及其请求.在本文 ...

  7. 51nod 1432 独木舟

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承 ...

  8. SAP CRM点了附件的超链接后报错的处理方式

    SAP CRM系统里,点击了附件的这些超链接后,如果是文本文件,会在浏览器里打开.如果是其他类型的文件,会弹出下载对话框. 然而最近我工作时遇到一个问题,点击超链接后,总是弹出Logon failed ...

  9. 11G GI启动顺序

    --11gR2 Clusterware and Grid Home - What You Need to Know (文档 ID 1053147.1)         上图来自<Oracle C ...

  10. [整理]ADB命令行学习笔记

    global driver# 元素定位driver.find_element_by_id("id") # id定位driver.find_element_by_name(" ...