树上的构造 树分治+树重心的性质 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 ...
随机推荐
- struts常量<constant>说明
1.<constant name="struts.action.extension" value="do" />这个时候访问action都必须加.d ...
- Android开发第二阶段(1)
今天:总结第一阶段的冲刺成果,第一阶段就是主要是学习andriod开发,参考文件有<黑马教学视频><Mars教学视频>...结果在看的过程遇到很多问题特别是对java的一些理解 ...
- vue+vue-video-player实现弹窗播放视频
将视频播放器标签放在对话框标签中,实现弹窗 template 中 <el-dialog :visible.sync="dialogVisible" width='680px' ...
- MDL
1 先是mdl的数据结构. 2 下面根据用法逐步的讲解mdl数据结构的含义:一般用法,先是 IoAllocateMdl :原型为: 最常用的是VirtualAddress和Length.把自己的Non ...
- React-native APK打包
安卓相关工具配置到环境变量,这样可以将安卓相关工具可以直接在cmd命令中调用 1 检查gradle版本 查看里面对应的编译工具版本号,如果提示版本不对你,那么直接去更新android sdk,相关的s ...
- WebForm与MVC模式优缺点
Asp.net Web开发方式,分为两种: 1. WebForm开发 2. Asp.Net MVC开发 MVC是微软对外公布的第一个开源的表示层框架,MVC目的不是取代WebForm开发,只是web开 ...
- LoadRunner函数大全之中文解释
LoadRunner函数大全之中文解释
- influxdb 命令
写入数据: curl -X POST -d '[{"name":"foo","columns":["val"],&quo ...
- CSS中可以和不可以继承的属性【转】
一.无继承性的属性 1.display:规定元素应该生成的框的类型 2.文本属性: vertical-align:垂直文本对齐 text-decoration:规定添加到文本的装饰 text-shad ...
- 第89天:HTML5中 访问历史、全屏和网页存储API
一.访问历史 API 通过history对象实现前进.后退和刷新之类的操作 history新增的两个方法history.replaceState()和history.pushState()方法属于HT ...