Codeforces Round #190 (Div. 2) E. Ciel the Commander 点分治
E. Ciel the Commander
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://www.codeforces.com/contest/322/problem/E
Description
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n - 1 undirected roads, and for any two cities there always exists a path between them.
Fox Ciel needs to assign an officer to each city. Each officer has a rank — a letter from 'A' to 'Z'. So there will be 26 different ranks, and 'A' is the topmost, so 'Z' is the bottommost.
There are enough officers of each rank. But there is a special rule must obey: if x and y are two distinct cities and their officers have the same rank, then on the simple path between x and y there must be a city z that has an officer with higher rank. The rule guarantee that a communications between same rank officers will be monitored by higher rank officer.
Help Ciel to make a valid plan, and if it's impossible, output "Impossible!".
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of cities in Tree Land.
Each of the following n - 1 lines contains two integers a and b (1 ≤ a, b ≤ n, a ≠ b) — they mean that there will be an undirected road between a and b. Consider all the cities are numbered from 1 to n.
It guaranteed that the given graph will be a tree.
Output
If there is a valid plane, output n space-separated characters in a line — i-th character is the rank of officer in the city with number i.
Otherwise output "Impossible!".
Sample Input
4
1 2
1 3
1 4
Sample Output
A B B B
HINT
题意
给你一棵树,然后让你给每一个顶点标等级,使得每一个相同等级的顶点的简单路径之间,必定存在一个顶点等级比他们两个低
等级从A-Z,一共26种
题解:
采用树分治做,每次找到树的重心。
为什么找重心呢?我们从一条链的情况上来看,肯定越小的放在越重心越好,然后利用这个性质不停分治就行了
代码:
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
#define maxn 100005
int vis[maxn],son[maxn],f[maxn],sum,root,ans[maxn];
vector<int> E[maxn];
void getroot(int x,int fa)
{
son[x]=;f[x]=;
for(int i=;i<E[x].size();i++)
{
int v = E[x][i];
if(v == fa || vis[v])continue;
getroot(v,x);
son[x]+=son[v];
f[x]=max(f[x],son[v]);
}
f[x]=max(f[x],sum-son[x]);
if(f[x]<f[root])root=x;
}
void work(int x,int fa,int dep)
{
ans[x]=dep;
vis[x]=;
for(int i=;i<E[x].size();i++)
{
int v = E[x][i];
if(vis[v])continue;
sum=son[v],root=;
getroot(v,x);
work(root,x,dep+);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].push_back(y);
E[y].push_back(x);
}
f[]=sum=n;
getroot(,);
work(root,,);
for(int i=;i<=n;i++)
printf("%c ",ans[i]+'A');
printf("\n");
return ;
}
Codeforces Round #190 (Div. 2) E. Ciel the Commander 点分治的更多相关文章
- Codeforces Round #190 (Div. 2) B. Ciel and Flowers
链接:http://codeforces.com/contest/322/problem/B 这题做错了.没考虑周全. #include <cstdio> #include <cst ...
- Codeforces Round #190 DIV.2 A. Ciel and Dancing
#include <cstdio> #include <iostream> #include <vector> using namespace std; int m ...
- 树上的构造 树分治+树重心的性质 Codeforces Round #190 (Div. 2) E
http://codeforces.com/contest/322/problem/E E. Ciel the Commander time limit per test 1 second memor ...
- Codeforces Round #190 (Div. 1 + Div. 2)
A. Ciel and Dancing 模拟. B. Ciel and Flowers 混合类型的数量只能为0.1.2,否则3个可以分成各种类型各自合成. C. Ciel and Robot 考虑一组 ...
- Codeforces Round 190 div.2 322C 321A Ciel and Robot
唔...这题是数学题. 比赛时做出来,但题意理解错了,以为只要判断那点是不是在线上就行了,发现过不了样例就没提交. 思路:记录每一步的偏移,假设那点是在路径上的某步,然后回推出那一个周期的第一步,判断 ...
- Codeforces Round #190 (Div. 2).D
一道贪心题. 可以分两种情况 1 .是没有把对面的牌全打败,那么只要用最大的可能去打攻击状态的牌. 2. 是将对面的牌全打败,那么只要保证打对面防守状态的花费最小,就可以保证最后的结果最大 两种情况下 ...
- Codeforces Round #190 (Div. 2) 水果俩水题
后天考试,今天做题,我真佩服自己... 这次又只A俩水题... orz各路神犇... 话说这次模拟题挺多... 半个多小时把前面俩水题做完,然后卡C,和往常一样,题目看懂做不出来... A: 算是模拟 ...
- [置顶] Codeforces Round #190 (Div. 2)(完全)
好久没有写博客了,一直找不到有意义的题可以写,这次也不算多么有意义,只是今天是比较空的一天,趁这个时候写一写. A. B. 有一点贪心,先把每个拿去3的倍数,余下0或1或2,然后三个一起拿. 对于以上 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- Dapper的完整扩展(转)
真心想说:其实...我不想用Dapper,如果OrmLite.Net支持参数化的话,也就没Dapper的什么事情了,对于OrmLite.Net只能做后续跟踪...... 这个其实是看了Dapper作者 ...
- LoadRunner error -27257
检查点函数 web_reg_find("Search=body", "savecount=num", "Text=test1", LAST) ...
- 白盒测试之初识gtest工具
因为公司最近对软件产品质量提高了要求,之前项目组中黑盒测试方法就越来越无法满足公司的要求.虽然作为研发,但是也要求对白盒测试有一个系统的了解(毕竟之前没有系统的接触过白盒测试). 单元测试工具有很多种 ...
- opencv3.0 在 android 上的使用
下载 OpenCV-3.0.0-android-sdk-1.zip 打开 intellj,新建立一个 android 工程后选择工程属性,导入模块(Import module from externa ...
- 一.JSP开发的工具下载与环境搭建
JSP技术的强势: (1)一次编写,到处运行.在这一点上Java比PHP更出色,除了系统之外,代码不用做任何更改. (2)系统的多平台支持.基本上可以在所有平台上的任意环境中开发,在任意环境中进行系统 ...
- 【LeetCode】58 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Spark及其应用场景初探
最近老大让用Spark做一个ETL项目,搭建了一套只有三个结点Standalone模式的Spark集群做测试,基础数据量大概8000W左右.看了官方文档,Spark确实在Map-Reduce上提升了很 ...
- Numpy中的矩阵计算
矩阵初始化 支持matlab语句初始化,支持narray和array初始化. >>> import numpy as np >>> M = np.matrix(&q ...
- ext 参考资料
http://extjs.org.cn/ 中文网站 http://www.sencha.com/ 英文网站 http://www.qeefee.com 个人总结
- 安装CiscoWorks LMS
安装CiscoWorks LMS,需要安装5张光盘,CiscoWorks LMS的安装步骤比较简单,在这里只介绍需要的重要步骤. 1.安装CD One 将CiscoWorks 2000 LMS CD ...