本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:codeforces781A Andryusha and Colored Balloons

正解:构造+结论

解题报告:

  考虑答案显然是$max(度数)+1$,这个似乎很好想,对于每个点我需要保证与之相邻的所有点颜色互不相同且与自己不同,那么需要度数$+1$种颜色,可以证明答案就是上式。

  那么我们只需要构造即可,随便找一个根$dfs$,保证每个儿子节点与当前节点和父亲节点的颜色不同即可。

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 200011;
const int MAXM = 400011;
int n,ecnt,first[MAXN],to[MAXM],next[MAXM],d[MAXN],col[MAXN],ans;
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void dfs(int x,int fa){
int cc=0;
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
cc++;
if(cc==col[fa]) cc++;
if(cc==col[x]) cc++;
if(cc==col[fa]) cc++;
col[v]=cc;
dfs(v,x);
}
} inline void work(){
n=getint(); int x,y;
for(int i=1;i<n;i++) {
x=getint(); y=getint();
link(x,y); link(y,x);
d[x]++; d[y]++;
}
int pos=1;
for(int i=2;i<=n;i++) if(d[i]>d[pos]) pos=i;
ans=d[pos]+1;
printf("%d\n",ans); col[1]=1;
dfs(1,0);
for(int i=1;i<=n;i++) printf("%d ",col[i]);
} int main()
{
work();
return 0;
}

  

codeforces781A Andryusha and Colored Balloons的更多相关文章

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

  2. Codeforces 782C. 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. AC日记——Andryusha and Colored Balloons codeforces 780c

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

  5. C. Andryusha and Colored Balloons

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

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

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

  7. CF781A Andryusha and Colored Balloons

    题意: Andryusha goes through a park each day. The squares and paths between them look boring to Andryu ...

  8. 782C. Andryusha and Colored Balloons DFS

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

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

随机推荐

  1. 带jsk证书,请求https接口

    首先是三个返回的实体类 BaseVo.java package https2; import java.io.Serializable; import java.lang.reflect.Invoca ...

  2. Less-css基础扩展

    //扩展Extend less的伪类,合并了选择器,放在与它引用匹配的选择器上 Use Method:以在study上扩展test的样式为例 .test{ color:#000000; font-si ...

  3. IE11 Enterprise Mode Template missing from GPMC

    IE11 Enterprise Mode Template missing from GPMC     Reason:You have not copied the new IE11 Enterpri ...

  4. 模块 - time/datetime

    time 模块 time模块方法: >>> import time >>> time.time() #时间戳 秒级别 1519212085.6211221 #从19 ...

  5. FatMouse's Speed---hdu1160(简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意就是给你一些老鼠(编号1,2,3,4,5,6,7,8...)的体重和他们的速度然后求出最大的 ...

  6. git学习------>如何用git log命令来查看某个指定文件的提交历史记录

    有时候接手一份新代码时,看到某些文件的改动,但不清楚这个改动的作者和原因,想查看该文件的具体提交历史记录. 今天一个同事是这样做的,直接敲git log命令,然后再使用vim命令的搜索关键字的方法来查 ...

  7. C++和JAVA实例化对象的区别

    JAVA: A a = new A(); 为A对象创建了一个实例,但在内存中开辟了两块空间:一块空间在堆区,存放new A()这个对象:另一块空间在堆栈,也就是栈,存放a,a的值为new A()这个对 ...

  8. 002-主流区块链技术特点及Hyperledger Fabric V0.6版本特点

    一.主流区块链技术特点 二.HyperLedger子项目 三.Hyperledger fabric架构 V0.6逻辑架构: V0.6区块链网络 对应的0.6版本的运行时架构: 0.6版本的架构特点是: ...

  9. 用仿ActionScript的语法来编写html5——第二篇,利用Sprite来实现动画

    上一篇,我已经模仿as,加入了LBitmap和LBitmapData类,并且用它们实现了静态图片的显示.这次用Sprite来动态显示图片.依然遵循上一篇对显示对象的处理的思路,添加LSprite类,并 ...

  10. 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数

    错误 4317 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”.请改用适用的接口. 类型“Microsoft.Office.Inte ...