Codeforces Round #411 (Div. 2) E
来源:互联网 发布:weibull 7软件下载 编辑:程序博客网 时间:2024/06/05 02:47
E - Ice cream coloring
题意:
有一棵树T,节点为1~n,现在有m种ice cream,每一个节点都有si种ice cream,现在新建一棵树G,节点为ice cream 的种类数,标号为1~m,边是否相连有如下规则:
if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set.
在原树T中同时存在于一个节点的ice cream互相连接,比如1 节点中有3种ice cream那么这三种ice cream互相存在边,也就是一个完全图。
要求:求出新建的G图中,要把所有的点都染色,相邻的点不能有相同的颜色,最少需要多少种颜色,输出最少的颜色个数和每一个点染色的颜色标号(从1开始),并且尽量下标小。
思路:
对于原树中存在于同一个点的ice cream种类数直接给出从小到大的标记,需要处理的问题是如果解决已经染过色的节点,方法:
保存当前节点u中已经被染色的ice cream染的哪一种颜色的标记,然后排序,从颜色1开始染未曾染色的ice cream,需要跳过已经用过的颜色。(细节处理,代码细细思考。)
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <set>using namespace std;const int maxn = 3*1e5+10;int n,m;set<int>ice[maxn];set<int>::iterator it;vector<int>edge[maxn];int cnt,ans[maxn];void dfs(int u,int fa){ if(fa == -1) { for(it = ice[u].begin();it != ice[u].end(); it++) { ans[(*it)] = ++cnt; } } else { vector<int>used; for(it = ice[u].begin();it != ice[u].end(); it++) { if(ans[(*it)]) { used.push_back(ans[(*it)]); } } int L = used.size(); int now = 1,pos = 0; sort(used.begin(),used.end()); for(it = ice[u].begin();it != ice[u].end(); it++) { if(!ans[(*it)]) { while(pos < L) { if(used[pos] == now) now++,pos++; else break; } ans[(*it)] = now++; } cnt = max(cnt,now-1); } } int Size = edge[u].size(); for(int i = 0;i < Size; i++) { int to = edge[u][i]; if(to != fa) dfs(to,u); }}int main(){// freopen("in.txt","r",stdin); scanf("%d%d",&n,&m); for(int i = 1;i <= n; i++) { int temp; scanf("%d",&temp); for(int j = 1;j <= temp; j++) { int cream; scanf("%d",&cream); ice[i].insert(cream); } } for(int i = 1;i < n; i++) { int u,v; scanf("%d%d",&u,&v); edge[u].push_back(v); edge[v].push_back(u); } cnt = 0; dfs(1,-1); if(cnt == 0) cnt = 1; printf("%d\n",cnt); for(int i = 1;i <= m; i++) { if(i != m) printf("%d ",ans[i]?ans[i]:1); else printf("%d\n",ans[i]?ans[i]:1); } return 0;}
阅读全文
0 0
- Codeforces Round #411 (Div. 2) E
- Codeforces Round #411 (Div. 2)E. Ice cream coloring
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces Round #103 (Div. 2) E题
- Codeforces Round #147 (Div. 2) E
- Codeforces Round #184 (Div. 2) D、E
- Codeforces Round #197 (Div. 2) (C~E)
- Codeforces Round #102 (Div. 2) //缺E
- Codeforces Round #104 (Div. 2) //缺E
- Codeforces Round #105 (Div. 2) //缺E
- Codeforces Round #103 (Div. 2) //缺E
- Codeforces Round #106 (Div. 2) //缺E
- Codeforces Round #223 (Div. 2) E
- Codeforces Round #229 (Div. 2) E
- Codeforces Round #139 (Div. 2) E. Unsolvable
- Codeforces Round #142 (Div. 2) E. Triangles
- Codeforces Round #256 (Div. 2) E. Divisors
- Codeforces Round #260 (Div. 2) E
- 双链表的基本使用及其应用(实验2.2)
- 解决Pr, AE在渲染时输出出错的问题
- BZOJ 4562: [Haoi2016]食物链 拓扑排序+DP(其实是递推)
- C++中引用(&)的用法和应用实例
- python3.5+selenium3.4自动化测试10_selenium框架2.0-unittest(下)
- Codeforces Round #411 (Div. 2) E
- 聚沙成塔--爬虫系列(二)(python3基础语法)
- unity_NGUI系统学习(十三)_使用脚本监听控件的值修改,取得控件的值
- 欧拉函数--打表法与直接求值法
- 使用PCL配合cvFindExtrinsicCameraParams2实现棋盘格控制模型的三维运动
- Struts2框架之result返回结果
- Java 高阶 —— 相等性比较
- 六行代码搭建静态网站
- Lintcode落单的数