点分治 (等级排) codeforces 321C
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!".
Example
4
1 2
1 3
1 4
A B B B
10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
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.
题目分析 : 给你一棵树,A,B,C... 表示等级,现要求两个相同等级的之间必须有一个比它大的字母,输出所有的字母序
思路分析 : 每次找树的重心,将它标记一个字母即可,因为找重心每次是减少一半的点,因此最多可以标记整个树是 2^25 个点的树
代码示例 :
const int maxn = 1e5+5;
const int inf = 0x3f3f3f3f;
#define ll long long int num = 0;
vector<int>ve[maxn];
int balance, root;
bool done[maxn];
int size[maxn], mx[maxn];
char ans[maxn]; void getroot(int x, int fa){
size[x] = 1, mx[x] = 0; //以当前结点为根节点的最大结点个数 for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i]; if (to == fa || done[to]) continue;
getroot(to, x);
size[x] += size[to];
mx[x] = max(mx[x], size[to]);
}
mx[x] = max(mx[x], num-size[x]);
if (mx[x] < balance) {balance = mx[x], root = x;}
} void dfs(int x, int k){
done[x] = true;
ans[x] = 'A'+k; for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i]; if (done[to]) continue;
balance = inf, num = size[to];
getroot(to, to);
dfs(root, k+1);
}
} int main() {
int n;
int a, b; cin >> n;
for(int i = 1; i < n; i++){
scanf("%d%d", &a, &b);
ve[a].push_back(b);
ve[b].push_back(a);
}
memset(done, false, sizeof(done));
balance = inf, num = n;
getroot(1, 1);
//printf("root = %d\n", root);
dfs(root, 0);
for(int i = 1; i <= n; i++) printf("%c%c",ans[i], i==n?'\n':' ');
return 0;
}
点分治 (等级排) codeforces 321C的更多相关文章
- 奇袭(单调栈+分治+桶排)(20190716 NOIP模拟测试4)
C. 奇袭 题目类型:传统 评测方式:文本比较 内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目描述 由于各种原因,桐人现在被困在Under World(以下简称UW)中,而 ...
- CodeForces 321C Ciel the Commander
Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...
- Ciel the Commander CodeForces - 321C (树, 思维)
链接 大意: 给定n结点树, 求构造一种染色方案, 使得每个点颜色在[A,Z], 且端点同色的链中至少存在一点颜色大于端点 (A为最大颜色) 直接点分治即可, 因为最坏可以涂$2^{26}-1$个节点 ...
- Codeforces G. Ciel the Commander
题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Day1下午
T1 暴力50分 排A和B X,不用考虑X 用数组80分, 权值线段树.平衡树100, 一个函数? T2 打表 dp logn+1,+ 搜索,dp? txt..... T3 30分暴力和尽量均 ...
- 0x08 总结与练习
1:前面已经搞好了. 2:poj2965 这种开关问题一个点要么点一次要么不点,枚举所有点的方案实行即可 #include<cstdio> #include<iostream> ...
- 使用分析函数实现Oracle 10G提供的CONNECT_BY_ISLEAF和CONNECT_BY_ROOT的功能(转载)
文章转载至:http://blog.csdn.net/wzy0623/article/details/1644049 如果,有侵犯您权益的地方,烦请及时的告知我,我会即刻停止侵权行为 Oracle 1 ...
- Community宣言
Community宣言 一个幽灵,共产主义的幽灵,在欧洲游荡.为了对这个幽灵进行神圣的围剿,旧欧洲的一切势力,教皇和沙皇.梅特涅和基佐.法国的激进派和德国的警察,都联合起来了. 有哪一个反对党不被它的 ...
- 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror) Solution
从这里开始 题目列表 瞎扯 Problem A Find a Number Problem B Berkomnadzor Problem C Cloud Computing Problem D Gar ...
随机推荐
- P1095 水仙花数
题目描述 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:"水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3 ...
- 如何在 centos 7.3 上安装 caffe 深度学习工具
有好多朋友在安装 caffe 时遇到不少问题.(看文章的朋友希望关心一下我的创业项目趣智思成) 今天测试并整理一下安装过程.我是在阿里云上测试,选择centos 7.3 镜像. 先安装 epel 源 ...
- P1019 聪聪理扑克
题目描述 聪聪的两个小伙伴灵灵和豪豪喜欢打扑克,什么斗地主.德州.牛牛,他们都玩的有模有样. 但是每次玩好扑克他们都不整理一下,所以整理扑克的任务就交到了聪聪的手上. 已知现在桌面上有 n 张扑克牌, ...
- Nginx与PHP交互过程 + Nginx与PHP通信的两种方式
一.Nginx与PHP交互过程的7步走(用户对动态PHP网页访问过程) step1:用户将http请求发送给nginx服务器(用户和nginx服务器进行三次握手进行TCP连接) step2:nginx ...
- [Linux] 使用awk比较两个文件的内容
当需要比较A , B两个文件 , A文件中存在 , 并且把也在B文件中存在的行去除掉 , 可以使用这个awk的用法来 awk '{if(ARGIND==1) {val[$0]}else{if($0 ...
- 红米K20 Pro 传感器失效解决方案
最近把Redmi K20 Pro升级到了安卓Q,用了几天,不好用,总是提示屏幕前方有遮挡,于是刷回了安卓9,但是发现重力感应传感器失效了,屏幕不会随这方向转,导航的时候看起来很难受. 网上查了一下,要 ...
- slim的中间件
slim中间件的作用简单来说就是过滤数据,request过来的数据要经过中间件才能到达内部,然后内部数据要到达外部的时候,也要经过中间件,正常通过才能到达外部
- MyBatis整合Spring MVC(易百教程)
MyBatis是ibatis的升级版,作为hibernate的老对手,它是一个可以自定义SQL.存储过程和高级映射的持久层框架.与Hibernate 的主要区别就是 Mybatis 是半自动化的,而 ...
- spring boot 中事物的使用
一.什么是事务? 事务,通俗的说就是,同时做多个事,要么全做,要么不做,也是其特性.举个例子来说,好比你在某宝.某东.某多上购物,在你提交订单的时候,库存也会相应减少,不可能是钱付了,库存不减少,或者 ...
- linux权限说明
Linux 中的权限 这里我拿一个tomcat目录举例说明.首先我们在linux系统中执行命令:ls -l 我们拿出其中一个说明-rwxr-xr-x,在Linux 系统中权限是区分用户的,即用户.组用 ...