CodeForces 321C Ciel the Commander
Ciel the Commander
This problem will be judged on CodeForces. Original ID: 321C
64-bit integer IO format: %I64d Java class name: (Any)
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 xand 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!".
Sample Input
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
Hint
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.
Source
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
bool done[maxn];
vector<int>g[maxn];
char ans[maxn];
int sz[maxn],maxson[maxn];
int dfs(int u,int fa){
sz[u] = ;
maxson[u] = ;
for(int i = g[u].size()-; i >= ; --i){
if(g[u][i] == fa || done[g[u][i]]) continue;
dfs(g[u][i],u);
sz[u] += sz[g[u][i]];
maxson[u] = max(maxson[u],sz[g[u][i]]);
}
return sz[u];
}
int FindRoot(const int sum,int u,int fa){
int ret = u;
maxson[u] = max(maxson[u],sum - sz[u]);
for(int i = g[u].size()-; i >= ; --i){
if(g[u][i] == fa || done[g[u][i]]) continue;
int x = FindRoot(sum,g[u][i],u);
if(maxson[x] < maxson[ret]) ret = x;
}
return ret;
}
bool solve(int u,char ch){
int root = FindRoot(dfs(u,),u,);
done[root] = true;
ans[root] = ch;
if(ch > 'Z') return false;
for(int i = g[root].size()-; i >= ; --i){
if(done[g[root][i]]) continue;
if(!solve(g[root][i],ch + )) return false;
}
return true;
}
int main(){
int n,u,v;
while(~scanf("%d",&n)){
for(int i = ; i <= n; ++i){
done[i] = false;
g[i].clear();
}
for(int i = ; i < n; ++i){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
if(solve(,'A'))
for(int i = ; i <= n; ++i)
printf("%c%c",ans[i],i == n?'\n':' ');
else puts("Impossible!");
}
return ;
}
CodeForces 321C Ciel the Commander的更多相关文章
- Codeforces G. Ciel the Commander
题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 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最高),如果两个不同的节点有相同等级的父节点 ...
- 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 321C
Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected ...
- Codeforces 321E Ciel and Gondolas
传送门:http://codeforces.com/problemset/problem/321/E [题解] 首先有一个$O(n^2k)$的dp. # include <stdio.h> ...
- Codeforces 321D Ciel and Flipboard(结论题+枚举)
题目链接 Ciel and Flipboard 题意 给出一个$n*n$的正方形,每个格子里有一个数,每次可以将一个大小为$x*x$的子正方形翻转 翻转的意义为该区域里的数都变成原来的相反数. ...
- codeforces B. Ciel and Flowers 解题报告
题目链接:http://codeforces.com/problemset/problem/322/B 题目意思:给定红花.绿花和蓝花的朵数,问组成四种花束(3朵红花,3朵绿花,3朵蓝花,1朵红花+1 ...
随机推荐
- 【进度总结】第一个web应用程序(未完成)
web程序快速导航 使用Eclipse for Java EE Web Development,并配置Tomcat,这部分内容在众多教程中都描述的十分详细.我直接从代码部分开始记录流程: 这张图是We ...
- 查看mysql已有用户并删除
查看: SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; 删除: drop us ...
- 仿天猫淘宝的ShopNC好商城原生Android 客户端源码项目
开发环境:Android Studio 2.0 | Gradle 2.0.0最后更新:2016-04-28 简介:基于好商城V4的Android客户端 目前已完成的功能(概述): 1.启动页 -> ...
- Django添加tinyMCE编辑器
tinymce的使用方法很简单,只需要在html页面中包含如下: <!-- Place inside the <head> of your HTML --> <scrip ...
- Python 学习日志9月20日
9月20日 周三 多大年龄了,还活得像个小孩.——急什么,人生又不长. 你习惯了思考宇宙星辰,一百年真的不长,一生也就不那么长,许多人的价值观念你也就无法理解.同样,许多人也无法理解你的价值观念,感兴 ...
- C#入门(3)
C#入门(3) Delegates, Events, Lambda Expressions 最早的windows是使用c风格的函数指针来进行callback的,但是这样仅仅传递了一个内存中的地址,无法 ...
- 动态规划初步--最长上升子序列(LIS)
一.问题 有一个长为n的数列 a0,a1,a2...,an-1a.请求出这个序列中最长的上升子序列的长度和对应的子序列.上升子序列指的是对任意的i < j都满足ai < aj的子序列. 二 ...
- PAT (Basic Level) Practise (中文)-1030. 完美数列(25)
PAT (Basic Level) Practise (中文)-1030. 完美数列(25) http://www.patest.cn/contests/pat-b-practise/1030 给 ...
- 电商技术中企业数据总线ESB和注册服务管理的区别
一.概述 1.什么是ESB 就是企业数据总线的意思,他的核心功能就是兼容各种协议接口,可以将数据在各种协议之间进行流转,并且可以针对数据格式进行编排转换. 异构系统,功能繁多,复杂 代表性的项目有:J ...
- javascript设计模式(张容铭) 第14章 超值午餐-组合模式 学习笔记
JS 组合模式更常用于创建表单上,比如注册页面可能有不同的表单提交模块.对于这些需求我们只需要有基本的个体,然后通过一定的组合即可实现,比如下面这个页面样式(如图14-2所示),我们来用组合模式实现. ...