【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 ...
随机推荐
- 关于Memcached的CAS和Set方法造成Socket泄漏的问题
为了解决多并发下写Memcached的冲突方案,我们项目组引入了CAS机制.类同于Java并发包中的CAS(Compareand set)原子操作,用来处理同一个Item被多个线程更改的并发问题.Me ...
- Windows-速度优化的几个方面
One. Win+R - > cmd- > msconfig 禁用不需要的启动项! Two. 关闭一些视觉选项 Three. 设置应用启动快捷键
- linux 环境 php 链接 sqlserver 2008
说明 由于业务需要 在 linux 系统下的 PHP 环境中 要链接 sqlserver2008 数据库 . 添加PHP 链接数据库扩展 php-mssql dockerfile FROM hub.0 ...
- firebug console说明
控制台(Console)是Firebug的第一个面板,也是最重要的面板,主要作用是显示网页加载过程中产生各类信息. 一.显示信息的命令 Firebug内置一个console对象,提供5种方法,用来显示 ...
- JQUERY多选框,单选框,检查选中的值
var str=""; $(":checkbox:checked").each(function(){ if($(this).attr("checke ...
- 【dotnet跨平台】"dotnet restore"和"dotnet run"都做了些什么?
[dotnet跨平台]"dotnet restore"和"dotnet run"都做了些什么? 前言: 关于dotnet跨平台的相关内容.能够參考:跨平台.NE ...
- PandoraBox 支持3G无线上网卡(电信卡3G卡)(三)
笔者采用的是华为EC122无线上网卡 一:编辑/etc/modules.d/60-usb-serial usbserial vendor=0x12d1 product=0x1505 二:编辑/et ...
- UsbManager, UsbDevice的简单示例
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- react项目中的注意点
一.ES6 的编译方法 目前主流的浏览器还不支持ES6. 现在一般采用webpack 和 <script type="text/babel">对jsx 语法进行编译, ...
- ES6 中的let 声明变量
1.let是声明的是块级变量,不会污染全局,一般条件与循环中会用到: 2.let 不可以变量提升: 3.let不遵循作用域,一个作用域内如果有该变量,就不会到全局去找,也不可以在一个作用域重复声明一 ...