http://codeforces.com/contest/782/problem/C

题意:给一棵树染最少的颜色,使得相邻距离为2的点都是不同的颜色,问最少是多少种颜色并输出每个点的颜色。

思路:比赛的时候没想到是找度最大的一个点并+1就是总颜色数,一直想怎么构造。

最后的总颜色数是度最大的一个点的度数+1。因为我们选的这个点到儿子的距离为1,因此其某一个儿子到另一个儿子的距离为2,就是这些儿子都要染成不同的颜色,+1是因为自己本身也要是不同的颜色。

然后确定了总颜色数,就可以DFS染色了。

参数记录一个上上个结点和上个结点染了什么颜色,只要儿子染成不同的就可以了,注意儿子之间颜色也要不同。

 #include <bits/stdc++.h>
using namespace std;
#define N 200010
struct Edge {
int v, nxt;
} edge[N*];
int head[N], tot, col[N], deg[N], ans; void Add(int u, int v) {
edge[tot] = (Edge) {v, head[u]}; head[u] = tot++;
edge[tot] = (Edge) {u, head[v]}; head[v] = tot++;
} void dfs(int u, int fa, int c1, int c2) {
int c = ;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v == fa) continue;
if(col[v]) continue;
for( ; c <= ans; c++)
if(c != c1 && c != c2) {
col[v] = c; break;
}
c++;
dfs(v, u, col[v], col[u]);
}
} int main() {
int n;
scanf("%d", &n);
memset(head, -, sizeof(head));
for(int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
deg[u]++; deg[v]++;
Add(u, v);
}
ans = ;
for(int i = ; i <= n; i++) if(ans < deg[i] + ) ans = deg[i] + ; col[] = ;
dfs(, -, , -);
printf("%d\n", ans);
for(int i = ; i <= n; i++) printf("%d ", col[i]);
return ;
}

Codeforces 781A: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. CodeForces - 780C Andryusha and Colored Balloons(dfs染色)

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

  3. 782C. Andryusha and Colored Balloons DFS

    Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...

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

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

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

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

  7. codeforces781A Andryusha and Colored Balloons

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

  8. C. Andryusha and Colored Balloons

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

  9. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

随机推荐

  1. QT Linux Demo程序编译

    我手上的qt源码包为:qt-everywhere-opensource-src-4.7.0.tar.gz 在Linux下编译比较容易,解压后直接 ./configure,一般会报缺少什么库这些.自己遇 ...

  2. WPF ObjectDataProvider的使用-只能检索用

    <Window x:Class="CollectionBinding.MainWindow"        xmlns="http://schemas.micros ...

  3. Selenium-等待

    分为3种 (1)就是通过线程强制等待 Thread.sleep(1000); (2)隐示等待.就是所有的命令都等待.分为3种 // 这个方法表示全局的等待.意思是针对所有的findElement方法都 ...

  4. .NET CORE EnvironmentVariable

    .NET CORE System variables SETIn System variablese.g1:Variable name: ASPNETCORE_ENVIRONMENTVariable ...

  5. Debug权限提升

    procedure SetPrivilege; var OldTokenPrivileges, TokenPrivileges: TTokenPrivileges; ReturnLength: dwo ...

  6. MinDoc v0.6 发布,轻量级文档在线管理系统

    更新日志 新增 标签功能,可以根据标签组织项目 新增 用户删除功能,删除后的用户项目以及其他数据会自动转移到超级管理员账户上 新增 项目描述支持Markdown语法 优化 项目标签添加效果 优化 登录 ...

  7. sqlserver从xlsx读取数据

    exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Querie ...

  8. 【C】用C语言提取bmp图片像素,并进行K-means聚类分析——容易遇到的问题

    关于bmp图片的格式,网上有很多文章,具体可以参考百度百科,也有例子程序.这里只提要注意的问题. (1)结构体定义问题:首先按照百度百科介绍的定义了结构体,但是编译发现重定义BITMAPFILEHEA ...

  9. Android零基础入门第38节:初识Adapter

    原文:Android零基础入门第38节:初识Adapter 在上一节一起了解了ListView的简单使用,那么本节继续来学习与ListView有着千丝万缕的Adapter. 一.了解MVC模式 在开始 ...

  10. 管道通信实例(A程序作为服务器,不断从B程序接收数据,并发送到C程序中)

    A程序作为服务器,不断从B程序接收数据,并发送到C程序中:#include <stdio.h>#include <conio.h> #include <tchar.h&g ...