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 ...
随机推荐
- 终于 Vue.js 成为世界一流的框架
终于 Vue.js 成为世界一流的框架 随着美团开源基于 Vue.js 的微信小程序框架 mpvue, Vue.js 在微信小程序端的能力被补齐,于是 Vue.js 成为了一个唯一能在 Web, H5 ...
- Java之final、static关键字及匿名对象
个人通俗理解: 1.final:首先被final修饰的变量就自动变成的不能被修改的常量了.被修饰的类会自动变成太监类,只能有父类,不能有子类:被修饰的方法也不能被子类重写了:被修饰的引用变量值也不能更 ...
- javascript 流程控制及函数
回顾 基本语法 在html的使用 <script></script> 注释 ///* */ 指令结束符 ;换行 输出 console.log()document.write() ...
- 解析 MFC 中的 FromHandle
MFC 对 Windows API 进行了封装,在很多方面都会提供便利.用 FromHandle 返回零时对象的指针,就可以调用各种类的方法.临时对象会在 OnIdle 中销毁.这里对 FromHan ...
- UIView和Masonry实现动画效果
Masonry 实现动画效果如下: //button点击方法 - (void)clickedButton { static BOOL isMove; //默认是NO Weakify(weakSelf) ...
- MediaRecord一些使用记录
今天学习了MediaRecord的使用,第一次使用做个记录. MediaRecord作用是声音录制,使用步骤如下: 1.新建出音频文件代码如下: 先创建出用于存储音频文件 File dir = new ...
- 怎样将英文版的Eclipse转为中文版的?
=====>1.打开eclipse储存文件夹 =====>2.在eclipse文件中找到dropins文件 =====>3.把已经下载好的eclipse汉化包复制到dropins中 ...
- iOS7.1企业应用"无法安装应用程序 因为证书无效"的解决方案
今天升级了iOS7.1后发现通过之前的url无法安装企业应用了,一直提示“无法安装应用程序 因为http://xxx.xxx.xxx证书无效”,折腾了一番,终于在StackOverFlow上找到了答案 ...
- CAD交互绘制批注(网页版)
js中实现代码说明: 动态拖放时的绘制事件: function DynWorldDrawComment( pCustomEntity, pWorldDraw, curPt) { // 得到绘制参数. ...
- node mocha mochawesome报安装不成功
1.进行cnpm安装: npm install cnpm -g --registry=https://registry.npm.taobao.org 2.查看cnpm版本 cnpm -v 3.安装mo ...