782C. Andryusha and Colored Balloons DFS
题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同
思路: 直接DFS。由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结点的颜色不同,只需考虑当前节点是否与父亲结点和父亲的父亲结点颜色是否相同。
/** @Date : 2017-05-09 22:37:25
* @FileName: 782C DFS.cpp
* @Platform: Windows
* @Author : Lweleth (SoundEarlf@gmail.com)
* @Link : https://github.com/Lweleth
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; vectorvt[2*N];
int ans[2*N];
int n, ma; int dfs(int x, int pre)
{
int l = vt[x].size();
int c = 0;
for(int i = 0; i < l; i++)
{
int nw = vt[x][i];
if(ans[nw] || nw == pre)
continue;
c++;
while(c == ans[pre] || c == ans[x])
c++;
ans[nw] = c;
ma = max(c, ma);
dfs(nw, x);
}
} int main()
{
while(cin >> n)
{
MMF(ans);
for(int i = 0; i <= n; i++) vt[i].clear();
for(int i = 0; i < n - 1; i++)
{
int x, y;
scanf("%d%d", &x, &y);
vt[x].PB(y);
vt[y].PB(x);
}
ma = 0;
ans[1] = 1;
dfs(1, 1);
printf("%d\n", ma);
for(int i = 1; i <= n; i++)
printf("%d%s", ans[i], i==n?"\n":" ");
}
return 0;
}
782C. Andryusha and Colored Balloons DFS的更多相关文章
- Codeforces 782C. Andryusha and Colored Balloons 搜索
C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...
- 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 ...
- 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 ...
- codeforces781A Andryusha and Colored Balloons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- AC日记——Andryusha and Colored Balloons codeforces 780c
C - Andryusha and Colored Balloons 思路: 水题: 代码: #include <cstdio> #include <cstring> #inc ...
- C. Andryusha and Colored Balloons
C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...
- CodeForces - 780C Andryusha and Colored Balloons(dfs染色)
Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, ...
- 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons
从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...
- 【codeforces 782C】Andryusha and Colored Balloons
[题目链接]:http://codeforces.com/contest/782/problem/C [题意] 给你一棵树 让你满足要求 ->任意相连的3个节点的颜色不能相同 的情况下进行染色 ...
随机推荐
- 《Linux内核与分析》第四周
20135130王川东 一.用户态.内核态和中断处理过程 CPU的几种不同的执行级别: 高执行级别下,代码可以执行特权指令,访问任意的物理地址,这种执行级别对应内核态: 低级别执行状态下,代码的掌握范 ...
- 《linux内核分析》 第二周
20135130 王川东 计算机三个“法宝”:存储程序计算机.函数调用堆栈和中断机制 深入理解函数调用堆栈: 堆栈是C语言运行时必须的一个记录调用路径和参数的空间: 作用包括: 函数调用框架: 保存参 ...
- python 二维矩阵及转byte知识点
1.注意python中的数组和list形式混合: 数组在numpy里面: 2.二维数组这样定义可以修改固定位置的值: rawDataArray_temp = [([0]*nIRImageWidth)f ...
- POJ 2484(对称博弈)
题目链接:http://poj.org/problem?id=2484 这道题目大意是这样的,有n个硬币围成一圈,两个人轮流开始取硬币(假设他们编号从1到n),可以选择取一枚或者取相邻的两枚(相邻是指 ...
- 团队Alpha冲刺(二)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...
- 项目Beta冲刺(团队)第二天
1.昨天的困难 夜间模式实现的时候一点击开关就会出现app黑屏卡死的状态,recreate()方法实现有问题 服务器有点问题 2.今天解决的进度 成员 进度 陈家权 研究如何实现私信模块 赖晓连 最新 ...
- NULL指针引起的一个linux内核漏洞
NULL指针一般都是应用于有效性检测的,其实这里面有一个约定俗成的规则,就是说无效指针并不一定是 NULL,只是为了简单起见,规则约定只要指针无效了就将之设置为NULL,结果就是NULL这个指针被用来 ...
- MySQL 忘记root密码怎么办
前言:记住如果忘记root密码,在启动MySQL的时候,跳过查询授权表就ok了. 对于RedHat 6 而言 (1)启动mysqld 进程时,为其使用:--skip-grant-tables --sk ...
- BETA预发布演示视频
视频连接:优酷http://v.youku.com/v_show/id_XMTgxMjQxMjc0NA==.html?from=y1.7-2
- PHP之implode()方法
implode — 将一个一维数组的值转化为字符串 string implode ( string $glue , array $pieces ) string implode ( array $pi ...