【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 ...
随机推荐
- PHP拓展开发
痛定思痛: 开始了解 PHP 拓展开发,下面这篇文章不错!照着文章讲的,终于实现了! m.php的代码 浏览器访问 m.php 文件!(备注:在linux 命令行中 php -r 'cthulhu() ...
- U盘容纳不了大于4G的文件比如ISO文件咋办?
格式化U盘成NTFS格式就行了,不这么做8,16,32G Upan都容纳不下来.
- TListView使用方法1(转)
ListView1.Items 为标准 Tlistitems类 ListView1.Items (1)赋值 with ListView1.Items.Add do begin Caption:=cap ...
- Cocoapods Undefined symbols for architecture armv7s\arm64
此类错误 "_OBJC_CLASS_$_AFURLSessionManager", referenced from: 解决的方法 在other linker flags里加入一行 ...
- leetcode第一刷_Best Time to Buy and Sell Stock III
这道题还是挺难的,属于我前面提到的,给个数组,线性时间找出个什么东西,尽管上面的两个买卖股票也是这类.只是相比之下稚嫩多了.有关至少至多的问题比較烦人,不好想,等再做一些题,可能会发现什么规律.这道题 ...
- Mmseg中文分词算法解析
Mmseg中文分词算法解析 @author linjiexing 开发中文搜索和中文词库语义自己主动识别的时候,我採用都是基于mmseg中文分词算法开发的Jcseg开源project.使用场景涉及搜索 ...
- 【前端JS】radio 可单选可点击取消选中
普通情况下 radio 单选框仅仅能实现多选一的效果,可是一旦选择当中一个后,这个单选框就不可点击取消其选中状态了.这样的功能在某些业务环境下并不适用.有时我们既须要单选框的多选一效果.也须要复选框的 ...
- cocoapods导入框架出错 The dependency `FMDB` is not used in any concrete target
问题描述: The dependency `FMDB` is not used in any concrete target 解决办法: 官网是这样给推荐的: 在创建Podfile的时候,用这种格式使 ...
- 【HDU 5384】Danganronpa(AC自己主动机)
看官方题解貌似就是个自己主动机裸题 比赛的时候用kuangbin的AC自己主动机模板瞎搞的,居然A了,并且跑的还不慢.. 存下模板吧 #include<cstdio> #include&l ...
- 两个月刷完Leetcode前400题经验总结
更新:气死了,挂个傻逼: 每次做个分享.组织个活动,就会有一些傻逼冒泡生怕别人不知道他是傻逼,气死我了!自己好好看看非法集资的概念,我办这个活动,一分钱都没收,入群99元是督促大家完成刷题任务,最后完 ...