Codeforces 321C Ciel the Commander 树分治 2017/1/10
来源:互联网 发布:内网穿透 软件 编辑:程序博客网 时间:2024/05/04 10:32
题目链接:见这里
题意:给出一棵n个节点的树,为每个节点分配一个大写字母(从A到Z,这里认为A>B>C…>Z),每种字母可以无限使用,
满足对于任意字母相同的两个节点u和v,在u到v的路径上存在一个节点的字母大于u节点处和v节点处的字母,
输出分配方案,无解输出”Impossible”
分析:找树的重心,把它染成最大的字母,然后把它去掉,树就会分成很多份,并且在这些树里我们不再使用最大的字母,我们一直重复这个过程,每次找重心,染字母,分块,就可以了,每次树的大小都起码减半,所以26个字母够用。
代码如下:
#include <bits/stdc++.h>using namespace std;const int maxn = 1e5 + 10;int n, num[maxn], dp[maxn], siz, root;bool vis[maxn];struct node{ int v, nxt;}E[maxn*2];int head[maxn], cnt;void init(){ memset(head, -1, sizeof(head)); cnt = 0; num[1] = dp[0] = n; memset(vis, false, sizeof(vis));}void addedge(int u, int v){ E[cnt].v = v, E[cnt].nxt = head[u], head[u] = cnt++;}char s[maxn];void getroot(int u, int fa){ dp[u] = 0; num[u] = 1; for(int i = head[u]; ~i; i = E[i].nxt){ int v = E[i].v; if(vis[v] || v == fa) continue; getroot(v, u); num[u] += num[v]; dp[u] = max(dp[u], num[v]); } dp[u] = max(dp[u], siz - num[u]); if(dp[u] < dp[root]) root = u;}void work(int u, int fa, int dep){ root = 0; siz = num[u]; getroot(u, fa); s[root] = dep + 'A'; vis[root] = 1; for(int i = head[root]; ~i; i = E[i].nxt){ int v = E[i].v; if(vis[v]) continue; work(v, u, dep+1); }}int main(){ scanf("%d", &n); init(); for(int i = 1; i < n; i++){ int u, v; scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } work(1, 1, 0); for(int i = 1; i <= n; i++) cout << s[i] << ' '; cout << endl;}
0 0
- Codeforces 321C Ciel the Commander 树分治 2017/1/10
- Codeforces 321C Ciel the Commander 树分治
- CodeForces 321 C.Ciel the Commander(树分治)
- CF 321C(Ciel the Commander-点分治)
- 树分治(点分治)CF 321C - Ciel the Commander
- Codeforces Round #190 (Div. 2) E. Ciel the Commander 题目与题解翻译
- [Codeforces 817E] Choosing The Commander Trie树
- CodeForces 321A Ciel and Robot[暴力]
- CodeForces 321 A - Ciel and Robot
- Codeforces 321B Ciel and Duel KM
- Codeforces 321E Ciel and Gondolas(DP)
- CodeForces 321A Ciel and Robot
- [Codeforces 321E] Ciel and Gondolas
- CodeForces 817E Choosing The Commander题解
- Codeforces Round 190 div.2 322C 321A Ciel and Robot
- Codeforces Round #190 (Div. 2)-C. Ciel and Robot
- Educational Codeforces Round 23 E. Choosing The Commander (字典树)
- Educational Codeforces Round 23 817E. Choosing The Commander 字典树 位运算
- 算法竞赛入门经典 习题2-8 子序列的和(subsequence)
- 学习笔记-增强学习
- opencv基本绘图函数--点,线,矩形,圆等
- LeetCode - 228. Summary Ranges - 思路详解- C++
- svm 简介
- Codeforces 321C Ciel the Commander 树分治 2017/1/10
- iOS 深入理解 @property
- 关于page(pageContext)属性(页面跳转后不能取得值)
- 17 - 04 - 02 图解HTTP(37)
- 2014.9.6联合模拟二 T1分发糖果
- 洛谷1042 乒乓球
- 洛谷 1007 独木桥
- 开发工具小技巧
- Team Rankings