【codeforces 782C】Andryusha and Colored Balloons
【题目链接】: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的更多相关文章
- 【47.95%】【codeforces 554C】Kyoya and Colored Balls
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 782A】Andryusha and Socks
[题目链接]:http://codeforces.com/contest/782/problem/A [题意] 如果手套没有成一双,那么其中的一只就会被放在桌子上; 问你桌子上手套的只数最多的时候有几 ...
- Codeforces 781A:Andryusha and Colored Balloons(DFS染色)
http://codeforces.com/contest/782/problem/C 题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色. 思路:比 ...
- Codeforces 782C. Andryusha and Colored Balloons 搜索
C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...
- 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 ...
- AC日记——Andryusha and Colored Balloons codeforces 780c
C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 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 ...
- codeforces781A Andryusha and Colored Balloons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
随机推荐
- node event中 on emit off 的封装
事件绑定一个事件名称对应多个事件函数 应此它们的关系是一对多的关系 数据类型采用对象的形式 key:val 因为函数有多个 所以val选用数组 事件仓库 eventList = { key:val, ...
- 关于python中矩阵的实现和矩阵的转置
python中矩阵的实现是靠序列,,, 序列有很多形式, 其实矩阵是现实生活中的东西,把现实生活中的结构转换到程序中. 就需要有个实现的方法,而这种路径是多种多样的. 下面给出一个把矩阵转换成pyth ...
- Altium Designer如何统一改变pcb状态下的原件标号位置
原创 我用的是Altium Designer16版本 变成 步骤如下: 选中标号 右击 下边一步很重要: 点击应用和确定 在之后弹出的对话框中选则你要改变的位置,我这里是把标号改变到原件的右侧: 等待 ...
- python输出杨辉三角
使用python列表,展示杨辉三角 # !/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan yanghui = [] fo ...
- poj 3071 Football(线段树+概率)
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2801 Accepted: 1428 Descript ...
- OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类
我们继续来看一下Foundation框架中的NSArray类和NSMutableArray类,其实NSArray类和Java中的List差不多,算是一种数据结构,当然我们从这两个类可以看到,NSArr ...
- 请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径
转自原文 请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径. 打开C:\Windows\System32\inetsrv\config路径 找到applicationH ...
- java測试网络连接是否成功并设置超时时间
/** * 获取RMI接口状态 * * @return "0":服务正常,"1": 连接报错,"2":连接超时 */ @Override p ...
- MySQL之SQL mode——检查官
原文:MySQL之SQL mode--检查官 MySQL升级后代码出bug? 前段时间,测试的MySQL服务器进行了一次升级,从MySQL5.6升级到了MySQL5.7.以为是简单的升级,不会影响到代 ...
- 2014年武汉的IT行情好像不太好
本周,加入武汉一起好工作一周了,也就是说本次找工作彻底结束了. 总的来说,求职行情不太行,双方都匹配的工作好少呀. 1. 武汉财富基石,过了一面,第二面没有去. 钱太少,4K多,跳楼价. 2.武汉 ...