CodeForces 804C Ice cream coloring

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;
}

原文地址:https://www.cnblogs.com/MingSD/p/10869004.html

时间: 2024-11-05 22:56:40

CodeForces 804C Ice cream coloring的相关文章

codeforces 686A A. Free Ice Cream(水题)

题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using

Ice Cream Tower

2017-08-18 21:53:38 writer:pprp 题意如下: Problem D. Ice Cream Tower Input file: Standard Input Output file: Standard Ouptut Time limit: 6 seconds Mr. Panda likes ice cream very much especially the ice cream tower. An ice cream tower consists of K ice cr

HackerRank Ice Cream Parlor

传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together have M dollars they want to spend on ice cream. The parlor offers N flavors, and they want to choose two flavors so that they end up spending the whol

codeforces D. Ice Sculptures 题解

The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus n ice sculptures were erected. The sculptures

【HackerRank】Ice Cream Parlor

Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N flavors available, they have to choose two distinct flavors whose cost equals M. Given a list of cost of N flavors, output the indices of two items wh

How to Implement Bluetooth Low Energy (BLE) in Ice Cream Sandwich

ShareThis - By Vikas Verma Bluetooth low energy (BLE) is a feature of Bluetooth 4.0 wireless radio technology, aimed at new, principally low-power and low-latency, applications for wireless devices within a short range. As I discussed in my previous

E. Sonya and Ice Cream(开拓思维)

E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that

Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)

题目: To encourage visitors active movement among the attractions, a circular path with ice cream stands was built in the park some time ago. A discount system common for all stands was also introduced. When a customer buys ice cream at some stand, he

CodeForces 686A Free Ice Cream (水题模拟)

题意:给定初始数量的冰激凌,然后n个操作,如果是“+”,那么数量就会增加,如果是“-”,如果现有的数量大于等于要减的数量,那么就减掉,如果小于, 那么孩子就会离家.问你最后剩下多少冰激凌,和出走的孩子数量. 析:多水的一个题,就是一个模拟,如果是+,就加上,如果是‘-’,就判断一下,如果不够,就记录下来. 代码如下: #include <iostream> #include <cmath> #include <cstdlib> #include <set>