Codeforces Round #190 (Div. 2) E. Ciel the Commander 点分治
2015-12-07 19:58
351 查看
E. Ciel the Commander
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://www.codeforces.com/contest/322/problem/EDescription
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]=1;f[x]=0; for(int i=0;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]=1; for(int i=0;i<E[x].size();i++) { int v = E[x][i]; if(vis[v])continue; sum=son[v],root=0; getroot(v,x); work(root,x,dep+1); } } int main() { int n; scanf("%d",&n); for(int i=1;i<n;i++) { int x,y;scanf("%d%d",&x,&y); E[x].push_back(y); E[y].push_back(x); } f[0]=sum=n; getroot(1,0); work(root,0,0); for(int i=1;i<=n;i++) printf("%c ",ans[i]+'A'); printf("\n"); return 0; }
相关文章推荐
- C#操作XML的方法
- ACM学习历程—UVALive 7147 World Cup(分类讨论 && 贪心)
- Lawnmower
- 监控MySQL主从同步延时
- 做spark的一点小感悟
- 发现C++Builder 2010一组类BUG
- POJ 1488 TEX Quotes
- hdu1180诡异的楼梯【优先队列+广搜】
- css 里面 ie8 支持css3的rgba()
- 2015第50周一了解微服务架构
- 【iOS】Assertion failure in -[MASViewConstraint install]
- java中简单工厂模式(静态工厂方法模式)
- 我的Android进阶之旅------>Android【设置】-【语言和输入法】-【语言】列表中找到相应语言所对应的列表项
- 计算机视觉领域稍微容易中的期刊
- VFS主要数据结构与sys_read浅析
- 获取英文字符串的首字母和汉字字符串的拼音首字母
- NGUI里使用UIDragDropItem的OnDragDropRelease时,surface总是显示UI Root的问题解决
- CCF真题之图像旋转
- OpenJudge_P1761 神奇的口袋(2)
- Mysql Explain 详解