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< ...
 
随机推荐
- 表达式求值(java)
			
今天去面试,考了这个,短时间没想出来... 太笨了! 后来想用栈和递归做 但是看了网上才知道,可以将中缀表达式转为后缀表达式,就极其方便了. import java.util.Scanner; imp ...
 - PAT 1013 Battle Over Cities(并查集)
			
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
 - UNION ALL与UNION
			
UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...
 - 决策树ID3算法python实现 -- 《机器学习实战》
			
from math import log import numpy as np import matplotlib.pyplot as plt import operator #计算给定数据集的香农熵 ...
 - docker,构建nginx反向代理tomcat
			
Nginx实现负载均衡是通过配置nginx.conf来实现的,nginx.conf的全部内容如下: user nginx; worker_processes 1; error_log /var/log ...
 - Ionic的下拉框在手机上点击无效
			
最近在维护ionic+angular的项目,在浏览器使用下拉框的时候调试的时候,一切正常. 但是在手机上测试的时候,遇到这个问题. 我使用的版本是ionic1.3.1,不知道新版本有没有解决这个bug ...
 - C#连接EXCEL和ACCESS字符串2003及2007版
			
97-2003版本 EXCEL Provider=Microsoft.Jet.OLEDB.4.0;Data Source=文件位置;ExtendedProperties=Excel 8.0;HDR=Y ...
 - MySQL之表的约束
			
一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...
 - linux下Pl353 NAND Flash驱动分析
			
linux的NAND Flash驱动位于drivers/mtd/nand子文件夹下: nand_base.c-->定义通用的nand flash基本操作函数,如读写page,可自己重写这些函数 ...
 - Python 模块之 xlrd  (Excel文件读写)
			
# 1. 导入模块 import xlrd # 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls') # 3. 使用技巧 # 3.1 获 ...