CodeForces 805E Ice cream coloring
直觉,构造。
画了几个样例,发现可以随便构造......先构造根节点的完全图,每个点置为不同的颜色,然后构造儿子节点的完全图......
#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的更多相关文章
- CodeForces 804C Ice cream coloring
Ice cream coloring 题解: 这个题目中最关键的一句话是, 把任意一种类型的冰激凌所在的所有节点拿下来之后,这些节点是一个连通图(树). 所以就不会存在多个set+起来之后是一个新的完 ...
- 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring
http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...
- 【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 ...
- codeforces 805 E. Ice cream coloring(dfs)
题目链接:http://codeforces.com/contest/805/problem/E 题意:你有n个节点,这个n个节点构成一棵树.每个节点拥有有si个类型的ice,同一个节点的ice互相连 ...
- 【DFS】【贪心】Codeforces Round #411 (Div. 1) C. Ice cream coloring
对那个树进行dfs,在动态维护那个当前的冰激凌集合的时候,显然某种冰激凌仅会进出集合各一次(因为在树上形成连通块). 于是显然可以对当前的冰激凌集合贪心染色.暴力去维护即可.具体实现看代码.map不必 ...
- CodeForces 686A-Free Ice Cream
题目: 儿童排队领冰激凌,给你两个数n,x分别代表接下来有n行与初始的冰激淋数:接下来n行,每行有一个字符('+'or‘-’),还有一个整数d,+d表示新增的冰激 凌数(由搬运工搬运到此),-d表示儿 ...
- 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 ...
- codeforces 686A A. Free Ice Cream(水题)
题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
随机推荐
- nginx 初探 之反向代理
首先要解释的是什么叫做反向代理? 平时我们浏览网页可以输入网址直接访问, 但如果访问国外的网站, 可能就没那么简单('中国特色'), 这时候我们需要配置一个代理服务器, 然后通过此服务器中转来访 ...
- c++数组遍历十种方式
int ia[3][4] = {1,2,3,4,5,6,7,8}; //下标 for (int i = 0; i < 3; i++) { for (int j = 0; j < 4 ...
- 【acmm】一道简单的数学题
emm卡常 我本来写成了这个样子: #include<bits/stdc++.h> using namespace std; typedef long long LL; ; struct ...
- C# 操作资源文件
(1)首先引用这两个命名空间 (2)两种方式调用资源文件中的内容 private void button2_Click(object sender, EventArgs e) { //通过Resour ...
- 某线下赛AWD
拿别人比赛的来玩一下,或许这就是菜的力量吧. 0x01 任意文件读取: switch ($row['media_type']) { case 0: // 图片广告 ...... break; case ...
- 巅峰极客CTF writeup[上]
经验教训 1.CTF不比实战,最好不要死磕.死磕就输了.我就是死磕在缓存文件死的.真的惭愧: 2.对于flag的位置不要太局限于web目录下,如果是命令执行直接上find / -name flag*: ...
- ubuntu之安装pycharm编辑器
pycharm是Java写的,运行需要Java环境. 安装java jdk sudo add-apt-repository ppa:webupd8team/java sudo apt-get upda ...
- 一款线程安全、基本功能齐全的STL
MiniSTL 目前正在完成一个STL,主要想通过该项目锻炼C++编程.模板编程.熟悉STL.锻炼数据结构和算法能力. 项目的目标是实现STL的几大构件+线程安全.项目过程中主要参考SGI STL源码 ...
- SQLite3数据库的操作
数据库的操作 我们在这个项目中使用的是SQLITE3数据库软件. 通过使用SQLITE3进行创建数据库,创建表,插入记录,查询记录,更新记录,关闭数据库等操作来实现将相应的数据存入数据库中. 打开数据 ...
- URAL 2078~2089
URAL 2078~2089 A - Bowling game 题目描述:给出保龄球每一局击倒的球数,按照保龄球的规则,算出总得分的最小值和最大值. solution 首先是最小值:每一局第一球击倒\ ...