【题目链接】:http://codeforces.com/contest/782/problem/C

【题意】



给你一棵树

让你满足要求

->任意相连的3个节点的颜色不能相同

的情况下进行染色

问最少需要的颜色数目

【题解】



这种染色题一般只要贪心染就好了

即之前没出现过的颜色最小的颜色就好;

(这里的之前指的是当前枚举的节点和它的父亲节点以及这个节点的儿子节点们。。)

感觉很多题都要顺着题意去做啊;



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 2e5+1000; vector <int> G[N];
int n;
int color[N]; void dfs(int x, int fa)
{
int now = 0;
for (int y : G[x])
if (y!=fa)
{
now++;
while (color[fa] == now || color[x] == now) now++;
color[y] = now;
dfs(y, x);
}
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n);
rep1(i, 1, n - 1)
{
int x, y;
rei(x), rei(y);
G[x].push_back(y), G[y].push_back(x);
}
color[1] = 1;
dfs(1, -1);
printf("%d\n", color[max_element(color + 1, color + 1 + n)-color]);
rep1(i, 1, n)
printf("%d ", color[i]);
return 0;
}

【codeforces 782C】Andryusha and Colored Balloons的更多相关文章

  1. 【47.95%】【codeforces 554C】Kyoya and Colored Balls

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 782A】Andryusha and Socks

    [题目链接]:http://codeforces.com/contest/782/problem/A [题意] 如果手套没有成一双,那么其中的一只就会被放在桌子上; 问你桌子上手套的只数最多的时候有几 ...

  3. Codeforces 781A:Andryusha and Colored Balloons(DFS染色)

    http://codeforces.com/contest/782/problem/C 题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色. 思路:比 ...

  4. Codeforces 782C. Andryusha and Colored Balloons 搜索

    C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...

  5. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons

    地址:http://codeforces.com/contest/782/problem/C 题目: C. Andryusha and Colored Balloons time limit per ...

  6. AC日记——Andryusha and Colored Balloons codeforces 780c

    C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. code force 403C.C. Andryusha and Colored Balloons

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. codeforces781A Andryusha and Colored Balloons

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

随机推荐

  1. Android 解决RecyclerView删除Item导致位置错乱的问题

    RecyclerView的刷新分为内容变化和结构变化,结构变化比如remove和insert等并不会导致viewholder的更新,所以有时候我们使用 notifyItemRemoved(positi ...

  2. android图片文件的路径地址与Uri的相互转换

    一个android文件的Uri地址一般如下: content://media/external/images/media/62026 这是一张图片的Uri,那么我们如何根据这个Uri获得其在文件系统中 ...

  3. regexp模式匹配+location页面跳转+cookie/localstorage本地存储

    学习js的过程中,根据知识点编写一些code进行测试,以便检验. 这段程序使用了以下知识点: 1.regexp,对数据进行模式匹配 2.使用location对象进行页面跳转. 3.cookie/loc ...

  4. sshfs 通过ssh 挂载远程目录

    安装:yum -y install sshfs 挂载远程 ssh 文件系统: sshfs -p 1234 root@192.168.1.218:/home/ /mnt/ sshfs -p SSH端口 ...

  5. 使用BeautifulSoup爬取“0daydown”站点的信息(2)——字符编码问题解决

    上篇中的程序实现了抓取0daydown最新的10页信息.输出是直接输出到控制台里面.再次改进代码时我准备把它们写入到一个TXT文档中.这是问题就出来了. 最初我的代码例如以下: #-*- coding ...

  6. JavaScript系列--JavaScript数组高阶函数reduce()方法详解及奇淫技巧

    一.前言 reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. reduce()方 ...

  7. LA 2678 – Subsequence

    看到限时3S,自己写了一个二重循环的,然后华丽的 TLE...T T 瞄了瞄书上,作者的思路果然是很好.膜拜中. 他只枚举了终点,然后用二分查找. 用到了lower_bound函数,这个lower_b ...

  8. Nginx 虚拟主机及正向代理设置

    添加虚拟主机 # vim /usr/local/nginx-1.9.0/conf/vhost/proxy.conf  server { resolver 8.8.8.8; listen ; locat ...

  9. 【例题 6-5 UVA 12657 】Boxes in a Line

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 双向链表模拟题. 移动的时候,要注意它就在所需要的位置的情况.那种情况不移动. (如果已经在所需位置了,还用链表的插入方式强行移动的 ...

  10. [Nuxt] Navigate with nuxt-link and Customize isClient Behavior in Nuxt and Vue.js

    Because Nuxt renders pages on the server, you should use the nuxt-link components to navigate betwee ...