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. postman中添加cookie信息

    日常测试中有部分接口请求需要有登录信息,否则调用报错,那如何在postman中添加用户的cookie呢,主要分2个步骤: 第一步: Charles抓包获取Headers信息,拷贝Headers中的Co ...

  2. asp。Net 页面传值

    00.引言 Web页面是无状态的, 服务器对每一次请求都认为来自不同用户,因此,变量的状态在连续对同一页面的多次请求之间或在页面跳转时不会被保留.在用ASP.NET 设计开发一个Web系统时, 遇到一 ...

  3. 用户会话跟踪机制(session+cookie)

    最近在优化之前给学校写的一个项目,发现了同一个浏览器(IE,Firefox)开多个选项卡的时候不能登录多个用户,后一个登录用户会把前一个用户给覆盖了,我的登录逻辑是把user对象存放到session中 ...

  4. .net core API第一次试水

    这是我第一个API 项目他比普通的.NET CORE APPLICATION要多一个controllers层和appsetting.json 我们主要研究一下流程是怎么样的 在我尝试着改变launch ...

  5. PaaS基础学习(1)

    PaaS基础学习(1) PaaS学习笔记目录 PaaS基础学习(1) 在PaaS上开发Web.移动应用(2) PaaS优点与限制(3) 1. 基础单元,一个基础单元就是所研究实体的最小的不可分割的单元 ...

  6. window.close() 关闭当前浏览器页

    function eseFun() { var browserName = navigator.appName; //获取浏览器名称 if(browserName == "Netscape& ...

  7. 自己太水了—HDOJ_2212

    Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...

  8. 浅谈web前端开发

    我个人认为前端攻城狮其实就是编程技术人员,用一句话来形容“比UI设计懂技术,比技术人员更懂交互”,当然也有人说前端工程师是工程师中的设计师,是设计师中的工程师. 好了废话不多说了,下面进入正题吧!   ...

  9. 一个小笔记(5):A*算法

    A-Star算法是一种静态路网中求解最短路径最有效的直接搜索方法其实百科有 http://baike.baidu.com/link?url=CvmkWQIAmztYgMq3Nk1WyWkDiC0koV ...

  10. Encryption-基础:base64加解密

    环境:vc2003 .h /********** This library is free software; you can redistribute it and/or modify it und ...