题目大意:
  给你一棵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. Lucene4.6 把时间信息写入倒排索引的Offset偏移量中,并实现按时间位置查询

    有个新的技术需求,需要对Lucene4.x的源码进行扩展,把如下的有时间位置的文本写入倒排索引,为此,我扩展了一个TimeTokenizer分词器,在这个分词器里将时间信息写入 偏移量Offset中. ...

  2. 51Nod-1586-约数和

    #include <cstdio> using namespace std; typedef long long ll; ; int n, q; int cnt[MAXN]; ll a[M ...

  3. MySql binlog(理论篇)

    1.什么是binlog? binlog日志用于记录所有更新了数据的sql语句或保存被修改的记录Row: 有了binlog,可以用于实时备份,master/slave主从同步: 在5.0版本前支持文本格 ...

  4. cube中的判断类型

    import { createAddAPI } from '../util' const DATE_RE = /^(1|2)\d{3}[.\-/]\d{1,2}[.\-/]\d{1,2}$/ cons ...

  5. Python 本地线程

    1. 本地线程,保证即使是多个线程,自己的值也是互相隔离. 2.普通对象演示 import threading import time class A(): pass a=A() def func(n ...

  6. 【bzoj4094】【洛谷3097】Optimal Milking

    假的,假的,都是假的. 题意是最大点独立集还要算贡献,写个网络流岂不是GG? 其实这个也就是奇偶不能选而已……所以无外乎这么四种情况: 左开右闭 左闭右开 都闭 都开 线段树按照套路维护一下就好了. ...

  7. linux用grep查找包含两个关键字的命令

    linux用grep查找包含两个关键字的命令 http://zhidao.baidu.com/link?url=VsFxeJXmU7W7hy1UH7eT6QAbUsVz9Ru2ABPuWYHWm4kB ...

  8. java Class.forName()

    Java程序在运行时,Java运行时系统一直对所有的对象进行所谓的运行时类型标识.这项信息纪录了每个对象所属的类. 虚拟机通常使用运行时类型信息选准正确方法去执行,用来保存这些类型信息的类是Class ...

  9. ORM中的N+1问题

    在orm中有一个经典的问题,那就是N+1问题,比如hibernate就有这个问题,这一般都是不可避免的. [N+1问题是怎么出现的] N+1一般出现在一对多查询中,下面以Group和User为例,Gr ...

  10. zabbix通过snmp监控vmware vpshere5.5

    https://www.iyunv.com/thread-516343-1-1.html