http://codeforces.com/contest/322/problem/E

E. Ciel the Commander
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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!".

Examples
input
4
1 2
1 3
1 4
output
A B B B
input
10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
output
D C B A D C B D C D
Note

In the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution.

题目大意:题意:给出一棵树,给每一个点填上一个字母,要求是得任意两个相同字母的点u,v路径上至少有一个点大于这个字母(A最大)

思路:

'A'节点必然只有一个,且他的位置放在重心一定是最优的。(证明利用反证法证明)

然后我们就每次找重心即可。

(md我好菜啊,又不会构造)

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
int n;
vector<int> G[maxn];
int val[maxn], sz[maxn];
bool vis[maxn]; void dfs_sz(int u, int fa){
sz[u] = ;
for (int i = ; i < G[u].size(); i++){
int v = G[u][i];
if (v == fa || vis[v]) continue;
dfs_sz(v, u);
sz[u] += sz[v];
}
} void dfs_ce(int u, int fa, int &cetroid, int &maxcnt, int allcnt){
int tmp = allcnt - sz[u];
for (int i = ; i < G[u].size(); i++){
int v = G[u][i];
if (v == fa || vis[v]) continue;
dfs_ce(v, u, cetroid, maxcnt, allcnt);
tmp = max(tmp, sz[v]);
}
if (tmp < maxcnt) {cetroid = u, maxcnt = tmp;}
} void dfs(int u, int deep){
int cetroid, maxcnt = maxn * ;
dfs_sz(u, -);
dfs_ce(u, -, cetroid, maxcnt, sz[u]);
val[cetroid] = deep;
vis[cetroid] = true;
for (int i = ; i < G[cetroid].size(); i++){
int v = G[cetroid][i];
if(vis[v]) continue;
dfs(v, deep + );
}
vis[cetroid] = false;
} bool solve(){
dfs(, );
for (int i = ; i <= n; i++){
if (val[i] > ) return false;
}
for (int i = ; i <= n; i++){
val[i]--;
printf("%c ", val[i] + 'A');
}
cout << endl;
return true;
} int main(){
cin >> n;
for (int i = ; i < n; i++){
int u, v; scanf("%d%d", &u, &v);
G[u].pb(v), G[v].pb(u);
}
if (!solve()) puts("Impossible!");
return ;
}

树上的构造 树分治+树重心的性质 Codeforces Round #190 (Div. 2) E的更多相关文章

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

  2. 树的性质和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步,且这个步数是向上取 ...

  3. 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

    题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...

  4. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)

    Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...

  5. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  6. Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和

    Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description ...

  7. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 /* 题意:在n^n的海洋里是否有k块陆地 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 输出完k个L后,之后全部输出S:) 5 10 的例子可以是这样的: LSLS ...

  8. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  9. Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

    Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 ht ...

随机推荐

  1. 博弈---ZOJ 2083 Win the Game(染绳子)

    原题:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2083 大意:两个人分别对n条绳子染 每次染m长 最后染不下的输,问先 ...

  2. 《我是一只it小小鸟》观后感

    在这个学期开始的时候我们的老师推荐给我们这本书.在很多的网站上只要一提到IT,总会有人推荐这本书,我在读这本书之前看了很多关于它的书评,其中有一位网友的一句话让我对它产生了很大的兴趣:“印象最深的是书 ...

  3. Jquery获取属性值

    jq获取某个标签内的属性值:$("#TeamPerformanceYearUl li:eq(0)").attr('data') jq获取li或者td第一个属性(索引值从零开始)$( ...

  4. WPF和Expression Blend开发实例:Adorner(装饰器)应用实例

    装饰器-- 表示用于修饰 UIElement 的 FrameworkElement 的抽象类 简单来说就是,在不改变一个UIElement结构的情况下,将一个Visual对象加到它上面. 应用举例: ...

  5. laya3d 文件格式

    先认识下laya3d的一些文件 导出文件说明:               ls        ---    场景文件: Json文件,包含场景中所有节点的数据信息,包含光照贴图信息          ...

  6. 在Eclipse中开发WEB项目

    本文的演示是从本地文件创建dynamic web project,从svn检出的同时创建dynamic web project于此类似.我们推荐使用解压版的tomcat6.x版本,来作为服务器.可以到 ...

  7. Docker 入门 到部署Web 程序- (阿里面试常用的docker命令和优点)

    最近阿里的面试官问我Docker是做什么用的,我记得之前360和美团,京东的都问过,但是一直没时间看,最近有时间了,系统的学习了一下Docker,在此做一下记录,方便各位看官学习交流 一.Docker ...

  8. adb使用过程常见的几种错误总结

    问题1:Failure [INSTALL_FAILED_ALREADY_EXISTS] 问题原因:该程序已存在. 解决方法:增加-r参数,即可成功覆盖安装 问题2:Failure [INSTALL_F ...

  9. 第168天:json对象和字符串的相互转换

    json对象和字符串的相互转换 1.json对象和字符串的转换 在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和pa ...

  10. Asp.net MVC 获取IPv4 地址

    public static string GetIP4Address() { string IP4Address = String.Empty; foreach (IPAddress IPA in D ...