树上的构造 树分治+树重心的性质 Codeforces Round #190 (Div. 2) E
http://codeforces.com/contest/322/problem/E
1 second
256 megabytes
standard input
standard output
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!".
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.
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!".
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
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.
题目大意:题意:给出一棵树,给每一个点填上一个字母,要求是得任意两个相同字母的点u,v路径上至少有一个点大于这个字母(A最大)
思路:
'A'节点必然只有一个,且他的位置放在重心一定是最优的。(证明利用反证法证明)
然后我们就每次找重心即可。
(md我好菜啊,又不会构造)
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
int n;
vector<int> G[maxn];
int val[maxn], sz[maxn];
bool vis[maxn]; void dfs_sz(int u, int fa){
sz[u] = ;
for (int i = ; i < G[u].size(); i++){
int v = G[u][i];
if (v == fa || vis[v]) continue;
dfs_sz(v, u);
sz[u] += sz[v];
}
} void dfs_ce(int u, int fa, int &cetroid, int &maxcnt, int allcnt){
int tmp = allcnt - sz[u];
for (int i = ; i < G[u].size(); i++){
int v = G[u][i];
if (v == fa || vis[v]) continue;
dfs_ce(v, u, cetroid, maxcnt, allcnt);
tmp = max(tmp, sz[v]);
}
if (tmp < maxcnt) {cetroid = u, maxcnt = tmp;}
} void dfs(int u, int deep){
int cetroid, maxcnt = maxn * ;
dfs_sz(u, -);
dfs_ce(u, -, cetroid, maxcnt, sz[u]);
val[cetroid] = deep;
vis[cetroid] = true;
for (int i = ; i < G[cetroid].size(); i++){
int v = G[cetroid][i];
if(vis[v]) continue;
dfs(v, deep + );
}
vis[cetroid] = false;
} bool solve(){
dfs(, );
for (int i = ; i <= n; i++){
if (val[i] > ) return false;
}
for (int i = ; i <= n; i++){
val[i]--;
printf("%c ", val[i] + 'A');
}
cout << endl;
return true;
} int main(){
cin >> n;
for (int i = ; i < n; i++){
int u, v; scanf("%d%d", &u, &v);
G[u].pb(v), G[v].pb(u);
}
if (!solve()) puts("Impossible!");
return ;
}
树上的构造 树分治+树重心的性质 Codeforces Round #190 (Div. 2) E的更多相关文章
- 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 ...
- 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E
http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和
Codeforces Round #588 (Div. 2)-E. Kamil and Making a Stream-求树上同一直径上两两节点之间gcd的和 [Problem Description ...
- 构造 Codeforces Round #302 (Div. 2) B Sea and Islands
题目传送门 /* 题意:在n^n的海洋里是否有k块陆地 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 输出完k个L后,之后全部输出S:) 5 10 的例子可以是这样的: LSLS ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造
Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 ht ...
随机推荐
- CDOJ ABCDE dp(前缀和优化)
题目链接: http://acm.uestc.edu.cn/#/problem/show/1307 ABCDE Time Limit: 1000/1000MS (Java/Others)Memory ...
- CDN问题
名称解释:正反向解析 主辅服务器 domain zone 记录:SOA.NS.A.CNAME.PRT.MX DNS配置文件中各字段作用,如TTL DNS端口号? TCP53和UDP53使用场合 Lin ...
- 【week10】规格说明书练习-吉林市1日游
假设我们全班同学及教师去吉林省吉林市1日游,请为这次活动给出规格说明书. 版本:1.0 编订:于淼 团队:2016级计算机技术全体同学 日期:2016/11/19 1.引言 1.1 编写目的 1.2 ...
- 使用union all 遇到的问题(俩条sql语句行数的和 不等于union all 后的 行数的和 !);遗留问题 怎么找到 相差的呐俩条数据 ?
create table buyer as SELECT b.id AS bankid FROM v_product_deal_main m, base_member b WHERE b.id = m ...
- session,cookie
简单: cookie可以由客户端,服务端产生,保存在客户端,客户端可以更改cookie中的内容 session只能在服务端产生,保存在服务端,会产生一个session_id,一个域下,只有一个id,这 ...
- 解决 Package test is missing dependencies for the following libraries: libcrypto.so.1.0.0
根据项目要求需要用到openssl这个库,看了看编译环境幸好本身就集成了该库.但在编译openssl的功能时,碰到缺少类库的错误. Package test is missing dependenci ...
- BZOJ 2109 航空管制(拓扑排序+贪心)
绝世好题啊.. 题意:给出一个DAG,和每个点要求出现在这个DAG里面的拓扑排序的位置<=ti,求出所有可能的拓扑排序里面每个点出现的位置的最小值. 正着做不好做,考虑反着做,建立这个图的反图. ...
- MethodHandle
JDK7为间接调用方法引入新的API,在java.lang.invoke包下,可以看作为反射的升级版,但它不像反射API那样显得冗长.繁重 主要的类 MethodHandle 方法句柄.对可直接执行的 ...
- BZOJ4925 城市规划
对每个人行道求出移动距离在哪些区间内时其在建筑物前面.现在问题即为选一个点使得其被最多的区间包含.差分即可.对建筑暴力去掉重叠部分.开始时没有去重用了nm次vector的push_back,时间大概是 ...
- EVE-NG硬盘扩容,存储海量镜像
EVE-NG硬盘扩容,存储海量镜像 来源 http://blog.51cto.com/sms1107/1928453 一.查看当前磁盘使用情况 /dev/mapper/eve--ng--vg-root ...