传送门

分析

这道题做了好长时间,题意就很难理解。

我们注意到这句话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)的更多相关文章

  1. Codeforces Round #411 (Div. 2) 【ABCDE】

    A. Fake NP 题意:给你l,r,让你输出[l,r]里面除1以外的,出现因子数量最多的那个数. 题解:如果l==r输出l,否则都输出2 #include<bits/stdc++.h> ...

  2. 【DFS】【贪心】Codeforces Round #411 (Div. 1) C. Ice cream coloring

    对那个树进行dfs,在动态维护那个当前的冰激凌集合的时候,显然某种冰激凌仅会进出集合各一次(因为在树上形成连通块). 于是显然可以对当前的冰激凌集合贪心染色.暴力去维护即可.具体实现看代码.map不必 ...

  3. 【推导】Codeforces Round #411 (Div. 1) B. Minimum number of steps

    最后肯定是bbbb...aaaa...这样. 你每进行一系列替换操作,相当于把一个a移动到右侧. 会增加一些b的数量……然后你统计一下就行.式子很简单. 喵喵喵,我分段统计的,用了等比数列……感觉智障 ...

  4. 【推导】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() ...

  5. 【Codeforces Round #406 (Div. 2)】题解

    The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...

  6. 【Codeforces Round#279 Div.2】B. Queue

    这题看别人的.就是那么诚实.http://www.cnblogs.com/zhyfzy/p/4117481.html B. Queue During the lunch break all n Ber ...

  7. 【Codeforces Round #405 ( Div 2)】题解

    Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...

  8. 【Codeforces Round #404 (Div. 2)】题解

    A. Anton and Polyhedrons 直接统计+答案就可以了. #include<cstdio> #include<cstring> #include<alg ...

  9. 【Codeforces Round #518 (Div. 2)】

    A:https://www.cnblogs.com/myx12345/p/9847588.html B:https://www.cnblogs.com/myx12345/p/9847590.html ...

随机推荐

  1. 提高系统性能——对SQL语句优化的思考

    软件在研发的过程中自始至终都在留意着系统的可扩展性.但与此同一时候也在关注着系统的性能,SQL语句作为系统性能的一环不容忽视.从今天開始结合开发的经验,谈一下我对SQL语句优化的理解和认知: 1.在联 ...

  2. CentOS笔记-其他杂记

    1.忘记密码时,可以用single模式来修改密码,为了安全,可以禁用single模式,参考网址如下 Centos服务器禁止单用户模式(single)来增强系统安全 2.远程登录:ssh root@xx ...

  3. Sequelize入门一

    最近刚开始接触Sequelize,当中遇到不少坑,所以想写篇Sequelize入门和大家分享,避免有道友和我一样爬坑. 学习sequelize的初衷是想解决SQL注入,它支持MySQL, SQLite ...

  4. GPS常见故障

    当出现故障时,依据可能原因进行排查. 下表列举典型故障及调试方法 现象 root cause 检查 实验   GPS无法开启/无法搜星 软件配置错误 SW 相关配置(如GPIO等) 录制mobile ...

  5. codeforces 435 B. Pasha Maximizes 解题报告

    题目链接:http://codeforces.com/problemset/problem/435/B 题目意思:给出一个最多为18位的数,可以通过对相邻两个数字进行交换,最多交换 k 次,问交换 k ...

  6. Silverlight实用窍门系列:1.Silverlight读取外部XML加载配置---(使用WebClient读取XAP包同目录下的XML文件))【附带实例源码】

    使用WebClient读取XAP包同目录下的XML文件 我们想要读取XAP包下面的XML文件,需要将此XML文件放在加载XAP包的网页的目录中去,然后使用URI方式读取此URL方式下的XML文件. 首 ...

  7. [USACO 2016Dec] Team Building

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4742 [算法] 动态规划 用Fi,j,k表示约翰的前i头牛和保罗的前j头牛匹配 , ...

  8. jQuery EasyUI Portal 保存拖动位置,仿谷歌DashBoard效果的

    仿照谷歌http://www.google.com/ig?hl=zh-CN中的效果,本文档中包含了拖动后保存位置至Cookie中以及拖动后不保存位置的html文件效果,文档结构

  9. 中文环境下PostgreSQL的使用

    虽然官方文档有提到编码的问题,但是对于中文讲的比较简单,给中文的PostgreSQL用户带来很多困扰,本文简单简述一下中文环境下PostgreSQL如何正确设置编码. 一.服务器端的编码设置 Post ...

  10. Sublime Text3 python代码去除白色框框

    之所以会出现白色框框,是因为代码不符合PEP8规范!!! 可以装一个 AUTOPEP8 插件,然后按 Ctrl + Alt + r 就会自动帮你PEP8格式化,白色框框就会消失了... 这是原来的博文 ...