CodeForces 804C Ice cream coloring

题解:

这个题目中最关键的一句话是, 把任意一种类型的冰激凌所在的所有节点拿下来之后,这些节点是一个连通图(树)。

所以就不会存在多个set+起来之后是一个新的完全图。

所以只要直接去做就好了。

对于每个节点来说,染色。

 

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 3e5 + 100;
vector<int> vc[N], e[N];
int col[N];
int vis[N];
int mx = 0;
void dfs(int o, int u){
    for(int v : vc[u]){
        vis[col[v]]++;
    }
    int b = 1;
    for(int v : vc[u]){
        if(col[v]) continue;
        while(vis[b]) ++b;
        vis[b] = 1;
        col[v] = b;
    }
    mx = max(mx, b);
    for(int v : vc[u]){
        vis[col[v]] = 0;
    }
    for(int v : e[u]){
        if(v == o) continue;
        dfs(u, v);
    }
}
int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i){
        int si, tv;
        scanf("%d", &si);
        for(int j = 1; j <= si; ++j){
            scanf("%d", &tv);
            vc[i].pb(tv);
        }
    }
    int u, v;
    for(int i = 1; i < n; ++i){
        scanf("%d%d", &u, &v);
        e[u].pb(v); e[v].pb(u);
    }
    dfs(0, 1);
    printf("%d\n", mx);
    for(int i = 1; i <= m; ++i){
        if(!col[i]) col[i] = 1;
        printf("%d%c", col[i], " \n"[i==m]);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/MingSD/p/10869004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值