传送门

分析

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

我们注意到这句话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. Andriod DiskLruCache的使用案例

    DiskLruCache是谷歌推荐的用来实现硬盘缓存的类,本案例将对DiskLruCache的基本用法做一个总结,包括:创建缓存.查找使用缓存.移除缓存等等. 实现效果图 创建DiskLruCache ...

  2. Codeforces 424 C. Magic Formulas

    xor是满足交换律的,展开后发现仅仅要能高速求出 [1mod1....1modn],....,[nmod1...nmodn]的矩阵的xor即可了....然后找个规律 C. Magic Formulas ...

  3. HDU 1040 As Easy As A+B [补]

    今天去老校区找她,不想带电脑了,所以没时间A题了 /*******************************************************************/ As Ea ...

  4. (转)C中的volatile用法

    volatile 影响编译器编译的结果,指出,volatile 变量是随时可能发生变化的,与volatile变量有关的运算,不要进行编译优化,以免出错,(VC++ 在产生release版可执行码时会进 ...

  5. C/C++语言中的位运算

    在计算机程序中,数据的位是可以操作的最小数据单位,理论上可以用“位运算”来完成所有的运算和操作. 一般的位操作是用来控制硬件的,或者做数据变换使用,但是,灵活的位操作可以有效地提高程序运行的效率.C语 ...

  6. Hashed Indexes Geospatial Index

    Indexes — MongoDB Manual https://docs.mongodb.com/manual/indexes/ 地理索引 哈希索引

  7. 合肥 专业做APP(安卓,ios) 微信公共平台

    合肥 专业做APP(安卓,ios) 微信公共平台 电话:15715696592

  8. ossfs常见配置错误

    以下问题出现在非root用户下 执行echo ××××> /etc/passwd-ossfs  bash: /etc/passwd-ossfs: Permission denied 使用sudo ...

  9. iOS 设置TextView控件内容行间距

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { if (textView.text.length < 1) { textV ...

  10. Java 快速失败( fail-fast ) 安全失败( fail-safe )

    原文:http://www.cnblogs.com/ygj0930/p/6543350.html 快速失败( fail-fast ):当你在迭代一个集合的时候,如果有另一个线程正在修改你正在访问的那个 ...