Codeforces 321C Ciel the Commander 树分治
2015-07-17 18:43
387 查看
题目链接
题意:
给定一棵树,要用字母A-Z 填到每个节点上
字母可以无限使用,但A至多只能用一次
目标:对于任意两个相同字母的节点,他们之间的路径上必须有至少一个节点的字母比他们小
例如:在两个C之间至少要有一个A 或者一个B
问:
输出填涂方案。
树分治即可,最多支持2^25个节点,不会无解。
题意:
给定一棵树,要用字母A-Z 填到每个节点上
字母可以无限使用,但A至多只能用一次
目标:对于任意两个相同字母的节点,他们之间的路径上必须有至少一个节点的字母比他们小
例如:在两个C之间至少要有一个A 或者一个B
问:
输出填涂方案。
树分治即可,最多支持2^25个节点,不会无解。
#include <iostream> #include <string> #include <vector> #include <cstring> #include <cstdio> #include <map> #include <queue> #include <algorithm> #include <stack> #include <cstring> #include <cmath> #include <set> #include <vector> using namespace std; template <class T> inline bool rd(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c<'0' || c>'9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template <class T> inline void pt(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) pt(x / 10); putchar(x % 10 + '0'); } typedef long long ll; typedef pair<int, int> pii; const int inf = 1e9; const int N = 1e5+10; int n; int num , dp , siz, root; bool vis ; vector<int>G ; char s ; void getroot(int u, int fa) { dp[u] = 0; num[u] = 1; for (auto v : G[u]) { if (vis[v] || v == fa)continue; getroot(v, u); num[u] += num[v]; dp[u] = max(num[v], dp[u]); } 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] = true; for (auto v : G[root]) { if (vis[v])continue; work(v, u, dep + 1); } } void init() { dp[0] = num[1] = n; memset(vis, 0, sizeof vis); } int main() { rd(n); for (int i = 1, u, v; i < n; i++) { rd(u); rd(v); G[u].push_back(v); G[v].push_back(u); } init(); work(1, 1, 0); for (int i = 1; i <= n; i++)putchar(s[i]), putchar(' '); return 0; }
相关文章推荐
- MySQL 性能优化的最佳20多条经验分享
- Chongqing is not fierce winds
- (4.1.8.7)Android Fragment 你应该知道的一切
- Linux-6.3-TCP
- 创建线程池
- html教程
- 多线程数据共享
- eclipse创建android项目,无法正常预览布局文件
- andrioid——checkbox勾选按钮自定义样式
- fastclick使用方法
- CSS中值得记住的一些技巧
- 慧荣 SM3257ENLT - ISP 131112-AA量产
- 在Android程序中使用已有的SQLite数据库
- Codeforces 321B Ciel and Duel KM
- form onsubmit
- 通过非暴力促进国内操作系统的可行性分析
- 黑马程序员——Java 网络编程 (第十一篇)
- 银行系统测试版
- Android帮助文档打开慢的解决方法
- 选择排序