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 ...
随机推荐
- 继承TextView简单画一个尺子
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; impor ...
- nmon基础
nmon是分析 AIX 和 Linux 性能的免费工具 最简单的安装方式(Ubuntu apt源) sudo apt-get install nmon 在terminal下打开nmon 敲CMD,出现 ...
- php sortable 动态排序
php sortable 动态排序未分页版.php 预览图: <?php mysql_connect("localhost","root","r ...
- 虚幻4以及DX12将允许开发者利用Xbox One的更多性能(转)
GamingBolt 最近采访了 Epic Games 的总经理 Ray Davis,讨论了有关旗下虚幻引擎(Unreal Engine)的议题.在这次的访谈中,Ray 解释了关于使用 DirectX ...
- js遇到这样基础题,看你能不能作对呢
var a = (function() { return typeof arguments; })(); alert(a); //Object var b = (function(x) { delet ...
- TCP/IP 相关知识点与面试题集
第一部分:TCP/IP相关知识点 对TCP/IP的整体认 链路层知识点 IP层知识点 运输层知识点 应用层知识点 (这些知识点都可以参考:http://www.cnblogs.com/newwy/p/ ...
- Web服务器与Servlet容器
今日要闻: Oracle启动了JRE7到JRE8的自动更新, JRE8发布于2014.3,于2014.10成为java.com默认版本, JRE7发布于2011.7, Oracle指定的Java生命政 ...
- 浅谈Spark(1) - Overview
Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用并行框架,Spark,拥有Hadoop MapReduce所具有的优点:但不同于MapReduce的是 ...
- Microsoft Azure云计算第一步—试用帐户申请
从本文开始,将会对Microsoft Azure云从Iaas, Paas, Saas三种类型的云应用通过文章进行介绍.千里之行,始于帐户:),如果大家需要申请免费试用帐户请参考本文. 对于直接付钱的壕 ...
- java 编写hadoop程序中使用第三方libxx.so库
在使用java编写hadoop处理程序时遇到了,java使用依赖的第三方libxx.so库的情况,找到了一种可行的方法,记录一下,希望对别人也有帮助: 加入需要使用的lib库为libxxx.so 1. ...