题目链接:http://codeforces.com/problemset/problem/321/C
解题思路:
点分治模板题。
每次找到重心给他分配一个字符,分治往下走的时候分配的字符ASCII码 \(+1\) 即可。
示例程序:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
vector<int> g[maxn];
int n;
bool vis[maxn];
char id[maxn];
int get_size(int u, int p) {
if (vis[u]) return 0;
int sum = 1;
for (auto v : g[u])
if (v != p)
sum += get_size(v, u);
return sum;
}
int get_wc(int u, int p, int tot, int &wc) {
if (vis[u]) return 0;
int sum = 1, ms = 0;
for (auto v : g[u]) {
if (v == p) continue;
int tmp = get_wc(v, u, tot, wc);
ms = max(ms, tmp);
sum += tmp;
}
ms = max(ms, tot - sum);
if (ms <= tot / 2) wc = u;
return sum;
}
void dfs(int u, char c) {
if (vis[u]) return;
get_wc(u, -1, get_size(u, -1), u);
vis[u] = true;
id[u] = c;
for (auto v : g[u])
dfs(v, c+1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 'A');
for (int i = 1; i <= n; i++)
printf("%c ", id[i]);
return 0;
}
标签:wc,get,int,题解,sum,Commander,maxn,ms,CF321C
From: https://www.cnblogs.com/quanjun/p/17503836.html