码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 321C Ciel the Commander 树分治裸题

时间:2015-07-17 22:51:57      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

题目链接

题意:

 给定一棵树,要用字母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[N], dp[N], siz, root;
bool vis[N];
vector<int>G[N];
char s[N];
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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 321C Ciel the Commander 树分治裸题

标签:

原文地址:http://blog.csdn.net/qq574857122/article/details/46931215

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!