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个木块,堆 ...
随机推荐
- 将本地项目和远程git仓库相连接
有时候写代码,是存在本地的,远程仓库没有对应的代码库,这个时候就需要把本地的代码项目与远程git库连接并推送. 1. 将项目文件添加到仓库中 本地的项目文档: 添加项目文件 git add . 2. ...
- 多例模式,保证实例的唯一性,仅适用于form窗体
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- codevs 1492 探案第二部
1492 探案第二部 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 我们伟大的 Sherlock·Holmes 先生最近遇上了一件相当棘手的案子,随 ...
- 简单高效的asp.net目录树源代码
前台页面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default. ...
- 对 JavaScript 进行单元测试的工具
简介 单元测试关注的是验证一个模块或一段代码的执行效果是否和设计或预期一样.有些开发人员认为,编写测试用例浪费时间而宁愿去编写新的模块.然而,在处理大型应用程序时,单元测试实际上会节省时间:它能帮助您 ...
- NYOJ 117 求逆序数 (树状数组)
题目链接 描述 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 现在,给你一个N个元素的序列,请你判断出 ...
- 牛客网习题剑指offer之数值的整数次方
分析: 要考虑到exponent为0和负数的情况. 如果base是0并且exponent是负数的时候呢?那就发生除0的情况了. AC代码: public class Solution { public ...
- vue-loader 调用了cssLoaders方法配置了css加载器属性。
module: { loaders: [ // 这里也是相应的配置,test就是匹配文件,loader是加载器, { test: /\.vue$/, loader: 'vue' }, { test: ...
- 怎么让IIS7第一次访问相应速度加快
当我们把网站部署在IIS7或IIS6S的时候,每当IIS或是Application Pool重启后,第一次请求网站反应总是很慢,原因大家都知道(不知道可以参考这个动画说明ASP.NET网页第一个Req ...
- PHP非常好用的分页类
分页类: <?php /* * ********************************************* * @类名: page * @参数: $myde_total - 总记 ...