树上的构造 树分治+树重心的性质 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 ...
随机推荐
- scrum立会报告+燃尽图(第二周第六次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2251 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公冶 ...
- 通俗理解Hilbert希尔伯特空间
作者:qang pan 链接:https://www.zhihu.com/question/19967778/answer/28403912 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权, ...
- flask验证登录学习过程(1)---实践flask_jwt
flask_jwt应用代码: from flask import Flask from flask_jwt import JWT,jwt_required,current_identity from ...
- js中call(),apply(),以及prototype的含义
最近段时间主要学习前端去了,然而所遇到的一些问题我觉得有必要去深究一下 prototype: 1 js中有三种表达方法 类方法,属性方法,原型方法 function People(name) { th ...
- python 将base64字符串还原为图片
今天弄验证码的时候发现,验证码的图片的src竟然是下面的这么一个一串字符串,吓到,好像不可以http请求的,第一次见,就好尴尬,去网上搜索了一下,说是: 这是Data URI scheme. data ...
- 使用 TestNG 并发测试 ;
使用TestNG对IE /Chrome/firefox 进行兼容性并发测试 : package testNGTest; import org.openqa.selenium.By; import or ...
- php缩略图
/*引入文件Easyphpthumbnail.class.php 引用地址:http://www.itdaodan.com/article-detail-id-252.html */ class ...
- RAD Studio 10.3 Rio (BCB & Dephi) 发布啦
期盼已久的RAD Studio 10.3 Rio 终于发布了: 下载链接:http://altd.embarcadero.com/download/radstudio/10.3/delphicbui ...
- Idea报错Command line is too long
需要在该项目文件夹下.idea/workspace.xml中添加 <component name="PropertiesComponent"> ... <prop ...
- 【刷题】洛谷 P4716 【模板】最小树形图
题目背景 这是一道模板题. 题目描述 给定包含 \(n\) 个结点, \(m\) 条有向边的一个图.试求一棵以结点 \(r\) 为根的最小树形图,并输出最小树形图每条边的权值之和,如果没有以 \(r\ ...