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 ...
随机推荐
- 修复Windows XP服务扩展视图显示空白
在服务管理控制台(Services.msc)扩展视图显示服务的描述,也有启动或停止服务的链接.在某些系统中,扩展视图可能出现一片空白,如图所示: 这是因为没有注册 JScript.dll文件,要解决此 ...
- 解决Git在更新项目时报凭证错误(Authentication failed)
报此错误,大概率原因是用户名和密码弄错了,我用的阿里云,在网上找了半天发现Git远程仓库用的用户名和密码不是阿里云登陆用的账户密码,必须另外设置: 链接:code.aliyun.com/profile ...
- linux安装redis官方教程
官方链接:http://redis.io/download Download, extract and compile Redis with: $ wget http://download.redis ...
- APP启动原理
当我们点击一个应用的时候,系统会自动创建一个相应的activity类实例,然后执行Oncreate方法,接着会执行以下两行代码,解释如下: super.onCreate(savedInstanceSt ...
- Slacklining 2017/2/6
原文 Get ready for a different kind of challenge What happens when mountain climbers take a day off?Ap ...
- win7 x64和win10 x64 windbg 本地调试记录
今天看CSDN和某文档看了win7 64位 和win10 64位 的windbg本地调试内核的办法 win7 x64 Step1:kdbgctrl –db Step2:kdbgctrl –e Step ...
- SQLite基础教程目录
SQLite基础教程目录 SQLite主页 SQLite概述 SQLite -安装 SQLite -命令 SQLite -语法 SQLite -数据类型 SQLite -创建数据库 SQLite -附 ...
- 数据库_4_SQL介绍
SQL SQL:Structured Query Language,结构化查询语言(数据已查询为主:99%是在进行查询操作) what型语言,而非how型的语言. SQL分为三个部分: DDL: ...
- scanf函数详解
函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]);scanf()函数是通用终端格式化输入函数,它从标准输入设备(键 ...
- ios之UIImageView
UIImageView,顾名思义,是用来放置图片的.使用Interface Builder设计界面时,当然可以直接将控件拖进去并设置相关属性,这就不说了,这里讲的是用代码. 1.创建一个UIImage ...