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. iOS 开发App捕获异常, 反馈给服务器, 提高用户体验

    在我们开发的app中, 不可避免的, 有时候用户使用软件会崩溃.  我们就需要捕获异常, 可以在入口类中加入相应的代码, 可以在每次用户打开程序的时候, 检查一下沙盒中是否有崩溃日志, 如果有, 可以 ...

  2. React-Native 开发问题整理

    1.内嵌WebView,点击输入框后页面不自动上滚 <activity android:name=".MainActivity" android:label="@s ...

  3. HashMap,Hashset,ArrayList以及LinkedList集合的区别,以及各自的用法

    基础内容 容器就是一种装其他各种对象的器皿.java.util包 容器:Set, List, Map ,数组.只有这四种容器. Collection(集合) 一个一个往里装,Map 一对一对往里装. ...

  4. 纯手写的css3正方体旋转效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. MMRecovery下载和恢复方法教程

    如何能下载到最新版本的恢复软件MMRecovery?下载后我们如何能够快速的使用起来呢?好的,我们接下来就开始这些内容. 一.下载最新版本的恢复软件MMRecovery 1.官方网站下载,如下图在浏览 ...

  6. CFBundleURLTypes URL scheme

    https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Ar ...

  7. gym 100947I (求因子)

    What a Mess Alex is a very clever boy, after all he is the son of the greatest watchmaker in Odin. O ...

  8. (二)VMware Harbor 安装

    转自:https://blog.csdn.net/qq_33633013/article/details/82217277 一.环境.软件准备 harbor 需要依赖docker,compose工具, ...

  9. cmd命令002

    cd..--> 返回上一级目录 cd\ --> 返回根目录"cd /d e:"--> 将当前盘符切换到e盘,"cd users/admin"- ...

  10. roi pooling层

    roi pooling是先进行roi projection(即映射)然后再池化 映射是把用来训练的图片的roi映射到最后一层特征层(即卷积层).方法其实很简单,图片经过特征提取后,到最后一层卷积层时, ...