从任意点出发,贪心染色即可。

#include<cstdio>
#include<algorithm>
using namespace std;
int v[200010<<1],next[200010<<1],first[200010],e;
void AddEdge(int U,int V)
{
v[++e]=V;
next[e]=first[U];
first[U]=e;
}
bool vis[200010];
int n,col[200010];
void dfs(int U,int n1,int n2)
{
vis[U]=1;
int co=0;
for(int i=first[U];i;i=next[i])
if(!vis[v[i]])
{
++co;
while(co==n1 || co==n2)
++co;
col[v[i]]=co;
dfs(v[i],col[U],col[v[i]]);
}
}
int main()
{
// freopen("c.in","r",stdin);
int x,y;
scanf("%d",&n);
for(int i=1;i<n;++i)
{
scanf("%d%d",&x,&y);
AddEdge(x,y);
AddEdge(y,x);
}
col[1]=1;
dfs(1,1,-1);
printf("%d\n",*max_element(col+1,col+n+1));
for(int i=1;i<n;++i)
printf("%d ",col[i]);
printf("%d\n",col[n]);
return 0;
}

【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons的更多相关文章

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

  2. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A. Andryusha and Socks

    地址:http://codeforces.com/contest/782/problem/A 题目: A. Andryusha and Socks time limit per test 2 seco ...

  3. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...

  4. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A模拟 B三分 C dfs D map

    A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)

    Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Mee ...

  6. 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E

    http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...

  7. 2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D

    http://codeforces.com/contest/782/problem/D 题意: 每个队有两种队名,问有没有满足以下两个条件的命名方法: ①任意两个队的名字不相同. ②若某个队 A 选用 ...

  8. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E Underground Lab

    地址:http://codeforces.com/contest/782/problem/E 题目: E. Underground Lab time limit per test 1 second m ...

  9. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League

    地址:http://codeforces.com/contest/782/problem/D 题目: D. Innokenty and a Football League time limit per ...

随机推荐

  1. ext radiogroup如何取值和设值

    var radios = Ext.create('Ext.form.Panel', { title: 'RadioGroup Example', width: 300, height: 125, bo ...

  2. HDU4280:Island Transport(最大流)

    Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. URAL - 1486 Equal Squares 二维哈希+二分

    During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...

  4. 两个神奇的函数~~~sscanf、atoi

    sscanf 对你没有看错 多了一个s 这个函数有什么作用呢 功能:读取字符串中的int.double.long.long long .float and so on 类型的数据 譬如说 我现在读入了 ...

  5. CSS垂直居中小结

    1.设置子元素: { ... position :absolute; margin:auto; top:; right:; bottom:; left:; } 2.设置子元素:(height必须是固定 ...

  6. 有关getClassLoader().getResourceAsStream(fileName)、class.getResourceAsStream(fileName)和().getContextClassLoader().getResourceAsStream(fileName)的区别

    一:前言 在自己获取属性时,碰见了XX.class.getResourceAsStream(fileName),自己对这个其实不是很理解,上网查了下资料,又看到了上述的几个,所以就研究了下. 二:内容 ...

  7. wiki 2490 导弹拦截塔

    2013-09-23 21:16 二分答案+匈牙利判断 对于每一个时间,我们重新建一张二分图,由于每个塔可能打多次,所以要拆点, 对于每个拆的点的可行飞行距离为(mid-t1)-(ll-1)*(t1+ ...

  8. 在ubuntu 上面安装ubuntu touch 模拟器

    Canonical 公司已经发布了一个运行着Unity8和Mir的Ubuntu Touch模拟器.虽然有一些bug,例如在64位的系统上会使系统崩溃,但我们相信这些都会被一 一修复,这篇文章将教大家如 ...

  9. Java坦克大战 (六) 之增加可玩性

    本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...

  10. Java==与equals方法的区别

    摘自:http://www.cnblogs.com/dolphin0520/p/3592500.html 1.对于==,如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等: 如果作用于引 ...