[CF321C]Ciel the Commander
题目大意:
给你一棵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的更多相关文章
- 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 ...
- CF 322E - Ciel the Commander 树的点分治
树链剖分可以看成是树的边分治,什么是点分治呢? CF322E - Ciel the Commander 题目:给出一棵树,对于每个节点有一个等级(A-Z,A最高),如果两个不同的节点有相同等级的父节点 ...
- CodeForces 321C Ciel the Commander
Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...
- Codeforces G. Ciel the Commander
题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforce 322E Ciel the Commander (点分治)
E. Ciel the Commander Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, ...
- Ciel the Commander CodeForces - 321C (树, 思维)
链接 大意: 给定n结点树, 求构造一种染色方案, 使得每个点颜色在[A,Z], 且端点同色的链中至少存在一点颜色大于端点 (A为最大颜色) 直接点分治即可, 因为最坏可以涂$2^{26}-1$个节点 ...
- 树上的构造 树分治+树重心的性质 Codeforces Round #190 (Div. 2) E
http://codeforces.com/contest/322/problem/E E. Ciel the Commander time limit per test 1 second memor ...
- Codeforces Round #190 (Div. 1 + Div. 2)
A. Ciel and Dancing 模拟. B. Ciel and Flowers 混合类型的数量只能为0.1.2,否则3个可以分成各种类型各自合成. C. Ciel and Robot 考虑一组 ...
- 如何修改Total Commander配件文件的位置
今天测试了一下Total Commander最新版的安装文件,测试完成后,并删除.结果导致原先一直在使用的绿色版的Total Comander配件文件变成了测试的配件文件,导致许多配置都丢失了,因此不 ...
随机推荐
- 480000 millis timeout while waiting for channel to be ready for write异常处理
2014-08-25 15:35:05,691 ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: DatanodeRegistration( ...
- 配置Tomcat时server.xml和content.xml自动还原问题
当我们在处理中文乱码或是配置数据源时,我们要修改Tomcat下的server.xml和content.xml文件. 但是当我们修改完后重启Tomcat服务器时发现xml文件又被还原了,修改无效果. 为 ...
- c++ STL(2)
Vector: #include "stdafx.h" #include<iostream> #include<vector> #include<al ...
- jquery中lhgdialog插件(一)
一:前言 最近在使用jquery的控件,其实以前也写但是突然之间遇到了需要从弹出窗口传值到父窗口,突然觉得这种传值的方式其实也是需要javascript的基础的,但是我自己还没有去真正的做过,所以还是 ...
- 2017南宁现场赛E The Champion
Bob is attending a chess competition. Now the competition is in the knockout phase. There are 2^r2r ...
- 【BZOJ1857】【SCOI2010】传送带 [三分]
传送带 Time Limit: 1 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 在一个2维平面上有两条传送带,每一条传送 ...
- C++高精度
整理了一下高精度,虽然可用java,但很多时候还是C++写的方便. 附上kuangbin神的高精度模板(HDU1134 求卡特兰数) #include <iostream> #includ ...
- ReadOnly与Enabled
txtDlrCode.ReadOnly = true; 1.当设置为只读,文本框有点击事件,点击该文本框还是可以响应点击事件 2.设置为只读,C#后台无法取得文本框的值,txtDlrCode.Text ...
- python日志模块笔记
前言 在应用中记录日志是程序开发的重要一环,也是调试的重要工具.但却很容易让人忽略.之前用flask写的一个服务就因为没有处理好日志的问题导致线上的错误难以察觉,修复错误的定位也很困难.最近恰好有时间 ...
- 搜索引擎--范例:新浪微博API获取最近的微博--statuses/public_timeline
新浪微博平台跟新浪SAE一样,都是一个字“坑”,好了,不再吐槽,直入主题 1:直接登录新浪,添加网站,就直接添加我们在新浪SAE上创建的应用即可 2:填写信息,ICP备案信息号填写sinaapp.co ...