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 ...
随机推荐
- Android Application 深入分析
http://blog.csdn.net/rain_butterfly/article/details/37598939
- Drupal如何处理系统变量?
Drupal的系统变量都保存在数据库variable表中: 然后,开发人员可以通过下面的API函数操作这些系统变量: function variable_get($name, $default = N ...
- cocos2d-x CCEditBox 字符不能显示完全的bug
cocos2d-x CCEditBox 字符不能显示完全的bug (cocos2dx版本 2.2.0)用CCEditBox制作帐号输入框,当输入的内容超过框的宽度时,框里面不会显示当前输入的字符,显示 ...
- matlab的&和&&操作
A&B(1)首先判断A的逻辑值,然后判断B的值,然后进行逻辑与的计算.(2)A和B可以为矩阵(e.g. A=[1 0],B=[0 0]).A&&B(1)首先判断A的逻辑值,如果 ...
- SpringMVC + Spring + MyBatis 学习笔记:为MyBatis增加打印SQL功能 (最简化配置)
系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 1.以下jar包拷贝到 ...
- CSS width:100%和width:auto的区别
width:100%和width:auto的区别 width:auto比较聪明,如果margin已经左右占去10px的空间,那么width给的值就是580px. <style> div{ ...
- Ruby on Rails vs. PHP vs. Python
开发者在开发web应用时,往往会对平台的选择感到困惑,而web专家通常会建议:要考虑几个因素,这些因素包括周转时间.质量.跨浏览器兼容性.与其他框架的整合.数据安全性.易于访问性等. 在考虑了这些因素 ...
- IOS自动布局
参考资料 https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AutolayoutPG/Vi ...
- Compiling Xen-4.4 From Source And Installing It On Ubuntu Server (Amd-64)
First of all, you should install a clean Ubuntu Server (Amd-64) on your server. (Version 14.04 is st ...
- collectionView/tableview刷新时关闭动画无效
collectionView/tableview reloadSections/reloaddata时去掉动画无效时可以尝试使用 [UIView performWithoutAnimation:^{ ...