题目大意:
  给你一棵n个结点的树,给每个结点分级,最高为'A',最低为'Z'。
  尝试构造一种分级方案,使得任意两个相同级别的结点路径上至少有一个更高级的结点。

思路:
  贪心+树上点分。
  递归处理每一棵子树。
  对于每次处理的子树,把重心分成尽量高的级别。
  最后判一下够不够分。

 #include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int inf=0x7fffffff;
const int N=;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
char lev[N];
int size[N]={inf},center,min,size_tree;
char max_level;
bool mark[N];
void getsize(const int &x,const int &par) {
size[x]=;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par||mark[y]) continue;
getsize(y,x);
size[x]+=size[y];
}
}
void dfs(const int &x,const int &par) {
size[x]=;
int tmp=;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par||mark[y]) continue;
dfs(y,x);
tmp=std::max(tmp,size[y]);
size[x]+=size[y];
}
tmp=std::max(tmp,size_tree-size[x]);
if(tmp<min) {
min=tmp;
center=x;
}
}
inline int find(const int &x) {
min=inf;
size_tree=size[x];
dfs(x,);
return center;
}
void solve(const int &x,const char &level) {
getsize(x,);
int center=find(x);
mark[center]=true;
lev[center]=level;
max_level=std::max(max_level,level);
for(unsigned i=;i<e[center].size();i++) {
const int &y=e[center][i];
if(mark[y]) continue;
solve(y,level+);
}
}
int main() {
const int n=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
solve(,'A');
if(max_level>'Z') {
puts("Impossible!");
} else {
for(register int i=;i<=n;i++) {
printf("%c%c",lev[i]," \n"[i==n]);
}
}
return ;
}

[CF321C]Ciel the Commander的更多相关文章

  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. CF 322E - Ciel the Commander 树的点分治

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

  3. CodeForces 321C Ciel the Commander

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

  4. Codeforces G. Ciel the Commander

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

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

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

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

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

  7. 树上的构造 树分治+树重心的性质 Codeforces Round #190 (Div. 2) E

    http://codeforces.com/contest/322/problem/E E. Ciel the Commander time limit per test 1 second memor ...

  8. Codeforces Round #190 (Div. 1 + Div. 2)

    A. Ciel and Dancing 模拟. B. Ciel and Flowers 混合类型的数量只能为0.1.2,否则3个可以分成各种类型各自合成. C. Ciel and Robot 考虑一组 ...

  9. 如何修改Total Commander配件文件的位置

    今天测试了一下Total Commander最新版的安装文件,测试完成后,并删除.结果导致原先一直在使用的绿色版的Total Comander配件文件变成了测试的配件文件,导致许多配置都丢失了,因此不 ...

随机推荐

  1. selenium 获取某个元素的html

    <table> <tbody id="tb-37327761306"> <tr class="sep-row"><td ...

  2. How do I use EC2 Systems Manager to join an instance to my AWS Directory Service domain?

    1. Create new role "EC2RoleforSSM" in AWS IAM AWS->IAM->Roles->Create role->Se ...

  3. Java面向对象编程三大特性 --- 多态

    多态特性: 子类Child继承父类Father,我们可以编写一个指向子类的父类类型引用,该引用既可以处理父类Father对象,也可以处理子类Child对象,当相同的消息发送给子类或者父类对象时,该对象 ...

  4. Spring MVC 参数校验

    转自:http://blog.csdn.net/eson_15/article/details/51725470 这一篇博文主要总结一下springmvc中对数据的校验.在实际中,通常使用较多是前端的 ...

  5. sql数据库的链接方式

    今天看见了一个数据库的链接方法,给转载了,记得我刚刚学DAO的时候老是要记载这些东西,所以就上博客园上面看了看,就转过来了... MySQL: String Driver="com.mysq ...

  6. 【uva11468-Substring】AC自动机+dp

    http://acm.hust.edu.cn/vjudge/problem/31655 题意:给定k个模板串,n个字符以及选择它的概率pro[i],要构造一个长度问L的字符串s,问s不包含任意一个模板 ...

  7. ZOJ 3599 K倍动态减法游戏

    下面的文字辅助理解来自http://blog.csdn.net/tbl_123/article/details/24884861 博弈论中的 K倍动态减法游戏,难度较大,参看了好多资料才懵懂! 此题可 ...

  8. (七)计算G711语音的打包长度和RTP里timestamp的增长量

    如何计算G711语音等的打包长度和RTP里timestamp的增长量 一般对于不同的语音有不同的打包周期,而不同的打包周期又对应着不同的timestamp in RTP 那么是如何计算的呢,我们通过G ...

  9. 《linux下进程的创建,执行,监控和终止》

    <linux下进程的创建,执行,监控和终止> http://blog.csdn.net/miss_acha/article/details/43671047 http://blog.csd ...

  10. XML解析代码

    import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.LinkedLi ...