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 ...
随机推荐
- Y2165终极分班考试题。
第一题答案:D 2.下面关于SQLServer中视图的说法错误的是:C 答案:视图是数据中存储的数据值得集合. 3.在JAVA中,关于日志记录工具log4j的描述错误的是:D 答案:log4j个输出级 ...
- [转]为ReportViewer导出的PDF文档加上水印
接到一個頗富挑戰性的需求,Reporting Service或RDLC報表可匯出成Excel.PDF等檔案格式,對一般麻瓜型使用者而言,PDF唯讀,Excel則可修改,業務單位希望在拿到報表紙本時加以 ...
- mongodb-3.2.8 单机复制集安装
规划: replSet 复制集名称: rs1 MongoDB数据库安装安装路径为:/usr/local/mongodb/ 复制集成员IP与端口: 节点1: localhost:28010 (默认的 ...
- 按Esc键实现关闭窗体
实现效果: 知识运用: KeyEventArgs类的KeyData属性 //获取KeyDown或KeyUp事件的键数据 public Keys KeyData {get;} 实现代码: private ...
- JSONP 跨域请求 - 获取JSON数据
如何用原生方式使用JSONP? 下边这一DEMO实际上是JSONP的简单表现形式,在客户端声明回调函数之后,客户端通过script标签向服务器跨域请求数据,然后服务端返回相应的数据并动态执行回调函数. ...
- idea Please specify commit message
在idea中使用github来进行版本控制的时候, 当点击提交的时候遇到了这个问题 错误: Please specify commit message 解决方法: 在commit message中填写 ...
- How to debug add-ins for arcgis
Debugging add-ins To debug an add-in, follow these steps: Confirm that the add-in is deployed to the ...
- js 两个数组对象根据账号比较去重,解决直接splice后数组索引改变
目的获取Arr2中不包含在arr1中的对象 根据Account进行比较,如果相等则删除tempArr数组对象. 结果返回张三 var arr1=[{"account":" ...
- C语言数据类型_02
C语言数据类型:
- 文件读写FILE类
1. 新建一个文件: FILE *f = fopen("a.txt","w+"); (1)fopen()函数介绍fopen的原型是:FILE *fopen(co ...