codeforces781A Andryusha and Colored Balloons
本文版权归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的更多相关文章
- 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 782C. 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 ...
- 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, ...
- CF781A Andryusha and Colored Balloons
题意: Andryusha goes through a park each day. The squares and paths between them look boring to Andryu ...
- 782C. Andryusha and Colored Balloons DFS
Link 题意: 给出一棵树,要求为其染色,并且使任意节点都不与距离2以下的节点颜色相同 思路: 直接DFS.由某节点出发的DFS序列,对于其个儿子的cnt数+1,那么因为DFS遍历的性质可保证兄弟结 ...
- 【贪心】【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< ...
随机推荐
- org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x160fd6f04410017 after 0ms
dubbo报错: org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x160fd6f04410017 after ...
- json字符串和json对象的转换
http://www.json.org/提供了一个json2.js,这样ie8(兼容模式),ie7和ie6就可以支持JSON对象以及其stringify()和parse()方法: parse用于从一个 ...
- eslint Rules
Rules 为了让你对规则有个更好的理解,ESLint 对其进行了分门别类. 所有的规则默认都是禁用的.在配置文件中,使用 "extends": "eslint:reco ...
- maven 编译报错 java: -source 1.6 中不支持switch 中存在字符串
maven项目的pom文件里面添加 <build><defaultGoal>compile</defaultGoal><pluginManagement> ...
- 7.javascript如何调试代码
http://www.cnblogs.com/youring2/archive/2012/08/08/2624093.html
- Checksum 磁盘扇区故障检测
w https://en.wikipedia.org/wiki/Checksum https://zh.wikipedia.org/wiki/校验和 A checksum is a small-siz ...
- c++相关知识
0.C语言基础知识及系统相关:http://c.biancheng.net/cpp/u/jiaocheng/ 1.C++ include观点与机制:http://developer.51cto.com ...
- MySQL前后台交互登录系统设计
1.首先我们做一个前台的注册页面 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- pycharm调试scrapy
pycharm调试scrapy 创建一个run.py文件作为调试入口 run.py中,name是要调试的爬虫的名字(注意,是爬虫类中的name,而不是爬虫类所在文件的名字) 拼接爬虫运行的命令,然后用 ...
- 报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Outline SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: trunc ...