Ciel the Commander

Time Limit: 1000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 321C
64-bit integer IO format: %I64d      Java class name: (Any)

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 xand 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

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

Hint

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.

 

Source

 
解题:树分治
深度大于26,说明impossible
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
bool done[maxn];
vector<int>g[maxn];
char ans[maxn];
int sz[maxn],maxson[maxn];
int dfs(int u,int fa){
sz[u] = ;
maxson[u] = ;
for(int i = g[u].size()-; i >= ; --i){
if(g[u][i] == fa || done[g[u][i]]) continue;
dfs(g[u][i],u);
sz[u] += sz[g[u][i]];
maxson[u] = max(maxson[u],sz[g[u][i]]);
}
return sz[u];
}
int FindRoot(const int sum,int u,int fa){
int ret = u;
maxson[u] = max(maxson[u],sum - sz[u]);
for(int i = g[u].size()-; i >= ; --i){
if(g[u][i] == fa || done[g[u][i]]) continue;
int x = FindRoot(sum,g[u][i],u);
if(maxson[x] < maxson[ret]) ret = x;
}
return ret;
}
bool solve(int u,char ch){
int root = FindRoot(dfs(u,),u,);
done[root] = true;
ans[root] = ch;
if(ch > 'Z') return false;
for(int i = g[root].size()-; i >= ; --i){
if(done[g[root][i]]) continue;
if(!solve(g[root][i],ch + )) return false;
}
return true;
}
int main(){
int n,u,v;
while(~scanf("%d",&n)){
for(int i = ; i <= n; ++i){
done[i] = false;
g[i].clear();
}
for(int i = ; i < n; ++i){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
if(solve(,'A'))
for(int i = ; i <= n; ++i)
printf("%c%c",ans[i],i == n?'\n':' ');
else puts("Impossible!");
}
return ;
}

CodeForces 321C Ciel the Commander的更多相关文章

  1. Codeforces G. Ciel the Commander

    题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input stan ...

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

  3. CF 322E - Ciel the Commander 树的点分治

    树链剖分可以看成是树的边分治,什么是点分治呢? CF322E - Ciel the Commander 题目:给出一棵树,对于每个节点有一个等级(A-Z,A最高),如果两个不同的节点有相同等级的父节点 ...

  4. Codeforce 322E Ciel the Commander (点分治)

    E. Ciel the Commander Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, ...

  5. Ciel the Commander CodeForces - 321C (树, 思维)

    链接 大意: 给定n结点树, 求构造一种染色方案, 使得每个点颜色在[A,Z], 且端点同色的链中至少存在一点颜色大于端点 (A为最大颜色) 直接点分治即可, 因为最坏可以涂$2^{26}-1$个节点 ...

  6. 点分治 (等级排) codeforces 321C

    Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected ...

  7. Codeforces 321E Ciel and Gondolas

    传送门:http://codeforces.com/problemset/problem/321/E [题解] 首先有一个$O(n^2k)$的dp. # include <stdio.h> ...

  8. Codeforces 321D Ciel and Flipboard(结论题+枚举)

    题目链接   Ciel and Flipboard 题意  给出一个$n*n$的正方形,每个格子里有一个数,每次可以将一个大小为$x*x$的子正方形翻转 翻转的意义为该区域里的数都变成原来的相反数. ...

  9. codeforces B. Ciel and Flowers 解题报告

    题目链接:http://codeforces.com/problemset/problem/322/B 题目意思:给定红花.绿花和蓝花的朵数,问组成四种花束(3朵红花,3朵绿花,3朵蓝花,1朵红花+1 ...

随机推荐

  1. C# winwordcontrol控件编程

    C# word控件WinWordControl可创建.编辑.保存word. 1.使用代码直接创建word文件,同时可以添加页眉.内容.图片及表格,示例代码: /// <summary>   ...

  2. 用java自带jdk开发第一个java程序

    [学习笔记] 1.用java自带jdk开发第一个java程序:   下面要讲的eclipse要想正常工作,需要先学会配置这里的jdk.jdk要想正常工作,需先学会配置JAVA_HOME和ClassPa ...

  3. 洛谷 P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

    贪婪的送礼者Greedy Gift Givers 难度:☆ Code: #include <iostream> #include <cstdio> #include <c ...

  4. jenkins+phantomjs环境搭建及使用

    #jenkins+phantomjs 前端性能自动化测试的安装和使用#gcc GNU编译器套件 https://gcc.gnu.org/ #nginx 高性能的HTTP和反向代理服务器 http:// ...

  5. vba控制图表,excel图表,一键完成

    来源http://club.excelhome.net/thread-1417686-1-1.html 官方教程链接 https://docs.microsoft.com/zh-cn/office/v ...

  6. (一)mybatis之JDBC介绍

    前言:为什么在学mybatis之前要先了解JDBC呢?因为mybatis是以ORM模型为中心思想的框架,而所有的ORM模型都是基于JDBC进行封装的,不同的ORM模型对JDBC封装的强度是不一样的. ...

  7. 如何在ABAP里用函数式编程思想打印出非波拉契Fibonacci(数列)

    在JavaScript里可以用ES6提供的FunctionGenerator这种黑科技来打印非波拉契数列,具体细节参考我这篇文章. 在ABAP里也有很多种方式实现这个需求. 下面这个report分别用 ...

  8. leecode 旋转数组

    描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋 ...

  9. Codeforces C The Game of Efil (暴力枚举状态)

    http://codeforces.com/gym/100650 阅读题,边界的cell的邻居要当成一个环形的来算,时间有8s,状态最多2^16种,所以直接暴力枚举就行了.另外一种做法是逆推. #in ...

  10. dfs染色法判定二分图

    #include<iostream> #include<cstring> using namespace std; ][],color[],n; int dfs(int x,i ...