【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 connected subgraph
也就是说在最后的图中相同颜色的点构成一个连通图
也就是说如果1和2、3在不同的点共同出现,那么2和3一定不会在某个点共同出现。于是我们可以直接暴力dfs,在每个点对没有颜色的冰激凌贪心染最小的颜色。
需要注意有某种冰激凌没有出现的情况。
trick
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <utility>
#include <bitset>
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
#define rep(i,a,b) for (int i=(a);i<(b);i++)
#define per(i,b,a) for (int i=(b)-1;i>=(a);i--)
#define REP(i,a,b) for (int i=(a);i<=(b);i++)
#define PER(i,b,a) for (int i=(b);i>=(a);i--)
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1000005; // 1e6;
int n,m;
vector<int>G[MAXN];
set<int> S[MAXN];
int ans;
int c[MAXN];
void dfs(int i, int fa)
{
vector<int> used;
if (fa==-1)
{
for (int x:S[i])
{
c[x] = ++ans;
//printf("c[%d]=%d\n",x,c[x]);
}
}
else
{
for (int x:S[i])
{
if (c[x] != 0) used.pb(c[x]);
}
sort(used.begin(), used.end());
int now = 1,ite = 0;
for (int x:S[i])
{
if (c[x] == 0)
{
while(ite < used.size())
{
if (now == used[ite]) ite++, now ++;
else break;
}
c[x]=now;
//printf("c[%d]=%d\n",x,c[x]);
now++;
}
}
ans = max(ans,now-1);
}
rep(j,0,G[i].size())
{
int v = G[i][j];
if (v==fa) continue;
dfs(v,i);
}
}
int main()
{
scanf("%d%d",&n,&m);
rep(i,1,n+1)
{
int si;
scanf("%d",&si);
rep(j,0,si)
{
int c;
scanf("%d",&c); S[i].insert(c);
}
}
rep(i,1,n)
{
int u,v;
scanf("%d%d",&u,&v);G[u].pb(v);G[v].pb(u);
}
dfs(1,-1);
if (ans == 0) ans = 1;
printf("%d\n",ans);
rep(i,1,m+1)
{
if (c[i]==0) c[i]=1;
printf("%d%c",c[i]," \n"[i==m]);
}
}
【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)的更多相关文章
- Codeforces Round #411 (Div. 2) 【ABCDE】
A. Fake NP 题意:给你l,r,让你输出[l,r]里面除1以外的,出现因子数量最多的那个数. 题解:如果l==r输出l,否则都输出2 #include<bits/stdc++.h> ...
- 【DFS】【贪心】Codeforces Round #411 (Div. 1) C. Ice cream coloring
对那个树进行dfs,在动态维护那个当前的冰激凌集合的时候,显然某种冰激凌仅会进出集合各一次(因为在树上形成连通块). 于是显然可以对当前的冰激凌集合贪心染色.暴力去维护即可.具体实现看代码.map不必 ...
- 【推导】Codeforces Round #411 (Div. 1) B. Minimum number of steps
最后肯定是bbbb...aaaa...这样. 你每进行一系列替换操作,相当于把一个a移动到右侧. 会增加一些b的数量……然后你统计一下就行.式子很简单. 喵喵喵,我分段统计的,用了等比数列……感觉智障 ...
- 【推导】Codeforces Round #411 (Div. 1) A. Find Amir
1 2 3 4 5 6 7 4-5-3-6-2-7-1 答案是(n-1)/2 #include<cstdio> using namespace std; int n; int main() ...
- 【Codeforces Round #406 (Div. 2)】题解
The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...
- 【Codeforces Round#279 Div.2】B. Queue
这题看别人的.就是那么诚实.http://www.cnblogs.com/zhyfzy/p/4117481.html B. Queue During the lunch break all n Ber ...
- 【Codeforces Round #405 ( Div 2)】题解
Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...
- 【Codeforces Round #404 (Div. 2)】题解
A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<alg ...
- 【Codeforces Round #518 (Div. 2)】
A:https://www.cnblogs.com/myx12345/p/9847588.html B:https://www.cnblogs.com/myx12345/p/9847590.html ...
随机推荐
- ARM汇编指令MCR/MRC学习
MCR指令将ARM处理器的寄存器中的数据传送到协处理器的寄存器中.假设协处理器不能成功地运行该操作.将产生没有定义的指令异常中断. 指令的语法格式: MCR{<cond>} p15, 0, ...
- JNI——访问数组
JNI在处理基本类型数组和对象数组上面是不同的.对象数组里面是一些指向对象实例或者其它数组的引用. 因为速度的原因,先通过GetXXXArrayElements函数把简单类型的数组转化成本地类型的数组 ...
- SDIO总线(一)
SDIO接口是在SD内存卡接口的基础上发展起来的接口.SDIO接口兼容曾经的SD内存卡.而且能够连接SDIO接口的设备. SDIO1.0标准定义了两种类型的SDIO卡: 1.全速的SDIO卡.传输率能 ...
- onedrive实现excel在线编辑 online excel
1.首先用火狐浏览器或者谷歌浏览器登录 https://onedrive.live.com 2.注冊邮箱账户信息 3.在邮箱激活账户信息 4.登录进去.点击我的账户,点击左側文件树.点选上载,将文件上 ...
- 【转载】FAT12格式的引导程序(2)
1.用WinImage来写入到引导区的详细步骤: 启动WinImage后,打开“文件”菜单,单击菜单中的“打开”命令. 选择之前保存的磁盘镜像文件“boot.img”或者“boot.ima”. 打开 ...
- aop中获取方法的注解
@Around(value="@annotation(apiLog)") public Object around(ProceedingJoinPoint pjp, ApiLog ...
- python安装easy_install和pip
1 安装easy_install https://pypi.python.org/pypi/setuptools 下载setuptools 执行python setup.py install就安装成功 ...
- Provided Maven Coordinates must be in the form 'groupId:artifactId:version'.
[hadoop@hadoop1 bin]$ ./spark-shell --packages org.mongodb.spark:mongo-spark-connector_2.10-2.2.1 Ex ...
- hadoop 集群搭建 配置 spark yarn 对效率的提升永无止境
[手动验证:任意2个节点间是否实现 双向 ssh免密登录] 弄懂通信原理和集群的容错性 任意2个节点间实现双向 ssh免密登录,默认在~目录下 [实现上步后,在其中任一节点安装\配置hadoop后,可 ...
- 创建Material Design风格的Android应用--应用主题
本人全部文章首先公布于个人博客,欢迎关注,地址:http://blog.isming.me 昨天正式公布了android 5,同一时候android developer站点也更新了,添加了创建Mate ...