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个木块,堆 ...
随机推荐
- "Access restriction: The type BASE64Encoder is not accessible due to restrict"问题解决
问题如题: Eclipse中有一种叫做存取限制的机制,来防止你错误使用那些非共享的API.通常来说,Eclipse做的是对的,因为两点,我们不想要使用非共享API的,而且Eclipse知道什么是共享的 ...
- [LeetCode] 23. Merge k Sorted Lists ☆☆
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- Python学习笔记(二十)调试
摘抄自: https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143191557 ...
- Javascript正则表达式难点、重点
重复的贪婪性 {n,m} {n,} {n} ? + * ?表示匹配0次或1次,(最多匹配1次): +表示匹配1次或多次,(最少匹配1次): *表示匹配0次或多次,(匹配任意次): {m,n}表示匹配m ...
- 使用asp.net改变图片颜色
最近奇葩经理提出了奇葩的需求,要能在网站上改变图片的颜色,比如灰色的变成彩色,彩色的变成灰色,尼玛楼主的感受你们不懂!于是有了下面的代码... 用法:调用update_pixelColor方法并传参数 ...
- 【AtCoder】ARC092 D - Two Sequences
[题目]AtCoder Regular Contest 092 D - Two Sequences [题意]给定n个数的数组A和数组B,求所有A[i]+B[j]的异或和(1<=i,j<=n ...
- 面向对象 ( OO ) 的程序设计——继承
本文地址:http://www.cnblogs.com/veinyin/p/7608282.html 仅支持实现继承,且主要依靠原型链来实现,不过一般会混合构造函数一起实现继承 1 原型链 继承使用 ...
- C语言二分查找
#include <stdio.h> /* 二分查找条件: 1.有序序列 2.数据在数组中 */ int baseBinarySearch(int a[],int h,int k) { ; ...
- imperva配置文件的导入导出
imperva配置文件的导入导出 Full_expimp.sh //进行备份 1导入 2导出 输入密码后 1 全部导出 是否想导出失败的数据 默认密码是system的密码 输入导出的路径 ...
- Linux端口占用
1.netstat netstat -anp | grep 23232 Sample: [root@BICServer 0825]# netstat -anp | grep 23232 tcp 0 0 ...