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 王川东 计算机是如何工作的? 计算机的基本原理是存储程序和程序控制.预先要把指挥计算机如何进行操作的指令序列(称为程序)和原始数据通过输入设备输送到计算机内存贮器中.每一条指令中明 ...
- [微软官网]One Windows Kernel
One Windows Kernel https://techcommunity.microsoft.com/t5/Windows-Kernel-Internals/One-Windows-Kerne ...
- Windows下多线程编程(一)
前言 熟练掌握Windows下的多线程编程,能够让我们编写出更规范多线程代码,避免不要的异常.Windows下的多线程编程非常复杂,但是了解一些常用的特性,已经能够满足我们普通多线程对性能及其他要求. ...
- Ajax修改全局变量问题解决方法(Zepto版)
前两天项目遇到一个用ajax修改全局变量的案例,一开始无法给这个全局变量修改赋值,在网上查了一下,解决如下: 修改前: var word=1; $.ajax({ url:"myJSON.js ...
- 网页显示百度地图 Jquery
cshtml: <div class="modal" style="visibility: hidden"> <div id="ma ...
- 【Python】Python对象类型及其运算
Python对象类型及其运算 基本要点: 程序中储存的所有数据都是对象(可变对象:值可以修改 不可变对象:值不可修改) 每个对象都有一个身份.一个类型.一个值 例: >>> a1 = ...
- 程序猿必备技能:数据库管理——关于MySQL
一.初识MySQL 1.什么是数据库? 数据库(Database,DB)简而言之就是存放数据的仓库,是为了实现一定目的,按照某种规则组织起来的数据的集合. 2.使用数据库的必要性 (1)结构化存储大量 ...
- 【Java并发编程】之二:线程中断
[Java并发编程]之二:线程中断 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一 ...
- 【Mybatis】Mybatis的sql模糊查询
这个网站中有很多方法.https://code.google.com/p/mybatis/issues/detail?id=85 自己试验了如下的方法. 1. 参数中直接加入%% param.set ...
- MySQL一主两从
服务器说明: MySQL-Master:192.168.1. MySQL-Slave1:192.168.1. MySQL-Slave2:192.168.1. 关闭防火墙,关闭selinux 统一采用源 ...