题目描述:

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

Copy

4
1 2
1 3
1 4

Output

Copy

A B B B

Input

Copy

10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

Output

Copy

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.

思路:

题目是给一个树,在树上的节点标号,要求是两个相同标号的连通的路径上必须有一个比他们等级高的标号。

刚开始时,这样想的:画了一幅图(很简单的),我把度数为一的标号为'Z',删掉这些点,又出现了度数唯一的点,标为'Y',再删掉,这样每次标记度数为一的点然后删到最后只剩两个点再特殊处理一下,如果这个过程中出现了字母用完的情况就说明不可能。但是这样做有个问题,其实字母用完了还是有可能继续完成标号的,我又改为字母用完了有倒序从'B'到'Z'标号,这样倒过来倒过去,还是错了,_(:з」∠)__

真正的做法是每次找树的重心,在重心处标号,根据重心定义(当前所有节点中最大的子树最小的节点),重心的最大子树的大小不会超过重心所在子树的一半。如果树退化成一条链,可以标记的节点数为\(1+2+4+...+2^{25}=2^{26}\)个节点,远远多于题目的限制。如果树不退化成链,那么可标记的点更多。因此必有解。

注意的是divide函数里面下一次divide是\(rt\)而不是\(v\)啊,血的教训啊w(゚Д゚)w,不是找的重心当然会把标记用光。

代码:

#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define max_n 200005
using namespace std;
int n;//n为节点数
char ans[max_n];
//链式前向星
int cnt = 0;
int head[max_n];
struct edge
{
int v;
int nxt;
}e[max_n<<1];
void add(int u,int v)
{
++cnt;
e[cnt].v=v;
e[cnt].nxt=head[u];
head[u]=cnt;
}
int rt,ms;//rt为重心,ms是最小 最大子树的大小
int Size;//当前整棵树的大小
int sz[max_n];//sz[i]表示以i为根的子树大小
int mson[max_n];//mson[i]表示以i的最大子树的大小
bool visit[max_n];//标记是否分治过 //求重心函数
void get_root(int u,int from)
{
sz[u]=1;mson[u]=0;//开始时以u为根的子树大小为1,u的最大子树为0
for(int i=head[u];i;i=e[i].nxt)//遍历与u相连边
{
int v=e[i].v;
if(visit[v]||v==from) continue;//防止重复遍历
get_root(v,u);
sz[u]+=sz[v];//更新以u为根的子树大小
mson[u]=max(mson[u],sz[v]);//更新u的最大子树
}
if(Size-sz[u]>mson[u]) mson[u]=Size-sz[u];//看是否另一半子树更大
if(ms>mson[u]) ms=mson[u],rt=u;//更新最小的最大子树和重心
} //求解答案函数
//分函数
void divide(int u,int ssize,int ch)
{
visit[u]=true;//当前节点已分治
ans[u] = ch;
for(int i = head[u];i;i=e[i].nxt)
{
int v=e[i].v;
if(visit[v]) continue;//已分治过的不用再分治
ms=INF;rt=0;//每一次求重心都要初始化这两个值
Size=sz[v]<sz[u]?sz[v]:ssize-sz[u];
get_root(v,0);//求出子树的重心
divide(rt,Size,ch+1);//分治子树
}
}
int main()
{
cin >> n;
for(int i = 1;i<n;i++)
{
int u,v;
cin >> u >> v;
add(u,v);
add(v,u);
}
Size=n;//开始为n整棵树大小
rt=0;ms=INF;
get_root(1,0);
/*for(int i = 1;i<=n;i++)
{
cout << "sz " << sz[i] << " mson " << mson[i] << endl;
}
cout << "rt " << rt << endl;*/
divide(rt,n,0);
for(int i = 1;i<=n;i++)
{
char chr = ans[i]+'A';
cout << chr << " ";
}
cout << endl;
return 0;
}

参考文章:

九野的博客,Codeforces 321C Ciel the Commander 树分治,https://blog.csdn.net/acmmmm/article/details/46931215

Codeforces G. Ciel the Commander的更多相关文章

  1. CodeForces 321C Ciel the Commander

    Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

  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 321E Ciel and Gondolas

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

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

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

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

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

  9. CodeForces 321A Ciel and Robot(数学模拟)

    题目链接:http://codeforces.com/problemset/problem/321/A 题意:在一个二维平面中,開始时在(0,0)点,目标点是(a.b),问能不能通过反复操作题目中的指 ...

随机推荐

  1. [原]部署kubernetes dashboard(二)

    #######################    以下为声明  ##################### 此文档是之前做笔记在两台机上进行的实践,kubernetes处于不断开发阶段 不能保证每 ...

  2. Caused by java.lang.Exception Failed to send data to Kafka Expiring

    flink 写kafka,报错,作业挂掉 Caused by: java.lang.Exception: Failed to send data to Kafka: Expiring 89 recor ...

  3. 【转】什么是5G?居然有人用漫画把它讲得如此接地气!

    最近一系列层出不穷的新闻,似乎都离不开一个关键词——5G.在各大报道中,都提到5G网络是移动无线技术的下一个重要发展. 任正非之前也在采访中说过: “5G,别人两三年也不会追上我们的.” “5G并不是 ...

  4. [ARM-Linux开发]Linux open函数

    Linux open函数 open 函数用于打开和创建文件.以下是 open 函数的简单描述 #include <fcntl.h> int open(const char *pathnam ...

  5. vue实现视频播放

    用的vue组件是vue-video-player,可以去百度下怎么引入到项目中: 因为这个组件是基于H5video实现的,视频格式有所限制,而且MP4不是H264的也不行.转码可以看下我上篇博客,地址 ...

  6. 浅谈SQL Server事务与锁(上篇)

    一  概述 在数据库方面,对于非DBA的程序员来说,事务与锁是一大难点,针对该难点,本篇文章试图采用图文的方式来与大家一起探讨. “浅谈SQL Server 事务与锁”这个专题共分两篇,上篇主讲事务及 ...

  7. js中常见的字符串方法(3)

    match() match()方法只接受一个参数,要么是一个正则表达式,要么是一个 RegExp 对象. 调用这个方法本质上与调用RegExp的exec()方法相同, var text = " ...

  8. 简单的爬虫程序以及使用PYQT进行界面设计(包含源码解析)

    由于这个是毕业设计的内容,而且还是跨专业的.爬虫程序肯定是很简单的,就是调用Yahoo的API进行爬取图片.这篇博客主要讲的是基础的界面设计. 放上源码,然后分部解析一下重要的地方.注:flickra ...

  9. 1. Spark GraphX概述

    1.1 什么是Spark GraphX Spark GraphX是一个分布式图处理框架,它是基于Spark平台提供对图计算和图挖掘简洁易用的而丰富的接口,极大的方便了对分布式图处理的需求.那么什么是图 ...

  10. android studio下 library打包文件(.aar)和本地引用

    关键点: 利用Gradle发布本地maven库支持android library 打包文件(*.aar) 的本地引用 开发环境: windows7 64位操作系统 android studio0.5. ...