Link

题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离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的更多相关文章

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

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

  2. 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 ...

  3. 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 ...

  4. codeforces781A Andryusha and Colored Balloons

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

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

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

  6. C. Andryusha and Colored Balloons

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

  7. CodeForces - 780C Andryusha and Colored Balloons(dfs染色)

    Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, ...

  8. 【贪心】【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< ...

  9. 【codeforces 782C】Andryusha and Colored Balloons

    [题目链接]:http://codeforces.com/contest/782/problem/C [题意] 给你一棵树 让你满足要求 ->任意相连的3个节点的颜色不能相同 的情况下进行染色 ...

随机推荐

  1. HDU 5187 zhx's contest 快速幂,快速加

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5187 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

  2. Java 成员初始化顺序

    package com.cwcec.test; class Fu { int num = 5; //构造代码块 { System.out.println("Fu constructor co ...

  3. 软工网络15团队作业4-DAY2

    每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 张陈东芳:查看数据库的连接 吴敏烽:规范商品实体类 周汉麟:研究获取商品信息的方法 林振斌:研究获取商 ...

  4. C# 事件总线 EventBus

    1. 引言 事件总线这个概念对你来说可能很陌生,但提到观察者(发布-订阅)模式,你也许就很熟悉.事件总线是对发布-订阅模式的一种实现.它是一种集中式事件处理机制,允许不同的组件之间进行彼此通信而又不需 ...

  5. 小工具xml生成记录

    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent ...

  6. SQL Inserted和deleted详解

    create trigger updateDeleteTime on user for update as begin update user set UpdateTime=(getdate()) f ...

  7. BZOJ 1066:[SCOI2007]蜥蜴(最大流)

    蜥蜴Description在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到 ...

  8. Python网络编程socket

    网络编程之socket 看到本篇文章的题目是不是很疑惑,what is this?,不要着急,但是记住一说网络编程,你就想socket,socket是实现网络编程的工具,那么什么是socket,什么是 ...

  9. addEventListener和attachEvent区别

    addEventListener()是标准的绑定事件监听函数的方法,是W3C所支持的,Chrome.FireFox.Opera.Safari.IE9.0及其以上版本都支持该函数:但是,IE8.0及其以 ...

  10. [NOI2011]兔兔与蛋蛋游戏 二分图博弈

    题面 题面 题解 通过观察,我们可以发现如下性质: 可以看做是2个人在不断移动空格,只是2个人能移动的边不同 一个位置不会被重复经过 : 根据题目要求,因为是按黑白轮流走,所以不可能重复经过一个点,不 ...